forked from welaika/wordless
/
process.php
226 lines (194 loc) · 5.36 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
<?php
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 = null === $cwd ? getcwd() : $cwd;
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()
{
$this->stdout = '';
$this->stderr = '';
$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 Exception('Unable to launch a new process.');
}
fwrite($pipes[0], $this->stdin);
$status = proc_get_status($process);
while($status['running']) {
$this->stdout .= stream_get_contents($pipes[1]);
$this->stderr .= stream_get_contents($pipes[2]);
$status = proc_get_status($process);
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$this->exitcode = $status['exitcode'];
return $this->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;
}
public function addOutput($line)
{
$this->stdout .= $line;
}
public function addErrorOutput($line)
{
$this->stderr .= $line;
}
public function getCommandLine()
{
return $this->commandline;
}
public function setCommandLine($commandline)
{
$this->commandline = $commandline;
}
public function getTimeout()
{
return $this->timeout;
}
public function setTimeout($timeout)
{
$this->timeout = $timeout;
}
public function getWorkingDirectory()
{
return $this->cwd;
}
public function setWorkingDirectory($cwd)
{
$this->cwd = $cwd;
}
public function getEnv()
{
return $this->env;
}
public function setEnv(array $env)
{
$this->env = $env;
}
public function getStdin()
{
return $this->stdin;
}
public function setStdin($stdin)
{
$this->stdin = $stdin;
}
public function getOptions()
{
return $this->options;
}
public function setOptions(array $options)
{
$this->options = $options;
}
}