From 70ef22707ef628ed266b820f392630b2f74c4b36 Mon Sep 17 00:00:00 2001 From: phenaproxima <phenaproxima@205645.no-reply.drupal.org> Date: Tue, 17 Aug 2021 13:59:29 +0000 Subject: [PATCH] Issue #3228521 by phenaproxima: Clean up ProcessFactory --- src/ComposerStager/ProcessFactory.php | 57 ++++++++++++++++++--------- src/Updater.php | 33 ---------------- 2 files changed, 39 insertions(+), 51 deletions(-) diff --git a/src/ComposerStager/ProcessFactory.php b/src/ComposerStager/ProcessFactory.php index fec8b8d48a..b190f57087 100644 --- a/src/ComposerStager/ProcessFactory.php +++ b/src/ComposerStager/ProcessFactory.php @@ -2,9 +2,8 @@ namespace Drupal\automatic_updates\ComposerStager; -use PhpTuf\ComposerStager\Exception\LogicException; +use PhpTuf\ComposerStager\Infrastructure\Process\ProcessFactory as StagerProcessFactory; use PhpTuf\ComposerStager\Infrastructure\Process\ProcessFactoryInterface; -use Symfony\Component\Process\Exception\ExceptionInterface; use Symfony\Component\Process\Process; /** @@ -14,27 +13,49 @@ use Symfony\Component\Process\Process; */ final class ProcessFactory implements ProcessFactoryInterface { + /** + * The decorated process factory. + * + * @var \PhpTuf\ComposerStager\Infrastructure\Process\ProcessFactoryInterface + */ + private $decorated; + + /** + * Constructs a ProcessFactory object. + */ + public function __construct() { + $this->decorated = new StagerProcessFactory(); + } + + /** + * Returns the value of an environment variable. + * + * @param string $variable + * The name of the variable. + * + * @return mixed + * The value of the variable. + */ + private function getEnv(string $variable) { + if (function_exists('apache_getenv')) { + return apache_getenv($variable); + } + return getenv($variable); + } + /** * {@inheritdoc} */ public function create(array $command): Process { - try { - if ($this->isComposerCommand($command)) { - $process = new Process($command, NULL, ['COMPOSER_HOME' => $this->getComposerHomePath()]); - $path = function_exists('apache_getenv') ? apache_getenv('PATH') : getenv('PATH'); - $path .= ':' . dirname(PHP_BINARY); - $env = $process->getEnv(); - $env['PATH'] = $path; - $process->setEnv($env); - return $process; - } - return new Process($command); - // @codeCoverageIgnore - } - catch (ExceptionInterface $e) { - // @codeCoverageIgnore - throw new LogicException($e->getMessage(), (int) $e->getCode(), $e); + $process = $this->decorated->create($command); + + $env = $process->getEnv(); + if ($this->isComposerCommand($command)) { + $env['COMPOSER_HOME'] = $this->getComposerHomePath(); } + // Ensure that the running PHP binary is in the PATH. + $env['PATH'] = $this->getEnv('PATH') . ':' . dirname(PHP_BINARY); + return $process->setEnv($env); } /** diff --git a/src/Updater.php b/src/Updater.php index c19b10ccb6..6e4b9f55c2 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -262,39 +262,6 @@ class Updater { $this->stager->stage($command, $this->getStageDirectory()); } - /** - * Returns the value of an environment variable. - * - * @param string $variable - * The name of the variable. - * - * @return mixed - * The value of the variable. - */ - private function getEnv(string $variable) { - if (function_exists('apache_getenv')) { - return apache_getenv($variable); - } - return getenv($variable); - } - - /** - * Sets the value of an environment variable. - * - * @param string $variable - * The name of the variable. - * @param mixed $value - * The value to set. - */ - private function setEnv(string $variable, $value): void { - if (function_exists('apache_setenv')) { - apache_setenv($variable, $value); - } - else { - putenv("$variable=$value"); - } - } - /** * Initializes an active update and returns its ID. * -- GitLab