-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,9 +73,12 @@ public function checkRequiredBinariesExist(array $binaries = []): void { | |
* Executes a buffered command. | ||
* | ||
* @param array $cmd The command to execute. | ||
* @param null $callback A function to run while waiting for the process to complete. | ||
* @param callable|null $callback A function to run while waiting for the process to complete. | ||
* @param string|null $cwd | ||
* @param float|null $timeout | ||
* @param array|null $env | ||
*/ | ||
public function execute(array $cmd, callable $callback = NULL, string $cwd = NULL, ?bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL): Process { | ||
public function execute(array $cmd, callable $callback = NULL, string $cwd = NULL, bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL): Process { | ||
Check warning on line 81 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
|
||
$process = new Process($cmd); | ||
$process = $this->configureProcess($process, $cwd, $printOutput, $timeout, $env); | ||
return $this->executeProcess($process, $callback, $printOutput); | ||
|
@@ -89,13 +92,8 @@ public function execute(array $cmd, callable $callback = NULL, string $cwd = NUL | |
* pipes or redirects not supported by `execute()`. | ||
* | ||
* Windows does not support prepending commands with environment variables. | ||
* | ||
* @param callable|null $callback | ||
* @param string|null $cwd | ||
* @param int|null $timeout | ||
* @param array|null $env | ||
*/ | ||
public function executeFromCmd(string $cmd, callable $callback = NULL, string $cwd = NULL, ?bool $printOutput = TRUE, int $timeout = NULL, array $env = NULL): Process { | ||
public function executeFromCmd(string $cmd, callable $callback = NULL, string $cwd = NULL, bool $printOutput = TRUE, int $timeout = NULL, array $env = NULL): Process { | ||
Check warning on line 96 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
|
||
$process = Process::fromShellCommandline($cmd); | ||
$process = $this->configureProcess($process, $cwd, $printOutput, $timeout, $env); | ||
|
||
|
@@ -106,7 +104,7 @@ public function executeFromCmd(string $cmd, callable $callback = NULL, string $c | |
* @param string|null $cwd | ||
* @param array|null $env | ||
*/ | ||
private function configureProcess(Process $process, string $cwd = NULL, ?bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL): Process { | ||
private function configureProcess(Process $process, string $cwd = NULL, bool $printOutput = TRUE, float $timeout = NULL, array $env = NULL): Process { | ||
Check warning on line 107 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
|
||
if (function_exists('posix_isatty') && !@posix_isatty(STDIN)) { | ||
$process->setInput(STDIN); | ||
} | ||
|
@@ -124,19 +122,18 @@ private function configureProcess(Process $process, string $cwd = NULL, ?bool $p | |
return $process; | ||
} | ||
|
||
private function executeProcess(Process $process, callable $callback = NULL, ?bool $printOutput = TRUE): Process { | ||
if ($callback === NULL && $printOutput !== FALSE) { | ||
$callback = function (mixed $type, mixed $buffer): void { | ||
private function executeProcess(Process $process, callable $callback = NULL, bool $printOutput = TRUE): Process { | ||
Check warning on line 125 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
|
||
if ($callback === NULL && $printOutput) { | ||
Check warning on line 126 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
Check warning on line 126 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
|
||
$callback = function (string $type, iterable|string $buffer): void { | ||
$this->output->write($buffer); | ||
}; | ||
} | ||
$process->start(); | ||
set_error_handler(fn () => NULL); | ||
while ($process->isRunning()) { | ||
$process->checkTimeout(); | ||
usleep(1000); | ||
if ($process->getInput()) { | ||
$this->runAsync($process); | ||
} | ||
else { | ||
$process->run($callback); | ||
} | ||
restore_error_handler(); | ||
|
||
$this->logger->notice('Command: {command} [Exit: {exit}]', [ | ||
'command' => $process->getCommandLine(), | ||
|
@@ -146,6 +143,23 @@ private function executeProcess(Process $process, callable $callback = NULL, ?bo | |
return $process; | ||
} | ||
|
||
/** | ||
* Run the $process asynchronously as a workaround for https://github.com/symfony/symfony/issues/21580. | ||
*/ | ||
private function runAsync(Process $process): void { | ||
$process->start(); | ||
|
||
// Ignore "Write of <n> bytes failed with errno=32 Broken pipe" errors. | ||
set_error_handler(static fn () => NULL); | ||
Check warning on line 153 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
|
||
|
||
while ($process->isRunning()) { | ||
$process->checkTimeout(); | ||
Check warning on line 156 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
|
||
usleep(1000); | ||
Check warning on line 157 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
Check warning on line 157 in src/Helpers/LocalMachineHelper.php GitHub Actions / Mutation Testing
|
||
} | ||
|
||
restore_error_handler(); | ||
} | ||
|
||
/** | ||
* Returns a set-up filesystem object. | ||
*/ | ||
|