-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathApplication.php
More file actions
501 lines (413 loc) · 12.1 KB
/
Application.php
File metadata and controls
501 lines (413 loc) · 12.1 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
/*
* This file is part of the PHP-CLI package.
*
* (c) Jitendra Adhikari <jiten.adhikary@gmail.com>
* <https://github.com/adhocore>
*
* Licensed under MIT license.
*/
namespace Ahc\Cli;
use Ahc\Cli\Exception\InvalidArgumentException;
use Ahc\Cli\Helper\OutputHelper;
use Ahc\Cli\Input\Command;
use Ahc\Cli\IO\Interactor;
use ReflectionClass;
use ReflectionFunction;
use Throwable;
use function array_diff_key;
use function array_fill_keys;
use function array_keys;
use function count;
use function func_num_args;
use function in_array;
use function is_array;
use function is_int;
use function method_exists;
/**
* A cli application.
*
* @author Jitendra Adhikari <jiten.adhikary@gmail.com>
* @license MIT
*
* @link https://github.com/adhocore/cli
*/
class Application
{
/**
* Locale of CLI.
*/
public static $locale = 'en';
/**
* list of translations for each supported locale.
*
* @var array<string, array>
*
* @example
* ```php
* ['locale' => ['key1' => 'value1', 'key2' => 'value2']]
* ```
*/
public static $locales = [];
/** @var Command[] */
protected array $commands = [];
/** @var array Raw argv sent to parse() */
protected array $argv = [];
/** @var array Command aliases [alias => cmd] */
protected array $aliases = [];
/** @var string Ascii art logo */
protected string $logo = '';
/** @var string Custom help screen */
protected string $help = '';
/** @var string Name of default command */
protected string $default = '__default__';
/** @var null|Interactor */
protected ?Interactor $io = null;
/** @var callable The callable to perform exit */
protected $onExit;
/** @var callable The callable to catch exception, receives exception & exit code, may rethrow exception or may exit program */
protected $onException = null;
public function __construct(protected string $name, protected string $version = '0.0.1', ?callable $onExit = null)
{
$this->onExit = $onExit ?? static fn (int $exitCode = 0) => exit($exitCode);
$this->command('__default__', 'Default command', '', true)->on([$this, 'showHelp'], 'help');
}
/**
* Get the name.
*/
public function name(): string
{
return $this->name;
}
/**
* Get the version.
*/
public function version(): string
{
return $this->version;
}
/**
* Get the commands.
*
* @return Command[]
*/
public function commands(): array
{
$commands = $this->commands;
unset($commands['__default__']);
return $commands;
}
/**
* Get the raw argv.
*/
public function argv(): array
{
return $this->argv;
}
/**
* Sets or gets the ASCII art logo.
*
* @param string|null $logo
*
* @return string|self
*/
public function logo(?string $logo = null)
{
if (func_num_args() === 0) {
return $this->logo;
}
$this->logo = $logo;
return $this;
}
public static function addLocale(string $locale, array $texts, bool $default = false)
{
if ($default) {
self::$locale = $locale;
}
self::$locales[$locale] = $texts;
}
/**
* Add a command by its name desc alias etc and return command.
*/
public function command(
string $name,
string $desc = '',
string $alias = '',
bool $allowUnknown = false,
bool $default = false
): Command {
$command = new Command($name, $desc, $allowUnknown, $this);
$this->add($command, $alias, $default);
return $command;
}
/**
* Add a prepared command and return itself.
*/
public function add(Command $command, string $alias = '', bool $default = false): self
{
$name = $command->name();
if (
$this->commands[$name] ??
$this->aliases[$name] ??
$this->commands[$alias] ??
$this->aliases[$alias] ??
null
) {
throw new InvalidArgumentException(t('Command "%s" already added', [$name]));
}
if ($alias) {
$command->alias($alias);
$this->aliases[$alias] = $name;
}
if ($default) {
$this->default = $name;
}
$this->commands[$name] = $command->version($this->version)->onExit($this->onExit)->bind($this);
if (method_exists($command, "getSubCommands")) {
foreach ($command->getSubCommands() as $subcommand) {
$this->add($subcommand);
}
}
return $this;
}
/**
* Set the default command.
*
* @param string $commandName The name of the default command
*
* @throws InvalidArgumentException If the specified command name does not exist
*
* @return self The application
*/
public function defaultCommand(string $commandName): self
{
if (!isset($this->commands[$commandName])) {
throw new InvalidArgumentException(t('Command "%s" does not exist', [$commandName]));
}
$this->default = $commandName;
return $this;
}
/**
* Get the default command.
*
* @return string|null The name of the default command, or null if not set
*/
public function getDefaultCommand(): ?string
{
return $this->default;
}
/**
* Groups commands set within the callable.
*
* @param string $group The group name
* @param callable $fn The callable that recieves Application instance and adds commands.
*
* @return self
*/
public function group(string $group, callable $fn): self
{
$old = array_fill_keys(array_keys($this->commands), true);
$fn($this);
foreach (array_diff_key($this->commands, $old) as $cmd) {
$cmd->inGroup($group);
}
return $this;
}
/**
* Gets matching command for given argv.
*/
public function commandFor(array $argv): Command
{
$argv += [null, null, null];
$command = null;
//sort by key length
//find maximal command compatibility by arguments
$keys = array_map('strlen', array_keys($this->commands));
array_multisort($keys, SORT_ASC, $this->commands);
foreach ($this->commands as $command_name => $value) {
$nb_args = count(explode(' ', $command_name));
$args = implode(' ', array_slice($argv, 1, $nb_args));
if (!empty($this->commands[$args])) {
$command = $this->commands[$args];
}
}
return
//cmd found by multi arguments
$command
// cmd
?? $this->commands[$argv[1]]
// cmd alias
?? $this->commands[$this->aliases[$argv[1]] ?? null]
// default.
?? $this->commands[$this->default];
}
/**
* Gets or sets io.
*
* @param Interactor|null $io
*
* @return Interactor|self
*/
public function io(?Interactor $io = null)
{
if ($io || !$this->io) {
$this->io = $io ?? new Interactor;
}
if (func_num_args() === 0) {
return $this->io;
}
return $this;
}
/**
* Parse the arguments via the matching command but dont execute action..
*
* @param array $argv Cli arguments/options.
*
* @return Command The matched and parsed command (or default)
*/
public function parse(array $argv): Command
{
$this->argv = $argv;
$command = $this->commandFor($argv);
$aliases = $this->aliasesFor($command);
// Eat the cmd name!
foreach ($argv as $i => $arg) {
if (in_array($arg, $aliases)) {
unset($argv[$i]);
break;
}
if ($arg[0] === '-') {
break;
}
}
return $command->parse($argv);
}
/**
* Sets exception handler callback.
*
* The callback receives exception & exit code. It may rethrow exception
* or may exit the program or just log exception and do nothing else.
*/
public function onException(callable $fn): self
{
$this->onException = $fn;
return $this;
}
/**
* Handle the request, invoke action and call exit handler.
*/
public function handle(array $argv): mixed
{
if ($this->default === '__default__' && count($argv) < 2) {
return $this->showHelp();
}
$exitCode = 255;
try {
$command = $this->parse($argv);
$result = $this->doAction($command);
$exitCode = is_int($result) ? $result : 0;
} catch (Throwable $e) {
isset($this->onException) && ($this->onException)($e, $exitCode);
$this->outputHelper()->printTrace($e);
}
return ($this->onExit)($exitCode);
}
/**
* Get aliases for given command.
*/
protected function aliasesFor(Command $command): array
{
$aliases = [$name = $command->name()];
foreach ($this->aliases as $alias => $cmd) {
if (in_array($name, [$alias, $cmd], true)) {
$aliases[] = $alias;
$aliases[] = $cmd;
}
}
return $aliases;
}
/**
* Sets or gets the custom help screen contents.
*
* @param string|null $help
*
* @return string|self
*/
public function help(?string $help = null): mixed
{
if (func_num_args() === 0) {
return $this->help;
}
$this->help = $help;
return $this;
}
/**
* Show custom help screen if one is set, otherwise shows the default one.
*/
public function showHelp(): mixed
{
if ($help = $this->help()) {
$writer = $this->io()->writer();
$writer->write($help, true);
return ($this->onExit)();
}
return $this->showDefaultHelp();
}
/**
* Shows command help then aborts.
*/
public function showDefaultHelp(): mixed
{
$writer = $this->io()->writer();
$header = "{$this->name}, " . t('version') . " {$this->version}";
$footer = t('Run `<command> --help` for specific help');
if ($this->logo) {
$writer->logo($this->logo, true);
}
$this->outputHelper()->showCommandsHelp($this->commands(), $header, $footer);
return ($this->onExit)();
}
protected function outputHelper(): OutputHelper
{
$writer = $this->io()->writer();
return new OutputHelper($writer);
}
/**
* Invoke command action.
*/
protected function doAction(Command $command): mixed
{
if ($command->name() === '__default__') {
return $this->notFound();
}
// Let the command collect more data (if missing or needs confirmation)
$command->interact($this->io());
if (!$command->action() && !method_exists($command, 'execute')) {
return null;
}
$params = [];
$values = $command->values();
// We prioritize action to be in line with commander.js!
$action = $command->action() ?? [$command, 'execute'];
foreach ($this->getActionParameters($action) as $param) {
$params[] = $values[$param->getName()] ?? null;
}
return $action(...$params);
}
/**
* Command not found handler.
*/
protected function notFound(): mixed
{
$available = array_keys($this->commands() + $this->aliases);
$this->outputHelper()->showCommandNotFound($this->argv[1], $available);
return ($this->onExit)(127);
}
protected function getActionParameters(callable $action): array
{
$reflex = is_array($action)
? (new ReflectionClass($action[0]))->getMethod($action[1])
: new ReflectionFunction($action);
return $reflex->getParameters();
}
}