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 resource|scalar|\Traversable|null $input The input content
174 * @throws InvalidArgumentException In case the argument is invalid
176 public function setInput($input)
178 $this->input = ProcessUtils::validateInput(__METHOD__, $input);
184 * Sets the process timeout.
186 * To disable the timeout, set this value to null.
188 * @param float|null $timeout
192 * @throws InvalidArgumentException
194 public function setTimeout($timeout)
196 if (null === $timeout) {
197 $this->timeout = null;
202 $timeout = (float) $timeout;
205 throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
208 $this->timeout = $timeout;
214 * Adds a proc_open option.
216 * @param string $name The option name
217 * @param string $value The option value
221 public function setOption($name, $value)
223 $this->options[$name] = $value;
229 * Disables fetching output and error output from the underlying process.
233 public function disableOutput()
235 $this->outputDisabled = true;
241 * Enables fetching output and error output from the underlying process.
245 public function enableOutput()
247 $this->outputDisabled = false;
253 * Creates a Process instance and returns it.
257 * @throws LogicException In case no arguments have been provided
259 public function getProcess()
261 if (0 === count($this->prefix) && 0 === count($this->arguments)) {
262 throw new LogicException('You must add() command arguments before calling getProcess().');
265 $options = $this->options;
267 $arguments = array_merge($this->prefix, $this->arguments);
268 $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
270 $process = new Process($script, $this->cwd, $this->env, $this->input, $this->timeout, $options);
272 if ($this->inheritEnv) {
273 $process->inheritEnvironmentVariables();
275 if ($this->outputDisabled) {
276 $process->disableOutput();