123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Process;
- use Symfony\Component\Process\Exception\InvalidArgumentException;
- use Symfony\Component\Process\Exception\LogicException;
- /**
- * Process builder.
- *
- * @author Kris Wallsmith <kris@symfony.com>
- */
- class ProcessBuilder
- {
- private $arguments;
- private $cwd;
- private $env = array();
- private $stdin;
- private $timeout = 60;
- private $options = array();
- private $inheritEnv = true;
- private $prefix;
- /**
- * 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 $prefix A command prefix
- *
- * @return ProcessBuilder
- */
- public function setPrefix($prefix)
- {
- $this->prefix = $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;
- }
- /**
- * Sets the input of the process.
- *
- * @param string|null $stdin The input as a string
- *
- * @return ProcessBuilder
- *
- * @throws InvalidArgumentException In case the argument is invalid
- */
- public function setInput($stdin)
- {
- $this->stdin = ProcessUtils::validateInput(__METHOD__, $stdin);
- 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 = (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;
- }
- /**
- * 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;
- }
- /**
- * Creates a Process instance and returns it.
- *
- * @return Process
- *
- * @throws LogicException In case no arguments have been provided
- */
- public function getProcess()
- {
- if (!$this->prefix && !count($this->arguments)) {
- throw new LogicException('You must add() command arguments before calling getProcess().');
- }
- $options = $this->options;
- $arguments = $this->prefix ? array_merge(array($this->prefix), $this->arguments) : $this->arguments;
- $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
- if ($this->inheritEnv) {
- $env = $this->env ? $this->env + $_ENV : null;
- } else {
- $env = $this->env;
- }
- return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
- }
- }
|