-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathProcess.php
153 lines (120 loc) · 3.39 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
<?php
namespace PHP_Parallel_Lint\PhpParallelLint\Process;
use PHP_Parallel_Lint\PhpParallelLint\Exceptions\RuntimeException;
class Process
{
const STDIN = 0,
STDOUT = 1,
STDERR = 2;
const READ = 'r',
WRITE = 'w';
/** @var resource */
protected $process;
/** @var resource */
protected $stdout;
/** @var resource */
protected $stderr;
/** @var string */
private $output;
/** @var string */
private $errorOutput;
/** @var int */
private $statusCode;
/**
* @param string $executable
* @param string[] $arguments
* @param string $stdInInput
* @throws RuntimeException
*/
public function __construct($executable, array $arguments = array(), $stdInInput = null)
{
$descriptors = array(
self::STDIN => array('pipe', self::READ),
self::STDOUT => array('pipe', self::WRITE),
self::STDERR => array('pipe', self::WRITE),
);
$cmdLine = '"' . $executable . '" ' . implode(' ', array_map('escapeshellarg', $arguments));
$this->process = proc_open($cmdLine, $descriptors, $pipes, null, null, array('bypass_shell' => true));
if ($this->process === false || $this->process === null) {
throw new RuntimeException("Cannot create new process $cmdLine");
}
list($stdin, $this->stdout, $this->stderr) = $pipes;
if ($stdInInput) {
fwrite($stdin, $stdInInput);
}
fclose($stdin);
}
/**
* @return bool
*/
public function isFinished()
{
if ($this->statusCode !== null) {
return true;
}
$status = proc_get_status($this->process);
if ($status['running']) {
return false;
} elseif ($this->statusCode === null) {
$this->statusCode = (int) $status['exitcode'];
}
// Process outputs
$this->output = stream_get_contents($this->stdout);
fclose($this->stdout);
$this->errorOutput = stream_get_contents($this->stderr);
fclose($this->stderr);
$statusCode = proc_close($this->process);
if ($this->statusCode === null) {
$this->statusCode = $statusCode;
}
$this->process = null;
return true;
}
public function waitForFinish()
{
while (!$this->isFinished()) {
usleep(100);
}
}
/**
* @return string
* @throws RuntimeException
*/
public function getOutput()
{
if (!$this->isFinished()) {
throw new RuntimeException("Cannot get output for running process");
}
return $this->output;
}
/**
* @return string
* @throws RuntimeException
*/
public function getErrorOutput()
{
if (!$this->isFinished()) {
throw new RuntimeException("Cannot get error output for running process");
}
return $this->errorOutput;
}
/**
* @return int
* @throws RuntimeException
*/
public function getStatusCode()
{
if (!$this->isFinished()) {
throw new RuntimeException("Cannot get status code for running process");
}
return $this->statusCode;
}
/**
* @return bool
* @throws RuntimeException
*/
public function isFail()
{
return $this->getStatusCode() === 1;
}
}