|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHP-CLI package. |
|
5
|
|
|
* <https://github.com/adhocore/php-cli> |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Sushil Gupta <[email protected]> |
|
8
|
|
|
* <https://github.com/sushilgupta> |
|
9
|
|
|
* |
|
10
|
|
|
* Licensed under MIT license. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Ahc\Cli\Helper; |
|
14
|
|
|
|
|
15
|
|
|
use Ahc\Cli\Exception\RuntimeException; |
|
16
|
|
|
|
|
17
|
|
|
/* |
|
18
|
|
|
* A thin proc_open wrapper to execute shell commands. |
|
19
|
|
|
* @author Sushil Gupta <[email protected]> |
|
20
|
|
|
* @license MIT |
|
21
|
|
|
*/ |
|
22
|
|
|
class Shell |
|
23
|
|
|
{ |
|
24
|
|
|
const STDIN_DESCRIPTOR_KEY = 0; |
|
25
|
|
|
const STDOUT_DESCRIPTOR_KEY = 1; |
|
26
|
|
|
const STDERR_DESCRIPTOR_KEY = 2; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string Command to be executed */ |
|
29
|
|
|
protected $command; |
|
30
|
|
|
|
|
31
|
|
|
/** @var array Descriptor to be passed for proc_open */ |
|
32
|
|
|
protected $descriptors; |
|
33
|
|
|
|
|
34
|
|
|
/** @var int Exit code of the process once it has been terminated */ |
|
35
|
|
|
protected $exitCode; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string Input for stdin */ |
|
38
|
|
|
protected $input; |
|
39
|
|
|
|
|
40
|
|
|
/** @var array Pointers to stdin, stdout & stderr */ |
|
41
|
|
|
protected $pipes; |
|
42
|
|
|
|
|
43
|
|
|
/** @var resource The actual process resource returned from proc_open */ |
|
44
|
|
|
protected $process; |
|
45
|
|
|
|
|
46
|
|
|
/** @var string Status of the process as returned from proc_get_status */ |
|
47
|
|
|
protected $status; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct(string $command, string $input = null) |
|
50
|
|
|
{ |
|
51
|
|
|
if (!\function_exists('proc_open')) { |
|
52
|
|
|
throw new RuntimeException('Required proc_open could not be found in your PHP setup'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->command = $command; |
|
56
|
|
|
$this->input = $input; |
|
57
|
|
|
$this->status = null; |
|
58
|
|
|
$this->exitCode = null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function getDescriptors() |
|
62
|
|
|
{ |
|
63
|
|
|
$out = '\\' === \DIRECTORY_SEPARATOR ? ['file', 'NUL', 'w'] : ['pipe', 'w']; |
|
64
|
|
|
|
|
65
|
|
|
return [ |
|
66
|
|
|
self::STDIN_DESCRIPTOR_KEY => ['pipe', 'r'], |
|
67
|
|
|
self::STDOUT_DESCRIPTOR_KEY => $out, |
|
68
|
|
|
self::STDERR_DESCRIPTOR_KEY => $out, |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function setInput() |
|
73
|
|
|
{ |
|
74
|
|
|
\fwrite($this->pipes[self::STDIN_DESCRIPTOR_KEY], $this->input); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function updateStatus() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->status = \proc_get_status($this->process); |
|
|
|
|
|
|
80
|
|
|
$this->exitCode = $this->status['exitcode']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function closePipes() |
|
84
|
|
|
{ |
|
85
|
|
|
\fclose($this->pipes[self::STDIN_DESCRIPTOR_KEY]); |
|
86
|
|
|
\fclose($this->pipes[self::STDOUT_DESCRIPTOR_KEY]); |
|
87
|
|
|
\fclose($this->pipes[self::STDERR_DESCRIPTOR_KEY]); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function execute() |
|
91
|
|
|
{ |
|
92
|
|
|
if ($this->isRunning()) { |
|
93
|
|
|
throw new RuntimeException('Process is already running'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->descriptors = $this->getDescriptors(); |
|
97
|
|
|
|
|
98
|
|
|
$this->process = proc_open($this->command, $this->descriptors, $this->pipes); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
if (!\is_resource($this->process)) { |
|
101
|
|
|
throw new RuntimeException('Bad program could not be started.'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->setInput(); |
|
105
|
|
|
$this->updateStatus(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getStatus() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->updateStatus(); |
|
111
|
|
|
|
|
112
|
|
|
return $this->status; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getOutput() |
|
116
|
|
|
{ |
|
117
|
|
|
return \stream_get_contents($this->pipes[self::STDOUT_DESCRIPTOR_KEY]); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getErrorOutput() |
|
121
|
|
|
{ |
|
122
|
|
|
return \stream_get_contents($this->pipes[self::STDERR_DESCRIPTOR_KEY]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function getExitCode() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->updateStatus(); |
|
128
|
|
|
|
|
129
|
|
|
return $this->exitCode; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function isRunning() |
|
133
|
|
|
{ |
|
134
|
|
|
$this->updateStatus(); |
|
135
|
|
|
|
|
136
|
|
|
return $this->status['running']; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function getProcessId() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->isRunning() ? $this->status['pid'] : null; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function stop() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->closePipes(); |
|
147
|
|
|
|
|
148
|
|
|
if (\is_resource($this->process)) { |
|
149
|
|
|
\proc_close($this->process); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$this->exitCode = $this->status['exitcode']; |
|
153
|
|
|
|
|
154
|
|
|
return $this->exitCode; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function kill() |
|
158
|
|
|
{ |
|
159
|
|
|
return \proc_terminate($this->process); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function __destruct() |
|
163
|
|
|
{ |
|
164
|
|
|
$this->stop(); |
|
165
|
|
|
} |
|
166
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..