-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Process.php
445 lines (399 loc) · 11 KB
/
Process.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Process;
/**
* Process is a thin wrapper around proc_* functions to ease
* start independent PHP processes.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
class Process
{
private $commandline;
private $cwd;
private $env;
private $stdin;
private $timeout;
private $options;
private $exitcode;
private $status;
private $stdout;
private $stderr;
/**
* Constructor.
*
* @param string $commandline The command line to run
* @param string $cwd The working directory
* @param array $env The environment variables
* @param string $stdin The STDIN content
* @param integer $timeout The timeout in seconds
* @param array $options An array of options for proc_open
*
* @throws \RuntimeException When proc_open is not installed
*
* @api
*/
public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
if (!function_exists('proc_open')) {
throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
}
$this->commandline = $commandline;
$this->cwd = $cwd;
// on windows, if the cwd changed via chdir(), proc_open defaults to the dir where php was started
if (null === $this->cwd && defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->cwd = getcwd();
}
if (null !== $env) {
$this->env = array();
foreach ($env as $key => $value) {
$this->env[(binary) $key] = (binary) $value;
}
} else {
$this->env = null;
}
$this->stdin = $stdin;
$this->timeout = $timeout;
$this->options = array_merge(array('suppress_errors' => true, 'binary_pipes' => true, 'bypass_shell' => false), $options);
}
/**
* Runs the process.
*
* The callback receives the type of output (out or err) and
* some bytes from the output in real-time. It allows to have feedback
* from the independent process during execution.
*
* The STDOUT and STDERR are also available after the process is finished
* via the getOutput() and getErrorOutput() methods.
*
* @param Closure|string|array $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @return integer The exit status code
*
* @throws \RuntimeException When process can't be launch or is stopped
*
* @api
*/
public function run($callback = null)
{
$this->stdout = '';
$this->stderr = '';
$that = $this;
$callback = function ($type, $data) use ($that, $callback) {
if ('out' == $type) {
$that->addOutput($data);
} else {
$that->addErrorOutput($data);
}
if (null !== $callback) {
call_user_func($callback, $type, $data);
}
};
$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
$process = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);
if (!is_resource($process)) {
throw new \RuntimeException('Unable to launch a new process.');
}
foreach ($pipes as $pipe) {
stream_set_blocking($pipe, false);
}
if (null === $this->stdin) {
fclose($pipes[0]);
$writePipes = null;
} else {
$writePipes = array($pipes[0]);
$stdinLen = strlen($this->stdin);
$stdinOffset = 0;
}
unset($pipes[0]);
while ($pipes || $writePipes) {
$r = $pipes;
$w = $writePipes;
$e = null;
$n = @stream_select($r, $w, $e, $this->timeout);
if (false === $n) {
break;
} elseif ($n === 0) {
proc_terminate($process);
throw new \RuntimeException('The process timed out.');
}
if ($w) {
$written = fwrite($writePipes[0], (binary) substr($this->stdin, $stdinOffset), 8192);
if (false !== $written) {
$stdinOffset += $written;
}
if ($stdinOffset >= $stdinLen) {
fclose($writePipes[0]);
$writePipes = null;
}
}
foreach ($r as $pipe) {
$type = array_search($pipe, $pipes);
$data = fread($pipe, 8192);
if (strlen($data) > 0) {
call_user_func($callback, $type == 1 ? 'out' : 'err', $data);
}
if (false === $data || feof($pipe)) {
fclose($pipe);
unset($pipes[$type]);
}
}
}
$this->status = proc_get_status($process);
$time = 0;
while (1 == $this->status['running'] && $time < 1000000) {
$time += 1000;
usleep(1000);
$this->status = proc_get_status($process);
}
$exitcode = proc_close($process);
if ($this->status['signaled']) {
throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
}
return $this->exitcode = $this->status['running'] ? $exitcode : $this->status['exitcode'];
}
/**
* Returns the output of the process (STDOUT).
*
* This only returns the output if you have not supplied a callback
* to the run() method.
*
* @return string The process output
*
* @api
*/
public function getOutput()
{
return $this->stdout;
}
/**
* Returns the error output of the process (STDERR).
*
* This only returns the error output if you have not supplied a callback
* to the run() method.
*
* @return string The process error output
*
* @api
*/
public function getErrorOutput()
{
return $this->stderr;
}
/**
* Returns the exit code returned by the process.
*
* @return integer The exit status code
*
* @api
*/
public function getExitCode()
{
return $this->exitcode;
}
/**
* Checks if the process ended successfully.
*
* @return Boolean true if the process ended successfully, false otherwise
*
* @api
*/
public function isSuccessful()
{
return 0 == $this->exitcode;
}
/**
* Returns true if the child process has been terminated by an uncaught signal.
*
* It always returns false on Windows.
*
* @return Boolean
*
* @api
*/
public function hasBeenSignaled()
{
return $this->status['signaled'];
}
/**
* Returns the number of the signal that caused the child process to terminate its execution.
*
* It is only meaningful if hasBeenSignaled() returns true.
*
* @return integer
*
* @api
*/
public function getTermSignal()
{
return $this->status['termsig'];
}
/**
* Returns true if the child process has been stopped by a signal.
*
* It always returns false on Windows.
*
* @return Boolean
*
* @api
*/
public function hasBeenStopped()
{
return $this->status['stopped'];
}
/**
* Returns the number of the signal that caused the child process to stop its execution.
*
* It is only meaningful if hasBeenStopped() returns true.
*
* @return integer
*
* @api
*/
public function getStopSignal()
{
return $this->status['stopsig'];
}
/**
* Adds a line to the STDOUT stream.
*
* @param string $line The line to append
*/
public function addOutput($line)
{
$this->stdout .= $line;
}
/**
* Adds a line to the STDERR stream.
*
* @param string $line The line to append
*/
public function addErrorOutput($line)
{
$this->stderr .= $line;
}
/**
* Gets the command line to be executed.
*
* @return string The command to execute
*/
public function getCommandLine()
{
return $this->commandline;
}
/**
* Sets the command line to be executed.
*
* @param string $commandline The command to execute
*/
public function setCommandLine($commandline)
{
$this->commandline = $commandline;
}
/**
* Gets the process timeout.
*
* @return integer The timeout in seconds
*/
public function getTimeout()
{
return $this->timeout;
}
/**
* Sets the process timeout.
*
* @param integer|null $timeout The timeout in seconds
*/
public function setTimeout($timeout)
{
$this->timeout = $timeout;
}
/**
* Gets the working directory.
*
* @return string The current working directory
*/
public function getWorkingDirectory()
{
// This is for BC only
if (null === $this->cwd) {
// getcwd() will return false if any one of the parent directories does not have
// the readable or search mode set, even if the current directory does
return getcwd() ?: null;
}
return $this->cwd;
}
/**
* Sets the current working directory.
*
* @param string $cwd The new working directory
*/
public function setWorkingDirectory($cwd)
{
$this->cwd = $cwd;
}
/**
* Gets the environment variables.
*
* @return array The current environment variables
*/
public function getEnv()
{
return $this->env;
}
/**
* Sets the environment variables.
*
* @param array $env The new environment variables
*/
public function setEnv(array $env)
{
$this->env = $env;
}
/**
* Gets the contents of STDIN.
*
* @return string The current contents
*/
public function getStdin()
{
return $this->stdin;
}
/**
* Sets the contents of STDIN.
*
* @param string $stdin The new contents
*/
public function setStdin($stdin)
{
$this->stdin = $stdin;
}
/**
* Gets the options for proc_open.
*
* @return array The current options
*/
public function getOptions()
{
return $this->options;
}
/**
* Sets the options for proc_open.
*
* @param array $options The new options
*/
public function setOptions(array $options)
{
$this->options = $options;
}
}