11<?php
22
3- class ProcessResult {
4- public $ stdout ;
5- public $ stderr ;
3+ readonly class ProcessResult {
4+ public function __construct (
5+ public string $ stdout ,
6+ public string $ stderr ,
7+ ) {}
68}
79
810function runCommand (array $ args , ?string $ cwd = null , bool $ printCommand = true ): ProcessResult {
911 $ cmd = implode (' ' , array_map ('escapeshellarg ' , $ args ));
1012 $ pipes = null ;
11- $ result = new ProcessResult ();
1213 $ descriptorSpec = [0 => ['pipe ' , 'r ' ], 1 => ['pipe ' , 'w ' ], 2 => ['pipe ' , 'w ' ]];
1314 if ($ printCommand ) {
1415 fwrite (STDOUT , "> $ cmd \n" );
@@ -24,6 +25,8 @@ function runCommand(array $args, ?string $cwd = null, bool $printCommand = true)
2425 stream_set_blocking ($ stdout , false );
2526 stream_set_blocking ($ stderr , false );
2627
28+ $ stdoutStr = '' ;
29+ $ stderrStr = '' ;
2730 $ stdoutEof = false ;
2831 $ stderrEof = false ;
2932
@@ -37,9 +40,9 @@ function runCommand(array $args, ?string $cwd = null, bool $printCommand = true)
3740 foreach ($ read as $ stream ) {
3841 $ chunk = fgets ($ stream );
3942 if ($ stream === $ stdout ) {
40- $ result -> stdout .= $ chunk ;
43+ $ stdoutStr .= $ chunk ;
4144 } elseif ($ stream === $ stderr ) {
42- $ result -> stderr .= $ chunk ;
45+ $ stderrStr .= $ chunk ;
4346 }
4447 }
4548
@@ -50,6 +53,8 @@ function runCommand(array $args, ?string $cwd = null, bool $printCommand = true)
5053 fclose ($ stdout );
5154 fclose ($ stderr );
5255
56+ $ result = new ProcessResult ($ stdoutStr , $ stderrStr );
57+
5358 $ statusCode = proc_close ($ processHandle );
5459 if ($ statusCode !== 0 ) {
5560 fwrite (STDOUT , $ result ->stdout );
0 commit comments