Skip to content

Commit

Permalink
feat(shell): refactor Shell\execute to use async streams
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Nov 3, 2021
1 parent f2e38d5 commit ece71c1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
* refactored `Psl\IO` handles API.
* introduced a new `Psl\File` component.
* refactor `Psl\Filesystem\write_file`, `Psl\Filesystem\append_file`, and `Psl\Filesystem\read_file` to use `Psl\File` component.
* refactor `Psl\Shell\execute` to use `Psl\IO\Stream` component.
2 changes: 1 addition & 1 deletion docs/component/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

- [escape_argument](./../../src/Psl/Shell/escape_argument.php#L17)
- [escape_command](./../../src/Psl/Shell/escape_command.php#L14)
- [execute](./../../src/Psl/Shell/execute.php#L37)
- [execute](./../../src/Psl/Shell/execute.php#L36)


18 changes: 11 additions & 7 deletions src/Psl/Shell/execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

namespace Psl\Shell;

use Psl\Async;
use Psl\Dict;
use Psl\Env;
use Psl\IO\Stream;
use Psl\Str;
use Psl\Vec;

use function fclose;
use function is_dir;
use function is_resource;
use function proc_close;
use function proc_open;
use function stream_get_contents;

/**
* Execute an external program.
Expand Down Expand Up @@ -71,11 +70,16 @@ function execute(
}
// @codeCoverageIgnoreEnd

$stdout_content = stream_get_contents($pipes[1]);
$stderr_content = stream_get_contents($pipes[2]);
$stdout = new Stream\StreamCloseReadHandle($pipes[1]);
$stderr = new Stream\StreamCloseReadHandle($pipes[2]);

[$stdout_content, $stderr_content] = Async\concurrently([
static fn(): string => $stdout->readAll(),
static fn(): string => $stderr->readAll(),
])->await();

fclose($pipes[1]);
fclose($pipes[2]);
$stdout->close();
$stderr->close();

$code = proc_close($process);
if ($code !== 0) {
Expand Down

0 comments on commit ece71c1

Please sign in to comment.