123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- <?php
- namespace Symfony\Component\Process;
- use Symfony\Component\Process\Exception\InvalidArgumentException;
- use Symfony\Component\Process\Exception\LogicException;
- 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;
-
- public function __construct(array $arguments = array())
- {
- $this->arguments = $arguments;
- }
-
- public static function create(array $arguments = array())
- {
- return new static($arguments);
- }
-
- public function add($argument)
- {
- $this->arguments[] = $argument;
- return $this;
- }
-
- public function setPrefix($prefix)
- {
- $this->prefix = \is_array($prefix) ? $prefix : array($prefix);
- return $this;
- }
-
- public function setArguments(array $arguments)
- {
- $this->arguments = $arguments;
- return $this;
- }
-
- public function setWorkingDirectory($cwd)
- {
- $this->cwd = $cwd;
- return $this;
- }
-
- public function inheritEnvironmentVariables($inheritEnv = true)
- {
- $this->inheritEnv = $inheritEnv;
- return $this;
- }
-
- public function setEnv($name, $value)
- {
- $this->env[$name] = $value;
- return $this;
- }
-
- public function addEnvironmentVariables(array $variables)
- {
- $this->env = array_replace($this->env, $variables);
- return $this;
- }
-
- public function setInput($input)
- {
- $this->input = ProcessUtils::validateInput(__METHOD__, $input);
- return $this;
- }
-
- public function setTimeout($timeout)
- {
- if (null === $timeout) {
- $this->timeout = null;
- return $this;
- }
- $timeout = (float) $timeout;
- if ($timeout < 0) {
- throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
- }
- $this->timeout = $timeout;
- return $this;
- }
-
- public function setOption($name, $value)
- {
- $this->options[$name] = $value;
- return $this;
- }
-
- public function disableOutput()
- {
- $this->outputDisabled = true;
- return $this;
- }
-
- public function enableOutput()
- {
- $this->outputDisabled = false;
- return $this;
- }
-
- 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) {
-
- $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;
- }
- }
|