/
Runner.php
270 lines (229 loc) · 7.83 KB
/
Runner.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
<?php declare(strict_types=1);
/*
* This file is part of Phive.
*
* Copyright (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de> and contributors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PharIo\Phive\Cli;
use const DEBUG_BACKTRACE_IGNORE_ARGS;
use function count;
use function debug_backtrace;
use function dirname;
use function error_get_last;
use function error_reporting;
use function explode;
use function file_get_contents;
use function implode;
use function ini_set;
use function is_array;
use function register_shutdown_function;
use function set_error_handler;
use function sprintf;
use function strlen;
use function substr;
use PharIo\Phive\Environment;
use PharIo\Phive\ErrorException;
use PharIo\Phive\Exception;
use PharIo\Phive\FeatureMissingException;
use PharIo\Phive\MigrationService;
use PharIo\Phive\MigrationsFailedException;
use PharIo\Phive\PhiveContext;
use PharIo\Phive\PhiveVersion;
use Throwable;
class Runner {
public const RC_OK = 0;
// OUTDATED PHP VERSION = 1
public const RC_EXT_MISSING = 2;
public const RC_UNKNOWN_COMMAND = 3;
public const RC_ERROR = 4;
public const RC_PARAM_ERROR = 5;
public const RC_MIGRATION_FAILED = 6;
public const RC_BUG_FOUND = 10;
// HHVM_RUNTIME = 99
/** @var CommandLocator */
private $locator;
/** @var Output */
private $output;
/** @var PhiveVersion */
private $version;
/** @var Environment */
private $environment;
/** @var Request */
private $request;
/** @var MigrationService */
private $migrationService;
public function __construct(
CommandLocator $locator,
Output $output,
PhiveVersion $version,
Environment $env,
MigrationService $migrationService,
Request $request
) {
$this->locator = $locator;
$this->output = $output;
$this->version = $version;
$this->environment = $env;
$this->request = $request;
$this->migrationService = $migrationService;
}
public function run(): int {
try {
$this->ensureFitness();
$this->setupRuntime();
$this->execute();
return self::RC_OK;
} catch (RunnerException $e) {
$this->showException($e);
return (int)$e->getCode();
} catch (ErrorException $e) {
$this->showErrorWithTrace($e->getMessage(), $e->getFile(), $e->getLine(), $e->getTrace());
return self::RC_BUG_FOUND;
} catch (Exception $e) {
$this->showException($e);
return self::RC_ERROR;
} catch (\Exception $e) {
$this->showErrorWithTrace($e->getMessage(), $e->getFile(), $e->getLine(), $e->getTrace());
return self::RC_BUG_FOUND;
} catch (Throwable $t) {
$this->showErrorWithTrace($t->getMessage(), $t->getFile(), $t->getLine(), $t->getTrace());
return self::RC_BUG_FOUND;
}
}
/**
* @throws ErrorException
*/
public function errorHandler(int $code, string $message, string $file, int $line): bool {
throw new ErrorException($message, $code, 1, $file, $line);
}
public function shutdownHandler(): void {
$error = error_get_last();
if ($error === null || $error['file'] === 'Unknown') {
return;
}
$this->showErrorWithTrace($error['message'], $error['file'], $error['line'], debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
}
/**
* @param array<int, array<string, mixed>> $trace
*/
private function showErrorWithTrace(string $error, string $file, int $line, ?array $trace = null): void {
$baseLen = strlen(dirname(__DIR__, 3) . '') + 1;
$message = [$error];
$message[] = '';
$message[] = sprintf(
'#0 %s(%d)',
substr($file, $baseLen),
$line
);
if (is_array($trace)) {
foreach ($trace as $pos => $step) {
$file = 'unknown file';
if (isset($step['file'])) {
$file = substr($step['file'], $baseLen);
}
$message[] = sprintf(
'#%d %s(%d): %s%s%s()',
$pos + 1,
$file,
$step['line'] ?? 0,
$step['class'] ?? '',
$step['type'] ?? '',
$step['function']
);
}
$message[] = sprintf('#%d {main}', count($trace) + 1);
}
$this->output->writeError(
sprintf(
file_get_contents(__DIR__ . '/error.txt'),
implode("\n ", $message),
$this->environment->getRuntimeString(),
$this->version->getVersion()
)
);
}
private function setupRuntime(): void {
error_reporting(-1);
ini_set('display_errors', 'off');
set_error_handler([$this, 'errorHandler']);
register_shutdown_function([$this, 'shutdownHandler']);
}
private function showHeader(): void {
$this->output->writeText($this->version->getVersionString() . "\n");
}
private function showFooter(): void {
$this->output->writeText("\n");
}
private function ensureFitness(): void {
try {
$this->environment->ensureFitness();
$this->migrationService->ensureFitness();
} catch (FeatureMissingException $e) {
throw new RunnerException(
sprintf(
"Your environment is not ready to run phive due to the following reason(s):\n\n- %s\n",
implode("\n- ", $e->getMissing())
),
self::RC_EXT_MISSING
);
} catch (MigrationsFailedException $failedException) {
throw new RunnerException(
sprintf(
"Phive is not ready to run due to the following failed migration(s):\n\n %s\n",
implode("\n ", $failedException->getFailed())
),
self::RC_MIGRATION_FAILED
);
}
}
private function parseRequest(): string {
try {
$options = $this->request->parse(new PhiveContext());
if ($options->hasArgument(0)) {
return $options->getArgument(0);
}
return '';
} catch (RequestException $e) {
throw new RunnerException(
$e->getMessage(),
self::RC_PARAM_ERROR
);
}
}
private function execute(): void {
$command = $this->parseRequest();
try {
$this->showHeader();
$this->migrationService->runMandatory();
$this->locator->getCommand($command)->execute();
$this->showFooter();
} catch (CommandLocatorException $e) {
if ($e->getCode() === CommandLocatorException::UnknownCommand) {
throw new RunnerException(
sprintf("Unknown command '%s'", $command),
self::RC_UNKNOWN_COMMAND
);
}
throw $e;
} catch (RequestException $e) {
throw new RunnerException(
sprintf(
"Error while processing arguments to command '%s':\n%s",
$command,
$e->getMessage()
),
self::RC_PARAM_ERROR
);
}
}
private function showException(Throwable $e): void {
foreach (explode("\n", $e->getMessage()) as $line) {
$this->output->writeError($line);
}
$this->showFooter();
}
}