From d2fc0f553729671bc92f41c7125d1a26d1d0cddd Mon Sep 17 00:00:00 2001 From: BELADEL ILYES ABDELRAZAK Date: Sun, 14 Mar 2021 00:58:33 +0100 Subject: [PATCH] Support the ability to output to console while the command getting executed --- README.md | 2 ++ src/Command.php | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 6697397..3218137 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ $command->setStdIn('string'); * `$escapeArgs`: Whether to escape any argument passed through `addArg()`. Default is `true`. * `$escapeCommand`: Whether to escape the command passed to `setCommand()` or the constructor. This is only useful if `$escapeArgs` is `false`. Default is `false`. + * `$streamOutput`: Whether to echo the output to the console while the current command getting executed. + Default is `false`. * `$useExec`: Whether to use `exec()` instead of `proc_open()`. This is a workaround for OS which have problems with `proc_open()`. Default is `false`. * `$captureStdErr`: Whether to capture stderr when `useExec` is set. This will try to redirect diff --git a/src/Command.php b/src/Command.php index 7fe4380..410f10b 100644 --- a/src/Command.php +++ b/src/Command.php @@ -17,6 +17,12 @@ class Command */ public $escapeArgs = true; + /** + * @var bool whether to stream the output of current command getting executed + * Default is `false`. + */ + public $streamOutput = false; + /** * @var bool whether to escape the command passed to `setCommand()` or the * constructor. This is only useful if `$escapeArgs` is `false`. Default @@ -469,9 +475,13 @@ public function execute() // while (($out = fgets($pipes[1])) !== false) { $this->_stdOut .= $out; + if($this->streamOutput) + echo $out; } while (($err = fgets($pipes[2])) !== false) { $this->_stdErr .= $err; + if($this->streamOutput) + echo $err; } $runTime = $hasTimeout ? time() - $startTime : 0; @@ -511,6 +521,18 @@ public function execute() } fclose($pipes[0]); } + + if($this->streamOutput) + { + while(!feof($pipes[1])) + { + $return_message = fgets($pipes[1], 1024); + if (strlen($return_message) == 0) break; + + echo $return_message; + } + } + $this->_stdOut = stream_get_contents($pipes[1]); $this->_stdErr = stream_get_contents($pipes[2]); fclose($pipes[1]);