88namespace Tester \Runner ;
99
1010use Tester \Helpers ;
11- use function count , in_array ;
11+ use function array_map , count , explode , implode , in_array , str_contains ;
1212
1313
1414/**
1515 * Wraps a PHP executable and its resolved version, extensions, and command-line options.
1616 */
1717class PhpInterpreter
1818{
19- private string $ commandLine ;
19+ /** @var list<string> */
20+ private array $ commandLine ;
2021 private bool $ cgi ;
2122 private \stdClass $ info ;
2223 private string $ error ;
@@ -25,14 +26,10 @@ class PhpInterpreter
2526 /** @param list<string> $args */
2627 public function __construct (string $ path , array $ args = [])
2728 {
28- $ this ->commandLine = Helpers::escapeArg ($ path );
2929 $ proc = @proc_open ( // @ is escalated to exception
30- $ this -> commandLine . ' --version ' ,
30+ [ $ path , ' --version '] ,
3131 [['pipe ' , 'r ' ], ['pipe ' , 'w ' ], ['pipe ' , 'w ' ]],
3232 $ pipes ,
33- null ,
34- null ,
35- ['bypass_shell ' => true ],
3633 );
3734 if ($ proc === false ) {
3835 throw new \Exception ("Cannot run PHP interpreter $ path. Use -p option. " );
@@ -42,20 +39,16 @@ public function __construct(string $path, array $args = [])
4239 $ output = stream_get_contents ($ pipes [1 ]);
4340 proc_close ($ proc );
4441
45- $ args = ' ' . implode (' ' , array_map ([Helpers::class, 'escapeArg ' ], $ args ));
4642 if (str_contains ($ output , 'phpdbg ' )) {
47- $ args = ' -qrrb -S cli ' . $ args ;
43+ $ args = [ ' -qrrb ' , ' -S ' , ' cli ', ... $ args] ;
4844 }
4945
50- $ this ->commandLine .= rtrim ( $ args) ;
46+ $ this ->commandLine = [ $ path , ... $ args] ;
5147
5248 $ proc = proc_open (
53- $ this ->commandLine . ' -d register_argc_argv=on ' . Helpers:: escapeArg ( __DIR__ . '/info.php ' ) . ' serialized ' ,
49+ [... $ this ->commandLine , ' -d ' , ' register_argc_argv=on ' , __DIR__ . '/info.php ' , ' serialized '] ,
5450 [['pipe ' , 'r ' ], ['pipe ' , 'w ' ], ['pipe ' , 'w ' ]],
5551 $ pipes ,
56- null ,
57- null ,
58- ['bypass_shell ' => true ],
5952 ) ?: throw new \Exception ("Unable to run $ path. " );
6053
6154 $ output = stream_get_contents ($ pipes [1 ]);
@@ -88,7 +81,7 @@ public function __construct(string $path, array $args = [])
8881 public function withArguments (array $ args ): static
8982 {
9083 $ me = clone $ this ;
91- $ me ->commandLine .= ' ' . implode ( ' ' , array_map ([Helpers::class, ' escapeArg ' ], $ args)) ;
84+ $ me ->commandLine = [... $ me -> commandLine , ... $ args] ;
9285 return $ me ;
9386 }
9487
@@ -98,11 +91,18 @@ public function withArguments(array $args): static
9891 */
9992 public function withPhpIniOption (string $ name , ?string $ value = null ): static
10093 {
101- return $ this ->withArguments (['-d ' . $ name . ($ value === null ? '' : "= $ value " )]);
94+ return $ this ->withArguments (['-d ' , $ name . ($ value === null ? '' : "= $ value " )]);
10295 }
10396
10497
10598 public function getCommandLine (): string
99+ {
100+ return implode (' ' , array_map ([Helpers::class, 'escapeArg ' ], $ this ->commandLine ));
101+ }
102+
103+
104+ /** @return list<string> */
105+ public function getCommand (): array
106106 {
107107 return $ this ->commandLine ;
108108 }
0 commit comments