forked from php-censor/php-censor
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseCommandExecutor.php
More file actions
263 lines (215 loc) · 7.01 KB
/
Copy pathBaseCommandExecutor.php
File metadata and controls
263 lines (215 loc) · 7.01 KB
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
<?php
namespace PHPCensor\Helper;
use Exception;
use PHPCensor\Logging\BuildLogger;
use Psr\Log\LogLevel;
/**
* Handles running system commands with variables.
*/
abstract class BaseCommandExecutor implements CommandExecutorInterface
{
/**
* @var BuildLogger
*/
protected $logger;
/**
* @var bool
*/
protected $quiet;
/**
* @var bool
*/
protected $verbose;
protected $lastOutput;
protected $lastError;
public $logExecOutput = true;
/**
* The path which findBinary will look in.
* @var string
*/
protected $rootDir;
/**
* Current build path
* @var string
*/
protected $buildPath;
/**
* @param BuildLogger $logger
* @param string $rootDir
* @param bool $quiet
* @param bool $verbose
*/
public function __construct(BuildLogger $logger, $rootDir, &$quiet = false, &$verbose = false)
{
$this->logger = $logger;
$this->quiet = $quiet;
$this->verbose = $verbose;
$this->lastOutput = [];
$this->rootDir = $rootDir;
}
/**
* Executes shell commands.
*
* @param array $args
*
* @return bool Indicates success
*/
public function executeCommand($args = [])
{
$this->lastOutput = [];
$this->logger->logDebug('Args: ' . json_encode($args));
$command = call_user_func_array('sprintf', $args);
$this->logger->logDebug('Command: ' . $command);
if ($this->quiet) {
$this->logger->log('Executing: ' . $command);
}
$status = 0;
$descriptorSpec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"], // stderr
];
$pipes = [];
$process = proc_open($command, $descriptorSpec, $pipes, $this->buildPath, null);
$this->lastOutput = '';
$this->lastError = '';
if (is_resource($process)) {
fclose($pipes[0]);
list($this->lastOutput, $this->lastError) = $this->readAlternating([$pipes[1], $pipes[2]]);
$status = proc_close($process);
}
$this->lastOutput = array_filter(explode(PHP_EOL, $this->lastOutput));
$shouldOutput = ($this->logExecOutput && ($this->verbose || $status != 0));
if ($shouldOutput && !empty($this->lastOutput)) {
$this->logger->log($this->lastOutput);
}
if (!empty($this->lastError)) {
$this->logger->log("\033[0;31m" . $this->lastError . "\033[0m", LogLevel::ERROR);
}
$rtn = false;
if ($status == 0) {
$rtn = true;
}
$this->logger->logDebug('Execution status: ' . $status);
return $rtn;
}
/**
* Reads from array of streams as data becomes available.
* @param array $descriptors
* @return string[] data read from each descriptor
*/
private function readAlternating(array $descriptors)
{
$outputs = [];
foreach ($descriptors as $key => $descriptor) {
stream_set_blocking($descriptor, false);
$outputs[$key] = '';
}
do {
$read = $descriptors;
$write = null;
$except = null;
stream_select($read, $write, $except, null);
foreach ($read as $descriptor) {
$key = array_search($descriptor, $descriptors);
if (feof($descriptor)) {
fclose($descriptor);
unset($descriptors[$key]);
} else {
$outputs[$key] .= fgets($descriptor);
}
}
} while (count($descriptors) > 0);
return $outputs;
}
/**
* Returns the output from the last command run.
*/
public function getLastOutput()
{
return implode(PHP_EOL, $this->lastOutput);
}
/**
* Returns the stderr output from the last command run.
*/
public function getLastError()
{
return $this->lastError;
}
/**
* Find a binary required by a plugin
*
* @param string $binary
* @param bool $quiet
*
* @throws Exception
*
* @return null|string
*/
public function findBinary($binary, $quiet = false)
{
$composerBin = $this->getComposerBinDir(realpath($this->buildPath));
if (is_string($binary)) {
$binary = [$binary];
}
foreach ($binary as $bin) {
$this->logger->logDebug(sprintf('Looking for binary: %s', $bin));
if (is_dir($composerBin) && is_file($composerBin . DIRECTORY_SEPARATOR . $bin)) {
$this->logger->logDebug(sprintf('Found in %s: %s', $composerBin, $bin));
return $composerBin . DIRECTORY_SEPARATOR . $bin;
}
if (is_file($this->rootDir . DIRECTORY_SEPARATOR . $bin)) {
$this->logger->logDebug(sprintf('Found in %s: %s', 'root', $bin));
return $this->rootDir . DIRECTORY_SEPARATOR . $bin;
}
if (is_file($this->rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . $bin)) {
$this->logger->logDebug(sprintf('Found in %s: %s', 'vendor/bin', $bin));
return $this->rootDir . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . $bin;
}
$findCmdResult = $this->findGlobalBinary($bin);
if (is_file($findCmdResult)) {
$this->logger->logDebug(sprintf('Found in %s: %s', '', $bin));
return $findCmdResult;
}
}
if ($quiet) {
return null;
}
throw new Exception(sprintf('Could not find %s', implode('/', $binary)));
}
/**
* Find a binary which is installed globally on the system
* @param string $binary
* @return null|string
*/
abstract protected function findGlobalBinary($binary);
/**
* Try to load the composer.json file in the building project
* If the bin-dir is configured, return the full path to it
* @param string $path Current build path
* @return string|null
*/
public function getComposerBinDir($path)
{
if (is_dir($path)) {
$composer = $path . DIRECTORY_SEPARATOR . 'composer.json';
if (is_file($composer)) {
$json = json_decode(file_get_contents($composer));
if (isset($json->config->{"bin-dir"})) {
return $path . DIRECTORY_SEPARATOR . $json->config->{"bin-dir"};
} elseif (is_dir($path . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'bin')) {
return $path . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'bin';
}
}
}
return null;
}
/**
* Set the buildPath property.
* @param string $path
*/
public function setBuildPath($path)
{
$this->buildPath = $path;
}
}