You are here

class ProcessBuilder in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/process/ProcessBuilder.php \Symfony\Component\Process\ProcessBuilder

Process builder.

@author Kris Wallsmith <kris@symfony.com>

Hierarchy

Expanded class hierarchy of ProcessBuilder

3 files declare their use of ProcessBuilder
ProcessBuilderTest.php in vendor/symfony/process/Tests/ProcessBuilderTest.php
ProcessHelper.php in vendor/symfony/console/Helper/ProcessHelper.php
Shell.php in vendor/symfony/console/Shell.php

File

vendor/symfony/process/ProcessBuilder.php, line 22

Namespace

Symfony\Component\Process
View source
class ProcessBuilder {
  private $arguments;
  private $cwd;
  private $env = array();
  private $input;
  private $timeout = 60;
  private $options = array();
  private $inheritEnv = true;
  private $prefix = array();
  private $outputDisabled = false;

  /**
   * Constructor.
   *
   * @param string[] $arguments An array of arguments
   */
  public function __construct(array $arguments = array()) {
    $this->arguments = $arguments;
  }

  /**
   * Creates a process builder instance.
   *
   * @param string[] $arguments An array of arguments
   *
   * @return ProcessBuilder
   */
  public static function create(array $arguments = array()) {
    return new static($arguments);
  }

  /**
   * Adds an unescaped argument to the command string.
   *
   * @param string $argument A command argument
   *
   * @return ProcessBuilder
   */
  public function add($argument) {
    $this->arguments[] = $argument;
    return $this;
  }

  /**
   * Adds a prefix to the command string.
   *
   * The prefix is preserved when resetting arguments.
   *
   * @param string|array $prefix A command prefix or an array of command prefixes
   *
   * @return ProcessBuilder
   */
  public function setPrefix($prefix) {
    $this->prefix = is_array($prefix) ? $prefix : array(
      $prefix,
    );
    return $this;
  }

  /**
   * Sets the arguments of the process.
   *
   * Arguments must not be escaped.
   * Previous arguments are removed.
   *
   * @param string[] $arguments
   *
   * @return ProcessBuilder
   */
  public function setArguments(array $arguments) {
    $this->arguments = $arguments;
    return $this;
  }

  /**
   * Sets the working directory.
   *
   * @param null|string $cwd The working directory
   *
   * @return ProcessBuilder
   */
  public function setWorkingDirectory($cwd) {
    $this->cwd = $cwd;
    return $this;
  }

  /**
   * Sets whether environment variables will be inherited or not.
   *
   * @param bool $inheritEnv
   *
   * @return ProcessBuilder
   */
  public function inheritEnvironmentVariables($inheritEnv = true) {
    $this->inheritEnv = $inheritEnv;
    return $this;
  }

  /**
   * Sets an environment variable.
   *
   * Setting a variable overrides its previous value. Use `null` to unset a
   * defined environment variable.
   *
   * @param string      $name  The variable name
   * @param null|string $value The variable value
   *
   * @return ProcessBuilder
   */
  public function setEnv($name, $value) {
    $this->env[$name] = $value;
    return $this;
  }

  /**
   * Adds a set of environment variables.
   *
   * Already existing environment variables with the same name will be
   * overridden by the new values passed to this method. Pass `null` to unset
   * a variable.
   *
   * @param array $variables The variables
   *
   * @return ProcessBuilder
   */
  public function addEnvironmentVariables(array $variables) {
    $this->env = array_replace($this->env, $variables);
    return $this;
  }

  /**
   * Sets the input of the process.
   *
   * @param mixed $input The input as a string
   *
   * @return ProcessBuilder
   *
   * @throws InvalidArgumentException In case the argument is invalid
   *
   * Passing an object as an input is deprecated since version 2.5 and will be removed in 3.0.
   */
  public function setInput($input) {
    $this->input = ProcessUtils::validateInput(sprintf('%s::%s', __CLASS__, __FUNCTION__), $input);
    return $this;
  }

  /**
   * Sets the process timeout.
   *
   * To disable the timeout, set this value to null.
   *
   * @param float|null $timeout
   *
   * @return ProcessBuilder
   *
   * @throws InvalidArgumentException
   */
  public function setTimeout($timeout) {
    if (null === $timeout) {
      $this->timeout = null;
      return $this;
    }
    $timeout = (double) $timeout;
    if ($timeout < 0) {
      throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
    }
    $this->timeout = $timeout;
    return $this;
  }

  /**
   * Adds a proc_open option.
   *
   * @param string $name  The option name
   * @param string $value The option value
   *
   * @return ProcessBuilder
   */
  public function setOption($name, $value) {
    $this->options[$name] = $value;
    return $this;
  }

  /**
   * Disables fetching output and error output from the underlying process.
   *
   * @return ProcessBuilder
   */
  public function disableOutput() {
    $this->outputDisabled = true;
    return $this;
  }

  /**
   * Enables fetching output and error output from the underlying process.
   *
   * @return ProcessBuilder
   */
  public function enableOutput() {
    $this->outputDisabled = false;
    return $this;
  }

  /**
   * Creates a Process instance and returns it.
   *
   * @return Process
   *
   * @throws LogicException In case no arguments have been provided
   */
  public function getProcess() {
    if (0 === count($this->prefix) && 0 === count($this->arguments)) {
      throw new LogicException('You must add() command arguments before calling getProcess().');
    }
    $options = $this->options;
    $arguments = array_merge($this->prefix, $this->arguments);
    $script = implode(' ', array_map(array(
      __NAMESPACE__ . '\\ProcessUtils',
      'escapeArgument',
    ), $arguments));
    if ($this->inheritEnv) {

      // include $_ENV for BC purposes
      $env = array_replace($_ENV, $_SERVER, $this->env);
    }
    else {
      $env = $this->env;
    }
    $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
    if ($this->outputDisabled) {
      $process
        ->disableOutput();
    }
    return $process;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProcessBuilder::$arguments private property
ProcessBuilder::$cwd private property
ProcessBuilder::$env private property
ProcessBuilder::$inheritEnv private property
ProcessBuilder::$input private property
ProcessBuilder::$options private property
ProcessBuilder::$outputDisabled private property
ProcessBuilder::$prefix private property
ProcessBuilder::$timeout private property
ProcessBuilder::add public function Adds an unescaped argument to the command string.
ProcessBuilder::addEnvironmentVariables public function Adds a set of environment variables.
ProcessBuilder::create public static function Creates a process builder instance.
ProcessBuilder::disableOutput public function Disables fetching output and error output from the underlying process.
ProcessBuilder::enableOutput public function Enables fetching output and error output from the underlying process.
ProcessBuilder::getProcess public function Creates a Process instance and returns it.
ProcessBuilder::inheritEnvironmentVariables public function Sets whether environment variables will be inherited or not.
ProcessBuilder::setArguments public function Sets the arguments of the process.
ProcessBuilder::setEnv public function Sets an environment variable.
ProcessBuilder::setInput public function Sets the input of the process.
ProcessBuilder::setOption public function Adds a proc_open option.
ProcessBuilder::setPrefix public function Adds a prefix to the command string.
ProcessBuilder::setTimeout public function Sets the process timeout.
ProcessBuilder::setWorkingDirectory public function Sets the working directory.
ProcessBuilder::__construct public function Constructor.