Yaffs site version 1.1
[yaffs-website] / vendor / symfony / process / ProcessBuilder.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Process;
13
14 use Symfony\Component\Process\Exception\InvalidArgumentException;
15 use Symfony\Component\Process\Exception\LogicException;
16
17 /**
18  * Process builder.
19  *
20  * @author Kris Wallsmith <kris@symfony.com>
21  */
22 class ProcessBuilder
23 {
24     private $arguments;
25     private $cwd;
26     private $env = array();
27     private $input;
28     private $timeout = 60;
29     private $options = array();
30     private $inheritEnv = true;
31     private $prefix = array();
32     private $outputDisabled = false;
33
34     /**
35      * Constructor.
36      *
37      * @param string[] $arguments An array of arguments
38      */
39     public function __construct(array $arguments = array())
40     {
41         $this->arguments = $arguments;
42     }
43
44     /**
45      * Creates a process builder instance.
46      *
47      * @param string[] $arguments An array of arguments
48      *
49      * @return static
50      */
51     public static function create(array $arguments = array())
52     {
53         return new static($arguments);
54     }
55
56     /**
57      * Adds an unescaped argument to the command string.
58      *
59      * @param string $argument A command argument
60      *
61      * @return $this
62      */
63     public function add($argument)
64     {
65         $this->arguments[] = $argument;
66
67         return $this;
68     }
69
70     /**
71      * Adds a prefix to the command string.
72      *
73      * The prefix is preserved when resetting arguments.
74      *
75      * @param string|array $prefix A command prefix or an array of command prefixes
76      *
77      * @return $this
78      */
79     public function setPrefix($prefix)
80     {
81         $this->prefix = is_array($prefix) ? $prefix : array($prefix);
82
83         return $this;
84     }
85
86     /**
87      * Sets the arguments of the process.
88      *
89      * Arguments must not be escaped.
90      * Previous arguments are removed.
91      *
92      * @param string[] $arguments
93      *
94      * @return $this
95      */
96     public function setArguments(array $arguments)
97     {
98         $this->arguments = $arguments;
99
100         return $this;
101     }
102
103     /**
104      * Sets the working directory.
105      *
106      * @param null|string $cwd The working directory
107      *
108      * @return $this
109      */
110     public function setWorkingDirectory($cwd)
111     {
112         $this->cwd = $cwd;
113
114         return $this;
115     }
116
117     /**
118      * Sets whether environment variables will be inherited or not.
119      *
120      * @param bool $inheritEnv
121      *
122      * @return $this
123      */
124     public function inheritEnvironmentVariables($inheritEnv = true)
125     {
126         $this->inheritEnv = $inheritEnv;
127
128         return $this;
129     }
130
131     /**
132      * Sets an environment variable.
133      *
134      * Setting a variable overrides its previous value. Use `null` to unset a
135      * defined environment variable.
136      *
137      * @param string      $name  The variable name
138      * @param null|string $value The variable value
139      *
140      * @return $this
141      */
142     public function setEnv($name, $value)
143     {
144         $this->env[$name] = $value;
145
146         return $this;
147     }
148
149     /**
150      * Adds a set of environment variables.
151      *
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
154      * a variable.
155      *
156      * @param array $variables The variables
157      *
158      * @return $this
159      */
160     public function addEnvironmentVariables(array $variables)
161     {
162         $this->env = array_replace($this->env, $variables);
163
164         return $this;
165     }
166
167     /**
168      * Sets the input of the process.
169      *
170      * @param mixed $input The input as a string
171      *
172      * @return $this
173      *
174      * @throws InvalidArgumentException In case the argument is invalid
175      *
176      * Passing an object as an input is deprecated since version 2.5 and will be removed in 3.0.
177      */
178     public function setInput($input)
179     {
180         $this->input = ProcessUtils::validateInput(__METHOD__, $input);
181
182         return $this;
183     }
184
185     /**
186      * Sets the process timeout.
187      *
188      * To disable the timeout, set this value to null.
189      *
190      * @param float|null $timeout
191      *
192      * @return $this
193      *
194      * @throws InvalidArgumentException
195      */
196     public function setTimeout($timeout)
197     {
198         if (null === $timeout) {
199             $this->timeout = null;
200
201             return $this;
202         }
203
204         $timeout = (float) $timeout;
205
206         if ($timeout < 0) {
207             throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
208         }
209
210         $this->timeout = $timeout;
211
212         return $this;
213     }
214
215     /**
216      * Adds a proc_open option.
217      *
218      * @param string $name  The option name
219      * @param string $value The option value
220      *
221      * @return $this
222      */
223     public function setOption($name, $value)
224     {
225         $this->options[$name] = $value;
226
227         return $this;
228     }
229
230     /**
231      * Disables fetching output and error output from the underlying process.
232      *
233      * @return $this
234      */
235     public function disableOutput()
236     {
237         $this->outputDisabled = true;
238
239         return $this;
240     }
241
242     /**
243      * Enables fetching output and error output from the underlying process.
244      *
245      * @return $this
246      */
247     public function enableOutput()
248     {
249         $this->outputDisabled = false;
250
251         return $this;
252     }
253
254     /**
255      * Creates a Process instance and returns it.
256      *
257      * @return Process
258      *
259      * @throws LogicException In case no arguments have been provided
260      */
261     public function getProcess()
262     {
263         if (0 === count($this->prefix) && 0 === count($this->arguments)) {
264             throw new LogicException('You must add() command arguments before calling getProcess().');
265         }
266
267         $options = $this->options;
268
269         $arguments = array_merge($this->prefix, $this->arguments);
270         $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));
271
272         if ($this->inheritEnv) {
273             // include $_ENV for BC purposes
274             $env = array_replace($_ENV, $_SERVER, $this->env);
275         } else {
276             $env = $this->env;
277         }
278
279         $process = new Process($script, $this->cwd, $env, $this->input, $this->timeout, $options);
280
281         if ($this->outputDisabled) {
282             $process->disableOutput();
283         }
284
285         return $process;
286     }
287 }