|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file was taken from the "symfony/process" for backward compatibility. |
|
4
|
|
|
* https://github.com/symfony/process/tree/3.4 |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alchemy <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Alchemy\Zippy\ProcessBuilder; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\Process\Exception\InvalidArgumentException; |
|
14
|
|
|
use Symfony\Component\Process\Exception\LogicException; |
|
15
|
|
|
use Symfony\Component\Process\Process; |
|
16
|
|
|
use Symfony\Component\Process\ProcessUtils; |
|
17
|
|
|
|
|
18
|
|
|
class ProcessBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
private $arguments; |
|
21
|
|
|
private $cwd; |
|
22
|
|
|
private $env = array(); |
|
23
|
|
|
private $input; |
|
24
|
|
|
private $timeout = 60; |
|
25
|
|
|
private $options; |
|
26
|
|
|
private $inheritEnv = true; |
|
27
|
|
|
private $prefix = array(); |
|
28
|
|
|
private $outputDisabled = false; |
|
29
|
|
|
/** |
|
30
|
|
|
* @param string[] $arguments An array of arguments |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(array $arguments = array()) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->arguments = $arguments; |
|
35
|
|
|
} |
|
36
|
|
|
/** |
|
37
|
|
|
* Creates a process builder instance. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string[] $arguments An array of arguments |
|
40
|
|
|
* |
|
41
|
|
|
* @return static |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function create(array $arguments = array()) |
|
44
|
|
|
{ |
|
45
|
|
|
return new static($arguments); |
|
46
|
|
|
} |
|
47
|
|
|
/** |
|
48
|
|
|
* Adds an unescaped argument to the command string. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $argument A command argument |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
|
|
public function add($argument) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->arguments[] = $argument; |
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* Adds a prefix to the command string. |
|
61
|
|
|
* |
|
62
|
|
|
* The prefix is preserved when resetting arguments. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string|array $prefix A command prefix or an array of command prefixes |
|
65
|
|
|
* |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setPrefix($prefix) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->prefix = \is_array($prefix) ? $prefix : array($prefix); |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
/** |
|
74
|
|
|
* Sets the arguments of the process. |
|
75
|
|
|
* |
|
76
|
|
|
* Arguments must not be escaped. |
|
77
|
|
|
* Previous arguments are removed. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string[] $arguments |
|
80
|
|
|
* |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setArguments(array $arguments) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->arguments = $arguments; |
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
/** |
|
89
|
|
|
* Sets the working directory. |
|
90
|
|
|
* |
|
91
|
|
|
* @param null|string $cwd The working directory |
|
92
|
|
|
* |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setWorkingDirectory($cwd) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->cwd = $cwd; |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
/** |
|
101
|
|
|
* Sets whether environment variables will be inherited or not. |
|
102
|
|
|
* |
|
103
|
|
|
* @param bool $inheritEnv |
|
104
|
|
|
* |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
public function inheritEnvironmentVariables($inheritEnv = true) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->inheritEnv = $inheritEnv; |
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
/** |
|
113
|
|
|
* Sets an environment variable. |
|
114
|
|
|
* |
|
115
|
|
|
* Setting a variable overrides its previous value. Use `null` to unset a |
|
116
|
|
|
* defined environment variable. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $name The variable name |
|
119
|
|
|
* @param null|string $value The variable value |
|
120
|
|
|
* |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setEnv($name, $value) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->env[$name] = $value; |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
/** |
|
129
|
|
|
* Adds a set of environment variables. |
|
130
|
|
|
* |
|
131
|
|
|
* Already existing environment variables with the same name will be |
|
132
|
|
|
* overridden by the new values passed to this method. Pass `null` to unset |
|
133
|
|
|
* a variable. |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $variables The variables |
|
136
|
|
|
* |
|
137
|
|
|
* @return $this |
|
138
|
|
|
*/ |
|
139
|
|
|
public function addEnvironmentVariables(array $variables) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->env = array_replace($this->env, $variables); |
|
142
|
|
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
/** |
|
145
|
|
|
* Sets the input of the process. |
|
146
|
|
|
* |
|
147
|
|
|
* @param resource|string|int|float|bool|\Traversable|null $input The input content |
|
148
|
|
|
* |
|
149
|
|
|
* @return $this |
|
150
|
|
|
* |
|
151
|
|
|
* @throws InvalidArgumentException In case the argument is invalid |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setInput($input) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->input = ProcessUtils::validateInput(__METHOD__, $input); |
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
/** |
|
159
|
|
|
* Sets the process timeout. |
|
160
|
|
|
* |
|
161
|
|
|
* To disable the timeout, set this value to null. |
|
162
|
|
|
* |
|
163
|
|
|
* @param float|null $timeout |
|
164
|
|
|
* |
|
165
|
|
|
* @return $this |
|
166
|
|
|
* |
|
167
|
|
|
* @throws InvalidArgumentException |
|
168
|
|
|
*/ |
|
169
|
|
|
public function setTimeout($timeout) |
|
170
|
|
|
{ |
|
171
|
|
|
if (null === $timeout) { |
|
172
|
|
|
$this->timeout = null; |
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
$timeout = (float) $timeout; |
|
176
|
|
|
if ($timeout < 0) { |
|
177
|
|
|
throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.'); |
|
178
|
|
|
} |
|
179
|
|
|
$this->timeout = $timeout; |
|
|
|
|
|
|
180
|
|
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
/** |
|
183
|
|
|
* Adds a proc_open option. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $name The option name |
|
186
|
|
|
* @param string $value The option value |
|
187
|
|
|
* |
|
188
|
|
|
* @return $this |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setOption($name, $value) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->options[$name] = $value; |
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
/** |
|
196
|
|
|
* Disables fetching output and error output from the underlying process. |
|
197
|
|
|
* |
|
198
|
|
|
* @return $this |
|
199
|
|
|
*/ |
|
200
|
|
|
public function disableOutput() |
|
201
|
|
|
{ |
|
202
|
|
|
$this->outputDisabled = true; |
|
203
|
|
|
return $this; |
|
204
|
|
|
} |
|
205
|
|
|
/** |
|
206
|
|
|
* Enables fetching output and error output from the underlying process. |
|
207
|
|
|
* |
|
208
|
|
|
* @return $this |
|
209
|
|
|
*/ |
|
210
|
|
|
public function enableOutput() |
|
211
|
|
|
{ |
|
212
|
|
|
$this->outputDisabled = false; |
|
213
|
|
|
return $this; |
|
214
|
|
|
} |
|
215
|
|
|
/** |
|
216
|
|
|
* Creates a Process instance and returns it. |
|
217
|
|
|
* |
|
218
|
|
|
* @return Process |
|
219
|
|
|
* |
|
220
|
|
|
* @throws LogicException In case no arguments have been provided |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getProcess() |
|
223
|
|
|
{ |
|
224
|
|
|
if (0 === \count($this->prefix) && 0 === \count($this->arguments)) { |
|
225
|
|
|
throw new LogicException('You must add() command arguments before calling getProcess().'); |
|
226
|
|
|
} |
|
227
|
|
|
$arguments = array_merge($this->prefix, $this->arguments); |
|
228
|
|
|
$process = new Process($arguments, $this->cwd, $this->env, $this->input, $this->timeout, $this->options); |
|
|
|
|
|
|
229
|
|
|
// to preserve the BC with symfony <3.3, we convert the array structure |
|
230
|
|
|
// to a string structure to avoid the prefixing with the exec command |
|
231
|
|
|
$process->setCommandLine($process->getCommandLine()); |
|
232
|
|
|
if ($this->inheritEnv) { |
|
233
|
|
|
$process->inheritEnvironmentVariables(); |
|
234
|
|
|
} |
|
235
|
|
|
if ($this->outputDisabled) { |
|
236
|
|
|
$process->disableOutput(); |
|
237
|
|
|
} |
|
238
|
|
|
return $process; |
|
239
|
|
|
} |
|
240
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.