-
Notifications
You must be signed in to change notification settings - Fork 0
/
CliCommand.php
426 lines (365 loc) · 11.9 KB
/
CliCommand.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
/**
* JBZoo Toolbox - Cli
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Cli
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @link https://github.com/JBZoo/Cli
*/
declare(strict_types=1);
namespace JBZoo\Cli;
use JBZoo\Utils\Arr;
use JBZoo\Utils\FS;
use JBZoo\Utils\Str;
use JBZoo\Utils\Vars;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use function JBZoo\Utils\bool;
use function JBZoo\Utils\float;
use function JBZoo\Utils\int;
/**
* Class CliCommand
* @package JBZoo\Cli
*/
abstract class CliCommand extends Command
{
/**
* @var Cli
* @psalm-suppress PropertyNotSetInConstructor
*/
protected $helper;
/**
* @inheritDoc
*/
protected function configure(): void
{
$this
->addOption('no-progress', null, InputOption::VALUE_NONE, "Disable progress bar animation for logs")
->addOption(
'mute-errors',
null,
InputOption::VALUE_NONE,
"Mute any sort of errors. So exit code will be always \"0\" (if it's possible).\n" .
"It has major priority then <info>--non-zero-on-error</info>. It's on your own risk!"
)
->addOption(
'stdout-only',
null,
InputOption::VALUE_NONE,
"For any errors messages application will use StdOut instead of StdErr. It's on your own risk!"
)
->addOption('non-zero-on-error', null, InputOption::VALUE_NONE, 'None-zero exit code on any StdErr message')
->addOption('timestamp', null, InputOption::VALUE_NONE, 'Show timestamp at the beginning of each message')
->addOption('profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information');
parent::configure();
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->helper = new Cli($input, $output);
$this->_('Working Directory is <i>' . \getcwd() . '</i>', OutLvl::DEBUG);
$exitCode = 0;
try {
$this->trigger('exec.before', [$this, $this->helper]);
\ob_start();
$exitCode = $this->executeAction();
if ($echoContent = \ob_get_clean()) {
$this->showLegacyOutput($echoContent);
}
} catch (\Exception $exception) {
if ($echoContent = \ob_get_clean()) {
$this->showLegacyOutput($echoContent);
}
$this->trigger('exception', [$this, $this->helper, $exception]);
if ($this->getOptBool('mute-errors')) {
$this->_($exception->getMessage(), OutLvl::EXCEPTION);
} else {
$this->showProfiler();
throw $exception;
}
}
$exitCode = Vars::range($exitCode, 0, 255);
if ($this->helper->isOutputHasErrors() && $this->getOptBool('non-zero-on-error')) {
$exitCode = 1;
}
$this->trigger('exec.after', [$this, $this->helper, &$exitCode]);
$this->showProfiler();
if ($this->getOptBool('mute-errors')) {
$exitCode = 0;
}
$this->_("Exit Code is \"{$exitCode}\"", OutLvl::DEBUG);
return $exitCode;
}
/**
* @return int
*/
abstract protected function executeAction(): int;
/**
* @param string $optionName
* @param bool $canBeArray
* @return mixed|null
*/
protected function getOpt(string $optionName, bool $canBeArray = true)
{
$value = $this->helper->getInput()->getOption($optionName);
if ($canBeArray && \is_array($value)) {
return Arr::last($value);
}
return $value;
}
/**
* @param string $optionName
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
protected function getOptBool(string $optionName): bool
{
$value = $this->getOpt($optionName);
return bool($value);
}
/**
* @param string $optionName
* @return int
*/
protected function getOptInt(string $optionName): int
{
$value = $this->getOpt($optionName) ?? 0;
return int($value);
}
/**
* @param string $optionName
* @return float
*/
protected function getOptFloat(string $optionName): float
{
$value = $this->getOpt($optionName) ?? 0.0;
return float($value);
}
/**
* @param string $optionName
* @return string
*/
protected function getOptString(string $optionName): string
{
$value = \trim((string)$this->getOpt($optionName));
$length = \strlen($value);
return $length > 0 ? $value : '';
}
/**
* @param string $optionName
* @return array
*/
protected function getOptArray(string $optionName): array
{
$list = $this->getOpt($optionName, false) ?? [];
return (array)$list;
}
/**
* @param string $optionName
* @param string $defaultDatetime
* @return \DateTimeImmutable
*/
protected function getOptDatetime(
string $optionName,
string $defaultDatetime = '1970-01-01 00:00:00'
): \DateTimeImmutable {
$dateAsString = $this->getOptString($optionName) ?: $defaultDatetime;
return new \DateTimeImmutable($dateAsString);
}
/**
* @return string|null
*/
protected static function getStdIn(): ?string
{
// It can be read only once, so we save result as internal varaible
static $result;
if (null === $result) {
$result = '';
while (!\feof(\STDIN)) {
$result .= \fread(\STDIN, 1024);
}
}
return $result;
}
/**
* Alias to write new line in std output
*
* @param string|array $messages
* @param string $verboseLevel
* @return void
*
* @SuppressWarnings(PHPMD.CamelCaseMethodName)
*/
protected function _($messages = '', string $verboseLevel = ''): void
{
$this->helper->_($messages, $verboseLevel);
}
/**
* @return bool
*/
protected function isInfoLevel(): bool
{
return $this->helper->getOutput()->isVerbose();
}
/**
* @return bool
*/
protected function isWarningLevel(): bool
{
return $this->helper->getOutput()->isVeryVerbose();
}
/**
* @return bool
*/
protected function isDebugLevel(): bool
{
return $this->helper->getOutput()->isDebug();
}
/**
* @return bool
*/
protected function isProfile(): bool
{
return $this->getOptBool('profile');
}
private function showProfiler(): void
{
if (!$this->isProfile()) {
return;
}
$totalTime = \number_format(\microtime(true) - $this->helper->getStartTime(), 3);
$curMemory = FS::format(\memory_get_usage(false));
$maxMemory = FS::format(\memory_get_peak_usage(true));
$this->_(\implode('; ', [
"Memory Usage/Peak: <green>{$curMemory}</green>/<green>{$maxMemory}</green>",
"Execution Time: <green>{$totalTime} sec</green>"
]));
}
/**
* @param string $eventName
* @param array $arguments
* @param callable|null $continueCallback
* @return int
*/
protected function trigger(string $eventName, array $arguments = [], ?callable $continueCallback = null): int
{
$application = $this->getApplication();
if (!$application) {
return 0;
}
if ($application instanceof CliApplication) {
$eManager = $application->getEventManager();
if (!$eManager) {
return 0;
}
return $eManager->trigger("jbzoo.cli.{$eventName}", $arguments, $continueCallback);
}
return 0;
}
/**
* @param string|null $echoContent
* @return void
*/
private function showLegacyOutput(?string $echoContent): void
{
$echoContent = $echoContent ?: '';
$lines = \explode("\n", $echoContent);
$lines = \array_map(static function ($line) {
return \rtrim($line);
}, $lines);
$lines = \array_filter($lines, static function ($line): bool {
return '' !== $line;
});
if (\count($lines) > 1) {
$this->_($lines, OutLvl::LEGACY);
} else {
$this->_($echoContent, OutLvl::LEGACY);
}
}
/**
* @param string $question
* @param string $default
* @param bool $isHidden
* @return string
*/
protected function ask(string $question, string $default = '', bool $isHidden = false): string
{
$question = \rtrim($question, ':');
$questionText = "<yellow-r>Question:</yellow-r> {$question}";
if (!$isHidden) {
$questionText .= ($default ? " (Default: <i>{$default}</i>)" : '');
}
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
$helper = $this->getHelper('question');
$questionObj = new Question($questionText . ': ', $default);
if ($isHidden) {
$questionObj->setHidden(true);
$questionObj->setHiddenFallback(false);
}
return (string)$helper->ask($this->helper->getInput(), $this->helper->getOutput(), $questionObj);
}
/**
* @param string $question
* @param bool $randomDefault
* @return string
*/
protected function askPassword(string $question, bool $randomDefault = true): string
{
$default = '';
if ($randomDefault) {
$question .= ' (Default: <i>Random</i>)';
$default = Str::random(10, false);
}
return $this->ask($question, $default, true);
}
/**
* @param string $question
* @param bool $default
* @return bool
*/
protected function confirmation(string $question = 'Are you sure?', bool $default = false): bool
{
$question = '<yellow-r>Question:</yellow-r> ' . \trim($question);
/** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
$helper = $this->getHelper('question');
$defaultValue = $default ? 'Y' : 'n';
$questionObj = new ConfirmationQuestion(
"{$question} (<c>Y/n</c>; Default: <i>{$defaultValue}</i>): ",
$default
);
return (bool)$helper->ask($this->helper->getInput(), $this->helper->getOutput(), $questionObj);
}
/**
* @param string $question
* @param string[] $options
* @param string|int|null $default
* @return string
*/
protected function askOption(string $question, array $options, $default = null): string
{
$question = '<yellow-r>Question:</yellow-r> ' . \trim($question);
$defaultValue = '';
if (null !== $default) {
$defaultValue = $options[$default] ?? $default ?: '';
if ('' !== $defaultValue) {
$defaultValue = " (Default: <i>{$defaultValue}</i>)";
}
}
$helper = $this->getHelper('question');
$questionObj = new ChoiceQuestion($question . $defaultValue . ': ', $options, $default);
$questionObj->setErrorMessage('The option "%s" is undefined. See the avaialable options');
return (string)$helper->ask($this->helper->getInput(), $this->helper->getOutput(), $questionObj);
}
}