-
Notifications
You must be signed in to change notification settings - Fork 351
/
CommandExecutor.php
292 lines (254 loc) · 7.75 KB
/
CommandExecutor.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
<?php
namespace Oro\Bundle\EntityConfigBundle\Tools;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
use Oro\Bundle\InstallerBundle\Process\PhpExecutableFinder;
use Oro\Bundle\CacheBundle\Manager\OroDataCacheManager;
class CommandExecutor
{
const DEFAULT_TIMEOUT = 300;
/**
* @var string
*/
protected $consoleCmdPath;
/**
* @var string
*/
protected $env;
/**
* @var OroDataCacheManager
*/
protected $dataCacheManager;
/** @var array */
protected $defaultOptions;
/**
* @var int
*
* @deprecated since 1.8. Use {@see getDefaultOption('process-timeout')} instead
*/
protected $defaultTimeout = self::DEFAULT_TIMEOUT;
/**
* Constructor
*
* @param string $consoleCmdPath
* @param string $env
* @param OroDataCacheManager $dataCacheManager
*/
public function __construct(
$consoleCmdPath,
$env,
OroDataCacheManager $dataCacheManager = null
) {
$this->consoleCmdPath = $consoleCmdPath;
$this->env = $env;
$this->dataCacheManager = $dataCacheManager;
$this->defaultOptions = [
'process-timeout' => self::DEFAULT_TIMEOUT
];
}
/**
* Launches a command as a separate process.
*
* The '--process-timeout' parameter can be used to set the process timeout
* in seconds. Default timeout is 300 seconds.
* If '--ignore-errors' parameter is specified any errors are ignored;
* otherwise, an exception is raises if an error happened.
* If '--disable-cache-sync' parameter is specified a synchronization of caches between current
* process and its child processes are disabled.
*
* @param string $command
* @param array $params
* @param LoggerInterface|null $logger
*
* @return integer The exit status code
*
* @throws \RuntimeException if command failed and '--ignore-errors' parameter is not specified
*/
public function runCommand($command, $params = [], LoggerInterface $logger = null)
{
$params = $this->prepareParameters($command, $params);
$ignoreErrors = false;
if (array_key_exists('--ignore-errors', $params)) {
$ignoreErrors = true;
unset($params['--ignore-errors']);
}
$pb = new ProcessBuilder();
$pb
->add($this->getPhp())
->add($this->consoleCmdPath);
if (array_key_exists('--process-timeout', $params)) {
$pb->setTimeout($params['--process-timeout']);
unset($params['--process-timeout']);
}
$disableCacheSync = false;
if (array_key_exists('--disable-cache-sync', $params)) {
$disableCacheSync = $params['--disable-cache-sync'];
unset($params['--disable-cache-sync']);
}
foreach ($params as $name => $val) {
$this->processParameter($pb, $name, $val);
}
$process = $pb
->inheritEnvironmentVariables(true)
->getProcess();
if (!$logger) {
$logger = new NullLogger();
}
$exitCode = $process->run(
function ($type, $data) use ($logger) {
if ($type === Process::ERR) {
$logger->error($data);
} else {
$logger->info($data);
}
}
);
// synchronize all data caches
if ($this->dataCacheManager && !$disableCacheSync) {
$this->dataCacheManager->sync();
}
$this->processResult($exitCode, $ignoreErrors, $logger);
return $exitCode;
}
/**
* Gets the default value of a given option
*
* @param string $name
*
* @return mixed
*/
public function getDefaultOption($name)
{
return isset($this->defaultOptions[$name]) ? $this->defaultOptions[$name] : null;
}
/**
* Sets the default value of a given option
*
* @param string $name
* @param mixed $value
*
* @return self
*/
public function setDefaultOption($name, $value = true)
{
$this->defaultOptions[$name] = $value;
return $this;
}
/**
* @param int $exitCode
* @param bool $ignoreErrors
* @param LoggerInterface $logger
* @throws \RuntimeException
*/
protected function processResult($exitCode, $ignoreErrors, LoggerInterface $logger)
{
if (0 !== $exitCode) {
if ($ignoreErrors) {
$logger->warning(sprintf('The command terminated with an exit code: %u.', $exitCode));
} else {
throw new \RuntimeException(sprintf('The command terminated with an exit code: %u.', $exitCode));
}
}
}
/**
* @param string $command
* @param array $params
*
* @return array
*/
protected function prepareParameters($command, array $params)
{
$params = array_merge(
[
'command' => $command
],
$params
);
if ($this->env && $this->env !== 'dev') {
$params['--env'] = $this->env;
}
foreach ($this->defaultOptions as $name => $value) {
$paramName = '--' . $name;
if (!array_key_exists($paramName, $params)) {
$params[$paramName] = $value;
}
}
return $params;
}
/**
* @param ProcessBuilder $pb
* @param string $name
* @param array|string|null $value
*/
protected function processParameter(ProcessBuilder $pb, $name, $value)
{
if ($name && '-' === $name[0]) {
if ($value === true) {
$this->addParameter($pb, $name);
} elseif ($value !== false) {
$this->addParameter($pb, $name, $value);
}
} else {
$this->addParameter($pb, $value);
}
}
/**
* @param ProcessBuilder $pb
* @param string $name
* @param array|string|null $value
*/
protected function addParameter(ProcessBuilder $pb, $name, $value = null)
{
$parameters = array();
if (null !== $value) {
if (is_array($value)) {
foreach ($value as $item) {
$parameters[] = sprintf('%s=%s', $name, $item);
}
} else {
$parameters[] = sprintf('%s=%s', $name, $value);
}
} else {
$parameters[] = $name;
}
foreach ($parameters as $parameter) {
$pb->add($parameter);
}
}
/**
* Finds the PHP executable.
*
* @return string
* @throws FileNotFoundException
*/
protected function getPhp()
{
$phpFinder = new PhpExecutableFinder();
$phpPath = $phpFinder->find();
if (!$phpPath) {
throw new FileNotFoundException('The PHP executable could not be found.');
}
return $phpPath;
}
/**
* @return int
*
* @deprecated since 1.8. Use {@see getDefaultOption('process-timeout')} instead
*/
public function getDefaultTimeout()
{
return $this->getDefaultOption('process-timeout');
}
/**
* @param int $defaultTimeout
*
* @deprecated since 1.8. Use {@see setDefaultOption('process-timeout', $timeout)} instead
*/
public function setDefaultTimeout($defaultTimeout)
{
$this->setDefaultOption('process-timeout', $defaultTimeout);
}
}