Skip to content

Support the ability to output to console while the command getting executed #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -469,9 +475,13 @@ public function execute()
//
while (($out = fgets($pipes[1])) !== false) {
$this->_stdOut .= $out;
if($this->streamOutput)
echo $out;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that there's always a console (tty) connected. But this may not always be the case. Echoing out here also limits the way this feature can be used. It would be much more flexible if we would accept a stream here (e.g. STDOUT, STDERR, ...). This would be similar to what we do with stdIn. So a better way would probably be, to add setStdOutStream() and setStdErrStream() and then write to the streams here and below.

As this change is not so trivial and there's already a lot going on here, we need to test that nothing breaks.

I can maybe try to implement it that way - but can't promise yet if I find time soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

}
while (($err = fgets($pipes[2])) !== false) {
$this->_stdErr .= $err;
if($this->streamOutput)
echo $err;
}

$runTime = $hasTimeout ? time() - $startTime : 0;
Expand Down Expand Up @@ -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;
}
}
Copy link
Owner

@mikehaertl mikehaertl Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block would not work as expected: In blocking mode, this part is not reached until the command ist complete. If we want to introduce such a feature it only makes sense for nonBlockingMode.


$this->_stdOut = stream_get_contents($pipes[1]);
$this->_stdErr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
Expand Down