1 | <?php |
||
32 | class ProcessBuilder |
||
33 | { |
||
34 | /** |
||
35 | * The CLI executable to launch. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $binary; |
||
40 | |||
41 | /** |
||
42 | * The CLI arguments to pass. |
||
43 | * |
||
44 | * @var string[] |
||
45 | */ |
||
46 | private $arguments = []; |
||
47 | |||
48 | /** |
||
49 | * The working directory. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $workingDirectory; |
||
54 | |||
55 | /** |
||
56 | * The environment variables. |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | private $environment = []; |
||
61 | |||
62 | /** |
||
63 | * Flag determining if the current environment shall get inherited. |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | private $inheritEnvironment = true; |
||
68 | |||
69 | /** |
||
70 | * The input content. |
||
71 | * |
||
72 | * @var resource|scalar|\Traversable|null |
||
73 | */ |
||
74 | private $input; |
||
75 | |||
76 | /** |
||
77 | * The timeout value. |
||
78 | * |
||
79 | * @var int|null |
||
80 | */ |
||
81 | private $timeout = 60; |
||
82 | |||
83 | /** |
||
84 | * The proc_open options to use. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | private $options = []; |
||
89 | |||
90 | /** |
||
91 | * Flag determining if the output shall be disabled. |
||
92 | * |
||
93 | * @var bool |
||
94 | */ |
||
95 | private $outputDisabled = false; |
||
96 | |||
97 | /** |
||
98 | * Flag determining if task shall get spawned into background. |
||
99 | * |
||
100 | * @var bool |
||
101 | */ |
||
102 | private $forceBackground = false; |
||
103 | |||
104 | /** |
||
105 | * Create a new instance. |
||
106 | * |
||
107 | * @param string $binary The binary to launch. |
||
108 | * |
||
109 | * @param string[] $arguments An array of arguments. |
||
110 | * |
||
111 | * @return ProcessBuilder |
||
112 | */ |
||
113 | public static function create($binary, array $arguments = []) |
||
117 | |||
118 | /** |
||
119 | * Constructor. |
||
120 | * |
||
121 | * @param string $binary The binary to launch. |
||
122 | * |
||
123 | * @param string[] $arguments An array of arguments. |
||
124 | */ |
||
125 | public function __construct($binary, array $arguments = []) |
||
130 | |||
131 | /** |
||
132 | * Adds an unescaped argument to the command string. |
||
133 | * |
||
134 | * @param string $argument A command argument. |
||
135 | * |
||
136 | * @return ProcessBuilder |
||
137 | */ |
||
138 | public function addArgument($argument) |
||
144 | |||
145 | /** |
||
146 | * Adds unescaped arguments to the command string. |
||
147 | * |
||
148 | * @param string[] $arguments The command arguments. |
||
149 | * |
||
150 | * @return ProcessBuilder |
||
151 | */ |
||
152 | public function addArguments(array $arguments) |
||
160 | |||
161 | /** |
||
162 | * Sets the arguments of the process. |
||
163 | * |
||
164 | * Arguments must not be escaped. |
||
165 | * Previous arguments are removed. |
||
166 | * |
||
167 | * @param string[] $arguments The new arguments. |
||
168 | * |
||
169 | * @return ProcessBuilder |
||
170 | */ |
||
171 | public function setArguments(array $arguments) |
||
180 | |||
181 | /** |
||
182 | * Sets the working directory. |
||
183 | * |
||
184 | * @param null|string $workingDirectory The working directory. |
||
185 | * |
||
186 | * @return ProcessBuilder |
||
187 | * |
||
188 | * @throws InvalidArgumentException When the working directory is non null and does not exist. |
||
189 | */ |
||
190 | public function setWorkingDirectory($workingDirectory) |
||
200 | |||
201 | /** |
||
202 | * Sets whether environment variables will be inherited or not. |
||
203 | * |
||
204 | * @param bool $inheritEnvironment Flag if the environment shall get inherited or not (default true). |
||
205 | * |
||
206 | * @return ProcessBuilder |
||
207 | */ |
||
208 | public function inheritEnvironmentVariables($inheritEnvironment = true) |
||
214 | |||
215 | /** |
||
216 | * Sets an environment variable. |
||
217 | * |
||
218 | * Setting a variable overrides its previous value. Use `null` to unset a |
||
219 | * defined environment variable. |
||
220 | * |
||
221 | * @param string $name The variable name. |
||
222 | * |
||
223 | * @param null|string $value The variable value. |
||
224 | * |
||
225 | * @return ProcessBuilder |
||
226 | */ |
||
227 | public function setEnv($name, $value) |
||
233 | |||
234 | /** |
||
235 | * Adds a set of environment variables. |
||
236 | * |
||
237 | * Already existing environment variables with the same name will be |
||
238 | * overridden by the new values passed to this method. Pass `null` to unset |
||
239 | * a variable. |
||
240 | * |
||
241 | * @param array $variables The variables. |
||
242 | * |
||
243 | * @return ProcessBuilder |
||
244 | */ |
||
245 | public function addEnvironmentVariables(array $variables) |
||
251 | |||
252 | /** |
||
253 | * Sets the input of the process. |
||
254 | * |
||
255 | * @param resource|scalar|\Traversable|null $input The input content. |
||
256 | * |
||
257 | * @return ProcessBuilder |
||
258 | * |
||
259 | * @throws InvalidArgumentException In case the argument is invalid. |
||
260 | */ |
||
261 | public function setInput($input) |
||
267 | |||
268 | /** |
||
269 | * Sets the process timeout. |
||
270 | * |
||
271 | * To disable the timeout, set this value to null. |
||
272 | * |
||
273 | * @param float|null $timeout The new timeout value. |
||
274 | * |
||
275 | * @return ProcessBuilder |
||
276 | * |
||
277 | * @throws InvalidArgumentException When the timeout value is negative. |
||
278 | */ |
||
279 | public function setTimeout($timeout) |
||
297 | |||
298 | /** |
||
299 | * Adds a proc_open() option. |
||
300 | * |
||
301 | * @param string $name The option name. |
||
302 | * |
||
303 | * @param string $value The option value. |
||
304 | * |
||
305 | * @return ProcessBuilder |
||
306 | */ |
||
307 | public function setOption($name, $value) |
||
313 | |||
314 | /** |
||
315 | * Disables fetching output and error output from the underlying process. |
||
316 | * |
||
317 | * @return ProcessBuilder |
||
318 | */ |
||
319 | public function disableOutput() |
||
325 | |||
326 | /** |
||
327 | * Enables fetching output and error output from the underlying process. |
||
328 | * |
||
329 | * @return ProcessBuilder |
||
330 | */ |
||
331 | public function enableOutput() |
||
337 | |||
338 | /** |
||
339 | * Set if the process execution should be forced into the background. |
||
340 | * |
||
341 | * @param boolean $forceBackground The new value. |
||
342 | * |
||
343 | * @return ProcessBuilder |
||
344 | */ |
||
345 | public function setForceBackground($forceBackground = true) |
||
351 | |||
352 | /** |
||
353 | * Generate the process. |
||
354 | * |
||
355 | * @return Process |
||
356 | */ |
||
357 | public function generate() |
||
378 | |||
379 | /** |
||
380 | * Retrieve the passed environment variables from the current session and return them. |
||
381 | * |
||
382 | * @return array |
||
383 | * |
||
384 | * @SuppressWarnings(PHPMD.Superglobals) |
||
385 | * @SuppressWarnings(PHPMD.CamelCaseVariableName) |
||
386 | */ |
||
387 | private function getEnvironmentVariables() |
||
402 | |||
403 | /** |
||
404 | * Apply the force to background flag to a command line. |
||
405 | * |
||
406 | * @param string $commandLine The current command line to execute. |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | private function applyForceToBackground($commandLine) |
||
421 | } |
||
422 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: