You are here

final class ProcessFactory in Automatic Updates 8.2

Defines a process factory which sets the COMPOSER_HOME environment variable.

@todo Figure out how to do this in composer_stager.

Hierarchy

  • class \Drupal\package_manager\ProcessFactory implements \PhpTuf\ComposerStager\Infrastructure\Process\ProcessFactoryInterface

Expanded class hierarchy of ProcessFactory

1 string reference to 'ProcessFactory'
package_manager.services.yml in package_manager/package_manager.services.yml
package_manager/package_manager.services.yml
1 service uses ProcessFactory
package_manager.process_factory in package_manager/package_manager.services.yml
Drupal\package_manager\ProcessFactory

File

package_manager/src/ProcessFactory.php, line 16

Namespace

Drupal\package_manager
View source
final class ProcessFactory implements ProcessFactoryInterface {

  /**
   * The decorated process factory.
   *
   * @var \PhpTuf\ComposerStager\Infrastructure\Process\ProcessFactoryInterface
   */
  private $decorated;

  /**
   * The file system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  private $fileSystem;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

  /**
   * Constructs a ProcessFactory object.
   *
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   */
  public function __construct(FileSystemInterface $file_system, ConfigFactoryInterface $config_factory) {
    $this->decorated = new StagerProcessFactory();
    $this->fileSystem = $file_system;
    $this->configFactory = $config_factory;
  }

  /**
   * 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 {
    $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);
  }

  /**
   * Returns the path to use as the COMPOSER_HOME environment variable.
   *
   * @return string
   *   The path which should be used as COMPOSER_HOME.
   */
  private function getComposerHomePath() : string {
    $home_path = $this->fileSystem
      ->getTempDirectory();
    $home_path .= '/automatic_updates_composer_home-';
    $home_path .= $this->configFactory
      ->get('system.site')
      ->get('uuid');
    $this->fileSystem
      ->prepareDirectory($home_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    return $home_path;
  }

  /**
   * Determines if a command is running Composer.
   *
   * @param string[] $command
   *   The command parts.
   *
   * @return bool
   *   TRUE if the command is running Composer, FALSE otherwise.
   */
  private function isComposerCommand(array $command) : bool {
    $executable = $command[0];
    $executable_parts = explode('/', $executable);
    $file = array_pop($executable_parts);
    return strpos($file, 'composer') === 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProcessFactory::$configFactory private property The config factory service.
ProcessFactory::$decorated private property The decorated process factory.
ProcessFactory::$fileSystem private property The file system service.
ProcessFactory::create public function
ProcessFactory::getComposerHomePath private function Returns the path to use as the COMPOSER_HOME environment variable.
ProcessFactory::getEnv private function Returns the value of an environment variable.
ProcessFactory::isComposerCommand private function Determines if a command is running Composer.
ProcessFactory::__construct public function Constructs a ProcessFactory object.