|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* Copyright (c) 2004-2011 Fabien Potencier |
|
|
7 |
* |
|
|
8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
|
9 |
* of this software and associated documentation files (the "Software"), to deal |
|
|
10 |
* in the Software without restriction, including without limitation the rights |
|
|
11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
|
12 |
* copies of the Software, and to permit persons to whom the Software is furnished |
|
|
13 |
* to do so, subject to the following conditions: |
|
|
14 |
* |
|
|
15 |
* The above copyright notice and this permission notice shall be included in all |
|
|
16 |
* copies or substantial portions of the Software. |
|
|
17 |
* |
|
|
18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
|
19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
|
20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
|
21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
|
22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
|
23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
|
24 |
* THE SOFTWARE. |
|
|
25 |
*/ |
|
|
26 |
|
|
|
27 |
namespace Assetic\Util; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* Process is a thin wrapper around proc_* functions to ease |
|
|
31 |
* start independent PHP processes. |
|
|
32 |
* |
|
|
33 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
34 |
* |
|
|
35 |
* @api |
|
|
36 |
*/ |
|
|
37 |
class Process |
|
|
38 |
{ |
|
|
39 |
private $commandline; |
|
|
40 |
private $cwd; |
|
|
41 |
private $env; |
|
|
42 |
private $stdin; |
|
|
43 |
private $timeout; |
|
|
44 |
private $options; |
|
|
45 |
private $exitcode; |
|
|
46 |
private $status; |
|
|
47 |
private $stdout; |
|
|
48 |
private $stderr; |
|
|
49 |
|
|
|
50 |
/** |
|
|
51 |
* Constructor. |
|
|
52 |
* |
|
|
53 |
* @param string $commandline The command line to run |
|
|
54 |
* @param string $cwd The working directory |
|
|
55 |
* @param array $env The environment variables |
|
|
56 |
* @param string $stdin The STDIN content |
|
|
57 |
* @param integer $timeout The timeout in seconds |
|
|
58 |
* @param array $options An array of options for proc_open |
|
|
59 |
* |
|
|
60 |
* @throws \RuntimeException When proc_open is not installed |
|
|
61 |
* |
|
|
62 |
* @api |
|
|
63 |
*/ |
|
|
64 |
public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array()) |
|
|
65 |
{ |
|
|
66 |
if (!function_exists('proc_open')) { |
|
|
67 |
throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.'); |
|
|
68 |
} |
|
|
69 |
|
|
|
70 |
$this->commandline = $commandline; |
|
|
71 |
$this->cwd = null === $cwd ? getcwd() : $cwd; |
|
|
72 |
if (null !== $env) { |
|
|
73 |
$this->env = array(); |
|
|
74 |
foreach ($env as $key => $value) { |
|
|
75 |
$this->env[(binary) $key] = (binary) $value; |
|
|
76 |
} |
|
|
77 |
} else { |
|
|
78 |
$this->env = null; |
|
|
79 |
} |
|
|
80 |
$this->stdin = $stdin; |
|
|
81 |
$this->timeout = $timeout; |
|
|
82 |
$this->options = array_merge(array('suppress_errors' => true, 'binary_pipes' => true, 'bypass_shell' => false), $options); |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
/** |
|
|
86 |
* Runs the process. |
|
|
87 |
* |
|
|
88 |
* The callback receives the type of output (out or err) and |
|
|
89 |
* some bytes from the output in real-time. It allows to have feedback |
|
|
90 |
* from the independent process during execution. |
|
|
91 |
* |
|
|
92 |
* The STDOUT and STDERR are also available after the process is finished |
|
|
93 |
* via the getOutput() and getErrorOutput() methods. |
|
|
94 |
* |
|
|
95 |
* @param Closure|string|array $callback A PHP callback to run whenever there is some |
|
|
96 |
* output available on STDOUT or STDERR |
|
|
97 |
* |
|
|
98 |
* @return integer The exit status code |
|
|
99 |
* |
|
|
100 |
* @throws \RuntimeException When process can't be launch or is stopped |
|
|
101 |
* |
|
|
102 |
* @api |
|
|
103 |
*/ |
|
|
104 |
public function run($callback = null) |
|
|
105 |
{ |
|
|
106 |
$this->stdout = ''; |
|
|
107 |
$this->stderr = ''; |
|
|
108 |
$that = $this; |
|
|
109 |
$callback = function ($type, $data) use ($that, $callback) |
|
|
110 |
{ |
|
|
111 |
if ('out' == $type) { |
|
|
112 |
$that->addOutput($data); |
|
|
113 |
} else { |
|
|
114 |
$that->addErrorOutput($data); |
|
|
115 |
} |
|
|
116 |
|
|
|
117 |
if (null !== $callback) { |
|
|
118 |
call_user_func($callback, $type, $data); |
|
|
119 |
} |
|
|
120 |
}; |
|
|
121 |
|
|
|
122 |
$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')); |
|
|
123 |
|
|
|
124 |
$process = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options); |
|
|
125 |
|
|
|
126 |
if (!is_resource($process)) { |
|
|
127 |
throw new \RuntimeException('Unable to launch a new process.'); |
|
|
128 |
} |
|
|
129 |
|
|
|
130 |
foreach ($pipes as $pipe) { |
|
|
131 |
stream_set_blocking($pipe, false); |
|
|
132 |
} |
|
|
133 |
|
|
|
134 |
if (null === $this->stdin) { |
|
|
135 |
fclose($pipes[0]); |
|
|
136 |
$writePipes = null; |
|
|
137 |
} else { |
|
|
138 |
$writePipes = array($pipes[0]); |
|
|
139 |
$stdinLen = strlen($this->stdin); |
|
|
140 |
$stdinOffset = 0; |
|
|
141 |
} |
|
|
142 |
unset($pipes[0]); |
|
|
143 |
|
|
|
144 |
while ($pipes || $writePipes) { |
|
|
145 |
$r = $pipes; |
|
|
146 |
$w = $writePipes; |
|
|
147 |
$e = null; |
|
|
148 |
|
|
|
149 |
$n = @stream_select($r, $w, $e, $this->timeout); |
|
|
150 |
|
|
|
151 |
if (false === $n) { |
|
|
152 |
break; |
|
|
153 |
} elseif ($n === 0) { |
|
|
154 |
proc_terminate($process); |
|
|
155 |
|
|
|
156 |
throw new \RuntimeException('The process timed out.'); |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
if ($w) { |
|
|
160 |
$written = fwrite($writePipes[0], (binary) substr($this->stdin, $stdinOffset), 8192); |
|
|
161 |
if (false !== $written) { |
|
|
162 |
$stdinOffset += $written; |
|
|
163 |
} |
|
|
164 |
if ($stdinOffset >= $stdinLen) { |
|
|
165 |
fclose($writePipes[0]); |
|
|
166 |
$writePipes = null; |
|
|
167 |
} |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
foreach ($r as $pipe) { |
|
|
171 |
$type = array_search($pipe, $pipes); |
|
|
172 |
$data = fread($pipe, 8192); |
|
|
173 |
if (strlen($data) > 0) { |
|
|
174 |
call_user_func($callback, $type == 1 ? 'out' : 'err', $data); |
|
|
175 |
} |
|
|
176 |
if (false === $data || feof($pipe)) { |
|
|
177 |
fclose($pipe); |
|
|
178 |
unset($pipes[$type]); |
|
|
179 |
} |
|
|
180 |
} |
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
$this->status = proc_get_status($process); |
|
|
184 |
|
|
|
185 |
$time = 0; |
|
|
186 |
while (1 == $this->status['running'] && $time < 1000000) { |
|
|
187 |
$time += 1000; |
|
|
188 |
usleep(1000); |
|
|
189 |
$this->status = proc_get_status($process); |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
proc_close($process); |
|
|
193 |
|
|
|
194 |
if ($this->status['signaled']) { |
|
|
195 |
throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig'])); |
|
|
196 |
} |
|
|
197 |
|
|
|
198 |
return $this->exitcode = $this->status['exitcode']; |
|
|
199 |
} |
|
|
200 |
|
|
|
201 |
/** |
|
|
202 |
* Returns the output of the process (STDOUT). |
|
|
203 |
* |
|
|
204 |
* This only returns the output if you have not supplied a callback |
|
|
205 |
* to the run() method. |
|
|
206 |
* |
|
|
207 |
* @return string The process output |
|
|
208 |
* |
|
|
209 |
* @api |
|
|
210 |
*/ |
|
|
211 |
public function getOutput() |
|
|
212 |
{ |
|
|
213 |
return $this->stdout; |
|
|
214 |
} |
|
|
215 |
|
|
|
216 |
/** |
|
|
217 |
* Returns the error output of the process (STDERR). |
|
|
218 |
* |
|
|
219 |
* This only returns the error output if you have not supplied a callback |
|
|
220 |
* to the run() method. |
|
|
221 |
* |
|
|
222 |
* @return string The process error output |
|
|
223 |
* |
|
|
224 |
* @api |
|
|
225 |
*/ |
|
|
226 |
public function getErrorOutput() |
|
|
227 |
{ |
|
|
228 |
return $this->stderr; |
|
|
229 |
} |
|
|
230 |
|
|
|
231 |
/** |
|
|
232 |
* Returns the exit code returned by the process. |
|
|
233 |
* |
|
|
234 |
* @return integer The exit status code |
|
|
235 |
* |
|
|
236 |
* @api |
|
|
237 |
*/ |
|
|
238 |
public function getExitCode() |
|
|
239 |
{ |
|
|
240 |
return $this->exitcode; |
|
|
241 |
} |
|
|
242 |
|
|
|
243 |
/** |
|
|
244 |
* Checks if the process ended successfully. |
|
|
245 |
* |
|
|
246 |
* @return Boolean true if the process ended successfully, false otherwise |
|
|
247 |
* |
|
|
248 |
* @api |
|
|
249 |
*/ |
|
|
250 |
public function isSuccessful() |
|
|
251 |
{ |
|
|
252 |
return 0 == $this->exitcode; |
|
|
253 |
} |
|
|
254 |
|
|
|
255 |
/** |
|
|
256 |
* Returns true if the child process has been terminated by an uncaught signal. |
|
|
257 |
* |
|
|
258 |
* It always returns false on Windows. |
|
|
259 |
* |
|
|
260 |
* @return Boolean |
|
|
261 |
* |
|
|
262 |
* @api |
|
|
263 |
*/ |
|
|
264 |
public function hasBeenSignaled() |
|
|
265 |
{ |
|
|
266 |
return $this->status['signaled']; |
|
|
267 |
} |
|
|
268 |
|
|
|
269 |
/** |
|
|
270 |
* Returns the number of the signal that caused the child process to terminate its execution. |
|
|
271 |
* |
|
|
272 |
* It is only meaningful if hasBeenSignaled() returns true. |
|
|
273 |
* |
|
|
274 |
* @return integer |
|
|
275 |
* |
|
|
276 |
* @api |
|
|
277 |
*/ |
|
|
278 |
public function getTermSignal() |
|
|
279 |
{ |
|
|
280 |
return $this->status['termsig']; |
|
|
281 |
} |
|
|
282 |
|
|
|
283 |
/** |
|
|
284 |
* Returns true if the child process has been stopped by a signal. |
|
|
285 |
* |
|
|
286 |
* It always returns false on Windows. |
|
|
287 |
* |
|
|
288 |
* @return Boolean |
|
|
289 |
* |
|
|
290 |
* @api |
|
|
291 |
*/ |
|
|
292 |
public function hasBeenStopped() |
|
|
293 |
{ |
|
|
294 |
return $this->status['stopped']; |
|
|
295 |
} |
|
|
296 |
|
|
|
297 |
/** |
|
|
298 |
* Returns the number of the signal that caused the child process to stop its execution |
|
|
299 |
* |
|
|
300 |
* It is only meaningful if hasBeenStopped() returns true. |
|
|
301 |
* |
|
|
302 |
* @return integer |
|
|
303 |
* |
|
|
304 |
* @api |
|
|
305 |
*/ |
|
|
306 |
public function getStopSignal() |
|
|
307 |
{ |
|
|
308 |
return $this->status['stopsig']; |
|
|
309 |
} |
|
|
310 |
|
|
|
311 |
public function addOutput($line) |
|
|
312 |
{ |
|
|
313 |
$this->stdout .= $line; |
|
|
314 |
} |
|
|
315 |
|
|
|
316 |
public function addErrorOutput($line) |
|
|
317 |
{ |
|
|
318 |
$this->stderr .= $line; |
|
|
319 |
} |
|
|
320 |
|
|
|
321 |
public function getCommandLine() |
|
|
322 |
{ |
|
|
323 |
return $this->commandline; |
|
|
324 |
} |
|
|
325 |
|
|
|
326 |
public function setCommandLine($commandline) |
|
|
327 |
{ |
|
|
328 |
$this->commandline = $commandline; |
|
|
329 |
} |
|
|
330 |
|
|
|
331 |
public function getTimeout() |
|
|
332 |
{ |
|
|
333 |
return $this->timeout; |
|
|
334 |
} |
|
|
335 |
|
|
|
336 |
public function setTimeout($timeout) |
|
|
337 |
{ |
|
|
338 |
$this->timeout = $timeout; |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
public function getWorkingDirectory() |
|
|
342 |
{ |
|
|
343 |
return $this->cwd; |
|
|
344 |
} |
|
|
345 |
|
|
|
346 |
public function setWorkingDirectory($cwd) |
|
|
347 |
{ |
|
|
348 |
$this->cwd = $cwd; |
|
|
349 |
} |
|
|
350 |
|
|
|
351 |
public function getEnv() |
|
|
352 |
{ |
|
|
353 |
return $this->env; |
|
|
354 |
} |
|
|
355 |
|
|
|
356 |
public function setEnv(array $env) |
|
|
357 |
{ |
|
|
358 |
$this->env = $env; |
|
|
359 |
} |
|
|
360 |
|
|
|
361 |
public function getStdin() |
|
|
362 |
{ |
|
|
363 |
return $this->stdin; |
|
|
364 |
} |
|
|
365 |
|
|
|
366 |
public function setStdin($stdin) |
|
|
367 |
{ |
|
|
368 |
$this->stdin = $stdin; |
|
|
369 |
} |
|
|
370 |
|
|
|
371 |
public function getOptions() |
|
|
372 |
{ |
|
|
373 |
return $this->options; |
|
|
374 |
} |
|
|
375 |
|
|
|
376 |
public function setOptions(array $options) |
|
|
377 |
{ |
|
|
378 |
$this->options = $options; |
|
|
379 |
} |
|
|
380 |
} |