From 0eafa0f2737a6517905466b66238258f200698eb Mon Sep 17 00:00:00 2001 From: Bernhard Rusch Date: Thu, 4 Feb 2021 11:08:36 +0100 Subject: [PATCH 1/4] Symfony 5 compatibility: new Symfony\Component\Process\Process() only accepts an array --- src/Parallelization.php | 22 ++++++++++------------ src/ProcessLauncher.php | 7 +++++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Parallelization.php b/src/Parallelization.php index 65c90d1..96b4b95 100644 --- a/src/Parallelization.php +++ b/src/Parallelization.php @@ -359,19 +359,17 @@ function () use ($input) { sprintf('The bin/console file could not be found at %s', getcwd())) ; - $commandTemplate = implode( - ' ', - array_merge( - array_filter([ - self::detectPhpExecutable(), - $consolePath, - $this->getName(), - implode(' ', array_slice($input->getArguments(), 1)), - '--child', - ]), - $this->serializeInputOptions($input, ['child', 'processes']) - ) + $commandTemplate = array_merge( + array_filter([ + self::detectPhpExecutable(), + $consolePath, + $this->getName(), + implode(' ', array_slice($input->getArguments(), 1)), + '--child', + ]), + $this->serializeInputOptions($input, ['child', 'processes']) ); + $terminalWidth = (new Terminal())->getWidth(); $processLauncher = new ProcessLauncher( diff --git a/src/ProcessLauncher.php b/src/ProcessLauncher.php index 82eef3e..d00fce5 100644 --- a/src/ProcessLauncher.php +++ b/src/ProcessLauncher.php @@ -29,6 +29,9 @@ */ class ProcessLauncher { + /** + * @var array + */ private $command; private $workingDirectory; @@ -49,7 +52,7 @@ class ProcessLauncher private $runningProcesses = []; public function __construct( - string $command, + array $command, string $workingDirectory, array $environmentVariables, int $processLimit, @@ -140,7 +143,7 @@ private function startProcess(InputStream $inputStream): void $process->inheritEnvironmentVariables(true); $process->start($this->callback); - $this->logger->debug('Command started: '.$this->command); + $this->logger->debug('Command started: '. implode(' ', $this->command)); $this->runningProcesses[] = $process; } From a709c70f9a39fa14002c1111f4bc81771e94377a Mon Sep 17 00:00:00 2001 From: Bernhard Rusch Date: Tue, 2 Mar 2021 10:46:17 +0100 Subject: [PATCH 2/4] Update src/ProcessLauncher.php Co-authored-by: Christian Fasching --- src/ProcessLauncher.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ProcessLauncher.php b/src/ProcessLauncher.php index d00fce5..04e86ff 100644 --- a/src/ProcessLauncher.php +++ b/src/ProcessLauncher.php @@ -140,7 +140,9 @@ private function startProcess(InputStream $inputStream): void ); $process->setInput($inputStream); - $process->inheritEnvironmentVariables(true); + if(method_exists($process, 'inheritEnvironmentVariables')) { + $process->inheritEnvironmentVariables(true); + } $process->start($this->callback); $this->logger->debug('Command started: '. implode(' ', $this->command)); From ca4e719192a4a131d469847b6f88c19625c357ee Mon Sep 17 00:00:00 2001 From: Bernhard Rusch Date: Tue, 2 Mar 2021 10:48:27 +0100 Subject: [PATCH 3/4] Update Parallelization.php --- src/Parallelization.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Parallelization.php b/src/Parallelization.php index 96b4b95..961d653 100644 --- a/src/Parallelization.php +++ b/src/Parallelization.php @@ -515,13 +515,13 @@ private function serializeInputOptions(InputInterface $input, array $blackListPa $optionString = ''; if (!$option->acceptValue()) { - $optionString .= ' --'.$name; + $optionString .= '--'.$name; } elseif ($option->isArray()) { foreach ($value as $arrayValue) { - $optionString .= ' --'.$name.'='.$arrayValue; + $optionString .= '--'.$name.'='.$arrayValue; } } else { - $optionString .= ' --'.$name.'='.$value; + $optionString .= '--'.$name.'='.$value; } $preparedOptionList[] = $optionString; From a68774fb08f57199987af63f75c2c714e2b973e8 Mon Sep 17 00:00:00 2001 From: Bernhard Rusch Date: Mon, 8 Mar 2021 10:27:42 +0100 Subject: [PATCH 4/4] Do not change signature of ProcessLauncher::__construct() --- src/Parallelization.php | 7 ++++++- src/ProcessLauncher.php | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Parallelization.php b/src/Parallelization.php index 961d653..e973911 100644 --- a/src/Parallelization.php +++ b/src/Parallelization.php @@ -37,6 +37,7 @@ use Symfony\Component\DependencyInjection\ResettableContainerInterface; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Contracts\Service\ResetInterface; +use Symfony\Component\Process\Process; use Throwable; use function trim; use Webmozart\Assert\Assert; @@ -372,8 +373,12 @@ function () use ($input) { $terminalWidth = (new Terminal())->getWidth(); + // @TODO: can be removed once ProcessLauncher accepts command arrays + $tempProcess = new Process($commandTemplate); + $commandString = $tempProcess->getCommandLine(); + $processLauncher = new ProcessLauncher( - $commandTemplate, + $commandString, self::getWorkingDirectory($this->getContainer()), $this->getEnvironmentVariables($this->getContainer()), $numberOfProcesses, diff --git a/src/ProcessLauncher.php b/src/ProcessLauncher.php index 04e86ff..ddd22bc 100644 --- a/src/ProcessLauncher.php +++ b/src/ProcessLauncher.php @@ -29,9 +29,6 @@ */ class ProcessLauncher { - /** - * @var array - */ private $command; private $workingDirectory; @@ -52,7 +49,7 @@ class ProcessLauncher private $runningProcesses = []; public function __construct( - array $command, + string $command, // @TODO change to array for 2.0 string $workingDirectory, array $environmentVariables, int $processLimit, @@ -131,13 +128,21 @@ public function run(array $items): void */ private function startProcess(InputStream $inputStream): void { - $process = new Process( + $arguments = [ $this->command, $this->workingDirectory, $this->environmentVariables, null, null - ); + ]; + + if(method_exists(Process::class, 'fromShellCommandline')) { + // Symfony >= 4.2 workaround as Symfony 5 requires `Process` to be initiated with an array + // @TODO: can be removed once $this->command was changed to an array + $process = Process::fromShellCommandline(...$arguments); + } else { + $process = new Process(...$arguments); + } $process->setInput($inputStream); if(method_exists($process, 'inheritEnvironmentVariables')) { @@ -145,7 +150,7 @@ private function startProcess(InputStream $inputStream): void } $process->start($this->callback); - $this->logger->debug('Command started: '. implode(' ', $this->command)); + $this->logger->debug('Command started: '.$this->command); $this->runningProcesses[] = $process; }