4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Process;
14 use Symfony\Component\Process\Exception\InvalidArgumentException;
15 use Symfony\Component\Process\Exception\LogicException;
20 * @author Kris Wallsmith <kris@symfony.com>
26 private $env = array();
28 private $timeout = 60;
29 private $options = array();
30 private $inheritEnv = true;
31 private $prefix = array();
32 private $outputDisabled = false;
37 * @param string[] $arguments An array of arguments
39 public function __construct(array $arguments = array())
41 $this->arguments = $arguments;
45 * Creates a process builder instance.
47 * @param string[] $arguments An array of arguments
51 public static function create(array $arguments = array())
53 return new static($arguments);
57 * Adds an unescaped argument to the command string.
59 * @param string $argument A command argument
63 public function add($argument)
65 $this->arguments[] = $argument;
71 * Adds a prefix to the command string.
73 * The prefix is preserved when resetting arguments.
75 * @param string|array $prefix A command prefix or an array of command prefixes
79 public function setPrefix($prefix)
81 $this->prefix = is_array($prefix) ? $prefix : array($prefix);
87 * Sets the arguments of the process.
89 * Arguments must not be escaped.
90 * Previous arguments are removed.
92 * @param string[] $arguments
96 public function setArguments(array $arguments)
98 $this->arguments = $arguments;
104 * Sets the working directory.
106 * @param null|string $cwd The working directory
110 public function setWorkingDirectory($cwd)
118 * Sets whether environment variables will be inherited or not.
120 * @param bool $inheritEnv
124 public function inheritEnvironmentVariables($inheritEnv = true)
126 $this->inheritEnv = $inheritEnv;
132 * Sets an environment variable.
134 * Setting a variable overrides its previous value. Use `null` to unset a
135 * defined environment variable.
137 * @param string $name The variable name
138 * @param null|string $value The variable value
142 public function setEnv($name, $value)
144 $this->env[$name] = $value;
150 * Adds a set of environment variables.
152 * Already existing environment variables with the same name will be
153 * overridden by the new values passed to this method. Pass `null` to unset
156 * @param array $variables The variables
160 public function addEnvironmentVariables(array $variables)
162 $this->env = array_replace($this->env, $variables);
168 * Sets the input of the process.
170 * @param mixed $input The input as a string
174 * @throws InvalidArgumentException In case the argument is invalid
176 * Passing an object as an input is deprecated since version 2.5 and will be removed in 3.0.
178 public function setInput($input)
180 $this->input = ProcessUtils::validateInput(__METHOD__, $input);
186 * Sets the process timeout.
188 * To disable the timeout, set this value to null.
190 * @param float|null $timeout
194 * @throws InvalidArgumentException
196 public function setTimeout($timeout)
198 if (null === $timeout) {
199 $this->timeout = null;
204 $timeout = (float) $timeout;
207 throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
210 $this->timeout = $timeout;
216 * Adds a proc_open option.
218 * @param string $name The option name
219 * @param string $value The option value
223 public function setOption($name, $value)
225 $this->options[$name] = $value;
231 * Disables fetching output and error output from the underlying process.
235 public function disableOutput()
237 $this->outputDisabled = true;
243 * Enables fetching output and error output from the underlying process.
247 public function enableOutput()
249 $this->outputDisabled = false;
255 * Creates a Process instance and returns it.
259 * @throws LogicException In case no arguments have been provided
261 public function getProcess()
263 if (0 === count($this->prefix) && 0 === count($this->arguments)) {
264 throw new LogicException('You must add() command arguments before calling getProcess().');
267 $options = $this->options;
269 $arguments = array_merge($this->prefix, $this->arguments);
270 $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
272 if ($this->inheritEnv) {
273 // include $_ENV for BC purposes
274 $env = array_replace($_ENV, $_SERVER, $this->env);
279 $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
281 if ($this->outputDisabled) {
282 $process->disableOutput();