51 public static function exec(
string $executable, array $args = [],
string &$stderr =
''): string|false
53 $cmd = escapeshellarg($executable);
54 foreach ($args as $arg) {
55 $cmd .=
' ' . escapeshellarg((
string) $arg);
66 $process = @proc_open($cmd, $descriptorspec, $pipes,
null,
null, [
'bypass_shell' =>
true]);
68 if (!is_resource($process)) {
69 self::writeLog(
'CommandRunner::exec: failed to start process: ' . $cmd);
74 $output = stream_get_contents($pipes[1]);
75 $stderr = stream_get_contents($pipes[2]);
80 if (!empty($stderr)) {
126 public static function stream(
string $executable, array $args, callable $lineCallback): int|false
128 $cmd = escapeshellarg($executable);
129 foreach ($args as $arg) {
130 $cmd .=
' ' . escapeshellarg((
string) $arg);
135 $process = popen($cmd,
'rb');
137 self::writeLog(
'CommandRunner::stream: failed to start process: ' . $cmd);
142 while (!feof($process)) {
143 $buffer .= fread($process, 8192);
144 while (($pos = strpos($buffer,
"\r")) !==
false) {
145 $line = trim(substr($buffer, 0, $pos));
146 $buffer = substr($buffer, $pos + 1);
147 $lineCallback($line);
152 if (!empty($buffer)) {
153 $lineCallback(trim($buffer));
156 return pclose($process);