From 316331a1f6faf070563091b9957a50e2d91d5c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 24 May 2023 11:21:33 +0200 Subject: [PATCH 1/3] Add log streaming --- src/CLI/Console.php | 30 ++++++++++++++++++------------ tests/CLI/ConsoleTest.php | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/CLI/Console.php b/src/CLI/Console.php index 916df25..6641fd9 100644 --- a/src/CLI/Console.php +++ b/src/CLI/Console.php @@ -27,7 +27,7 @@ public static function title(string $title): bool */ public static function log(string $message): int|false { - return \fwrite(STDOUT, $message."\n"); + return \fwrite(STDOUT, $message . "\n"); } /** @@ -40,7 +40,7 @@ public static function log(string $message): int|false */ public static function success(string $message): int|false { - return \fwrite(STDOUT, "\033[32m".$message."\033[0m\n"); + return \fwrite(STDOUT, "\033[32m" . $message . "\033[0m\n"); } /** @@ -53,7 +53,7 @@ public static function success(string $message): int|false */ public static function error(string $message): int|false { - return \fwrite(STDERR, "\033[31m".$message."\033[0m\n"); + return \fwrite(STDERR, "\033[31m" . $message . "\033[0m\n"); } /** @@ -66,7 +66,7 @@ public static function error(string $message): int|false */ public static function info(string $message): int|false { - return \fwrite(STDOUT, "\033[34m".$message."\033[0m\n"); + return \fwrite(STDOUT, "\033[34m" . $message . "\033[0m\n"); } /** @@ -79,7 +79,7 @@ public static function info(string $message): int|false */ public static function warning(string $message): int|false { - return \fwrite(STDERR, "\033[1;33m".$message."\033[0m\n"); + return \fwrite(STDERR, "\033[1;33m" . $message . "\033[0m\n"); } /** @@ -92,7 +92,7 @@ public static function warning(string $message): int|false */ public static function confirm(string $question): string { - if (! self::isInteractive()) { + if (!self::isInteractive()) { return ''; } @@ -131,9 +131,9 @@ public static function exit(int $status = 0): void * @param int $timeout * @return int */ - public static function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1): int + public static function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1, callable $onProgress = null): int { - $cmd = '( '.$cmd.' ) 3>/dev/null ; echo $? >&3'; + $cmd = '( ' . $cmd . ' ) 3>/dev/null ; echo $? >&3'; $pipes = []; $process = \proc_open( @@ -157,8 +157,14 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri } while (\is_resource($process)) { - $stdout .= \stream_get_contents($pipes[1]); - $stderr .= \stream_get_contents($pipes[2]); + $stdoutContents = \stream_get_contents($pipes[1]) ?: ''; + $stderrContents = \stream_get_contents($pipes[2]) ?: ''; + if (isset($onProgress) && (!empty($stdoutContents) || !empty($stderrContents))) { + $onProgress($stdoutContents, $stderrContents); + } + + $stdout .= $stdoutContents; + $stderr .= $stderrContents; $status .= \stream_get_contents($pipes[3]); if ($timeout > 0 && \time() - $start > $timeout) { @@ -167,7 +173,7 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri return 1; } - if (! \proc_get_status($process)['running']) { + if (!\proc_get_status($process)['running']) { \fclose($pipes[1]); \fclose($pipes[2]); \proc_close($process); @@ -211,7 +217,7 @@ public static function loop(callable $callback, int $sleep = 1 /* seconds */, in sleep($delay); } - while (! connection_aborted() || PHP_SAPI == 'cli') { + while (!connection_aborted() || PHP_SAPI == 'cli') { $suspend = $sleep; try { diff --git a/tests/CLI/ConsoleTest.php b/tests/CLI/ConsoleTest.php index 790c32a..51d7c82 100755 --- a/tests/CLI/ConsoleTest.php +++ b/tests/CLI/ConsoleTest.php @@ -38,6 +38,26 @@ public function testExecuteBasic() $this->assertEquals(0, $code); } + public function testExecuteStream() + { + $stdout = ''; + $stderr = ''; + $stdin = ''; + + $stdoutStream = ''; + $stderrStream = ''; + $code = Console::execute('printf 1 && sleep 1 && printf 2 && sleep 1 && printf 3 && sleep 1 && printf 4 && sleep 1 && printf 5', $stdin, $stdout, $stderr, 10, function($stdout, $stderr) use (&$stdoutStream, &$stderrStream) { + $stdoutStream .= $stdout; + $stderrStream .= $stderr; + }); + + $this->assertEquals('', $stderr); + $this->assertEquals('12345', $stdout); + $this->assertEquals('', $stderrStream); + $this->assertEquals('12345', $stdoutStream); + $this->assertEquals(0, $code); + } + public function testExecuteStdOut() { $stdout = ''; From 068c16eb821feaea7ce8275dfdbd5515fe2a2aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 24 May 2023 11:22:21 +0200 Subject: [PATCH 2/3] Format --- src/CLI/Console.php | 20 ++++++++++---------- tests/CLI/ConsoleTest.php | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/CLI/Console.php b/src/CLI/Console.php index 6641fd9..1706113 100644 --- a/src/CLI/Console.php +++ b/src/CLI/Console.php @@ -27,7 +27,7 @@ public static function title(string $title): bool */ public static function log(string $message): int|false { - return \fwrite(STDOUT, $message . "\n"); + return \fwrite(STDOUT, $message."\n"); } /** @@ -40,7 +40,7 @@ public static function log(string $message): int|false */ public static function success(string $message): int|false { - return \fwrite(STDOUT, "\033[32m" . $message . "\033[0m\n"); + return \fwrite(STDOUT, "\033[32m".$message."\033[0m\n"); } /** @@ -53,7 +53,7 @@ public static function success(string $message): int|false */ public static function error(string $message): int|false { - return \fwrite(STDERR, "\033[31m" . $message . "\033[0m\n"); + return \fwrite(STDERR, "\033[31m".$message."\033[0m\n"); } /** @@ -66,7 +66,7 @@ public static function error(string $message): int|false */ public static function info(string $message): int|false { - return \fwrite(STDOUT, "\033[34m" . $message . "\033[0m\n"); + return \fwrite(STDOUT, "\033[34m".$message."\033[0m\n"); } /** @@ -79,7 +79,7 @@ public static function info(string $message): int|false */ public static function warning(string $message): int|false { - return \fwrite(STDERR, "\033[1;33m" . $message . "\033[0m\n"); + return \fwrite(STDERR, "\033[1;33m".$message."\033[0m\n"); } /** @@ -92,7 +92,7 @@ public static function warning(string $message): int|false */ public static function confirm(string $question): string { - if (!self::isInteractive()) { + if (! self::isInteractive()) { return ''; } @@ -133,7 +133,7 @@ public static function exit(int $status = 0): void */ public static function execute(string $cmd, string $stdin, string &$stdout, string &$stderr, int $timeout = -1, callable $onProgress = null): int { - $cmd = '( ' . $cmd . ' ) 3>/dev/null ; echo $? >&3'; + $cmd = '( '.$cmd.' ) 3>/dev/null ; echo $? >&3'; $pipes = []; $process = \proc_open( @@ -159,7 +159,7 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri while (\is_resource($process)) { $stdoutContents = \stream_get_contents($pipes[1]) ?: ''; $stderrContents = \stream_get_contents($pipes[2]) ?: ''; - if (isset($onProgress) && (!empty($stdoutContents) || !empty($stderrContents))) { + if (isset($onProgress) && (! empty($stdoutContents) || ! empty($stderrContents))) { $onProgress($stdoutContents, $stderrContents); } @@ -173,7 +173,7 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri return 1; } - if (!\proc_get_status($process)['running']) { + if (! \proc_get_status($process)['running']) { \fclose($pipes[1]); \fclose($pipes[2]); \proc_close($process); @@ -217,7 +217,7 @@ public static function loop(callable $callback, int $sleep = 1 /* seconds */, in sleep($delay); } - while (!connection_aborted() || PHP_SAPI == 'cli') { + while (! connection_aborted() || PHP_SAPI == 'cli') { $suspend = $sleep; try { diff --git a/tests/CLI/ConsoleTest.php b/tests/CLI/ConsoleTest.php index 51d7c82..d80582e 100755 --- a/tests/CLI/ConsoleTest.php +++ b/tests/CLI/ConsoleTest.php @@ -46,7 +46,7 @@ public function testExecuteStream() $stdoutStream = ''; $stderrStream = ''; - $code = Console::execute('printf 1 && sleep 1 && printf 2 && sleep 1 && printf 3 && sleep 1 && printf 4 && sleep 1 && printf 5', $stdin, $stdout, $stderr, 10, function($stdout, $stderr) use (&$stdoutStream, &$stderrStream) { + $code = Console::execute('printf 1 && sleep 1 && printf 2 && sleep 1 && printf 3 && sleep 1 && printf 4 && sleep 1 && printf 5', $stdin, $stdout, $stderr, 10, function ($stdout, $stderr) use (&$stdoutStream, &$stderrStream) { $stdoutStream .= $stdout; $stderrStream .= $stderr; }); From bec6cb6686485f66331ec83d320f3b6306fa8e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 9 Jun 2023 10:06:08 +0200 Subject: [PATCH 3/3] Add process to callback --- src/CLI/Console.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CLI/Console.php b/src/CLI/Console.php index 1706113..4b8af2a 100644 --- a/src/CLI/Console.php +++ b/src/CLI/Console.php @@ -160,7 +160,7 @@ public static function execute(string $cmd, string $stdin, string &$stdout, stri $stdoutContents = \stream_get_contents($pipes[1]) ?: ''; $stderrContents = \stream_get_contents($pipes[2]) ?: ''; if (isset($onProgress) && (! empty($stdoutContents) || ! empty($stderrContents))) { - $onProgress($stdoutContents, $stderrContents); + $onProgress($stdoutContents, $stderrContents, $process); } $stdout .= $stdoutContents;