-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommand.php
More file actions
517 lines (486 loc) · 13.2 KB
/
Copy pathCommand.php
File metadata and controls
517 lines (486 loc) · 13.2 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php declare(strict_types=1);
/*
* This file is part of Webisters CLI Library.
*
* (c) Hafiz Muhammad Moaz <thewebisters@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Framework\CLI;
use JetBrains\PhpStorm\Pure;
/**
* Class Command.
*
* @package cli
*/
abstract class Command
{
/**
* Console instance of the current command.
*/
protected Console $console;
/**
* Command name.
*/
protected string $name;
/**
* Command group.
*/
protected string $group;
/**
* Command description.
*/
protected string $description;
/**
* Command aliases.
*
* @var array<int,string>
*/
protected array $aliases = [];
/**
* Command usage.
*/
protected string $usage = 'command [options] -- [arguments]';
/**
* Command options.
*
* @var array<string,string>
*/
protected array $options = [];
/**
* Argument definitions.
*
* @var array<int,array<string,mixed>> Definitions keyed by position, each
* with optional "name", "type", "required", "default" and "description" keys
*/
protected array $argumentDefinitions = [];
/**
* Option definitions.
*
* @var array<string,array<string,mixed>> Definitions keyed by option name,
* each with optional "type" ("string", "int", "float", "numeric" or
* "flag"), "required" and "default" keys. Only options declared as
* "flag" accept being passed without a value
*/
protected array $optionDefinitions = [];
/**
* Tells if command is active.
*/
protected bool $active = true;
/**
* The exit code the command wants the process to use.
*/
protected int $exitCode = 0;
/**
* Command constructor.
*
* @param Console|null $console
*/
public function __construct(?Console $console = null)
{
if ($console) {
$this->console = $console;
}
}
/**
* Run the command.
*/
abstract public function run() : void;
/**
* Get console instance.
*
* @return Console
*/
public function getConsole() : Console
{
return $this->console;
}
/**
* Set console instance.
*
* @param Console $console
*
* @return static
*/
public function setConsole(Console $console) : static
{
$this->console = $console;
return $this;
}
/**
* Get command name.
*
* @return string
*/
public function getName() : string
{
if (isset($this->name)) {
return $this->name;
}
$name = static::class;
$pos = \strrpos($name, '\\');
if ($pos !== false) {
$name = \substr($name, $pos + 1);
}
if (\str_ends_with($name, 'Command')) {
$name = \substr($name, 0, -7);
}
$name = \strtolower($name);
return $this->name = $name;
}
/**
* Set command name.
*
* @param string $name
*
* @return static
*/
public function setName(string $name) : static
{
$this->name = $name;
return $this;
}
/**
* Get command group.
*
* @return string|null
*/
public function getGroup() : ?string
{
return $this->group ?? null;
}
/**
* Set command group.
*
* @param string $group
*
* @return static
*/
public function setGroup(string $group) : static
{
$this->group = $group;
return $this;
}
/**
* Get command description.
*
* @return string
*/
public function getDescription() : string
{
if (isset($this->description)) {
return $this->description;
}
$description = $this->console->getLanguage()->render('cli', 'noDescription');
return $this->description = $description;
}
/**
* Set command description.
*
* @param string $description
*
* @return static
*/
public function setDescription(string $description) : static
{
$this->description = $description;
return $this;
}
/**
* Get command aliases.
*
* @return array<int,string>
*/
#[Pure]
public function getAliases() : array
{
return $this->aliases;
}
/**
* Set command aliases.
*
* @param array<int,string> $aliases
*
* @return static
*/
public function setAliases(array $aliases) : static
{
$this->aliases = \array_values($aliases);
return $this;
}
/**
* Get command usage.
*
* @return string
*/
#[Pure]
public function getUsage() : string
{
return $this->usage;
}
/**
* Set command usage.
*
* @param string $usage
*
* @return static
*/
public function setUsage(string $usage) : static
{
$this->usage = $usage;
return $this;
}
/**
* Get command options.
*
* @return array<string,string>
*/
#[Pure]
public function getOptions() : array
{
return $this->options;
}
/**
* Set command options.
*
* @param array<string,string> $options
*
* @return static
*/
public function setOptions(array $options) : static
{
$this->options = $options;
return $this;
}
/**
* Get argument definitions.
*
* @return array<int,array<string,mixed>> Definitions keyed by position,
* each with optional "type", "required" and "default" keys
*/
#[Pure]
public function getArgumentDefinitions() : array
{
return $this->argumentDefinitions;
}
/**
* Set argument definitions.
*
* @param array<int,array<string,mixed>> $definitions Definitions keyed by
* position, each with optional "type", "required" and "default" keys
*
* @return static
*/
public function setArgumentDefinitions(array $definitions) : static
{
$this->argumentDefinitions = $definitions;
return $this;
}
/**
* Get option definitions.
*
* @return array<string,array<string,mixed>> Definitions keyed by option
* name, each with optional "type", "required" and "default" keys
*/
#[Pure]
public function getOptionDefinitions() : array
{
return $this->optionDefinitions;
}
/**
* Set option definitions.
*
* @param array<string,array<string,mixed>> $definitions Definitions keyed
* by option name, each with optional "type", "required" and "default" keys
*
* @return static
*/
public function setOptionDefinitions(array $definitions) : static
{
$this->optionDefinitions = $definitions;
return $this;
}
/**
* Validate parsed arguments and options against the definitions.
*
* Supported types are "string", "int", "float" and "numeric". An argument
* or option marked as required must be present. Values declared with a
* type are cast when possible and reported as errors when they do not
* match.
*
* @param array<int,string> $arguments The parsed positional arguments
* @param array<string,bool|string> $options The parsed options
*
* @return array<int,string> The validation error messages, empty when valid
*/
public function validate(array $arguments, array $options) : array
{
return \array_merge(
$this->validateDefinitions($this->argumentDefinitions, $arguments, 'argument'),
$this->validateDefinitions($this->optionDefinitions, $options, 'option')
);
}
/**
* Apply definition defaults to the console.
*
* Any defined argument or option that was not passed on the command line
* and declares a "default" key gets its default written back into the
* Console, so getArgument() and getOption() return the resolved value.
* Values are stored as strings (or booleans for flags) because the
* Console accessors are typed that way.
*
* @param Console $console The console the command is running on
*/
public function applyDefaults(Console $console) : void
{
$arguments = $console->getArguments();
foreach ($this->argumentDefinitions as $key => $definition) {
$default = $definition['default'] ?? null;
if (!\array_key_exists($key, $arguments) && \is_scalar($default)) {
$console->setArgument((int) $key, (string) $default);
}
}
$options = $console->getOptions();
foreach ($this->optionDefinitions as $key => $definition) {
$default = $definition['default'] ?? null;
if (\array_key_exists($key, $options) || !\is_scalar($default)) {
continue;
}
$console->setOption((string) $key, \is_bool($default) ? $default : (string) $default);
}
}
/**
* Validate a set of values against their definitions.
*
* @param array<int|string,array<string,mixed>> $definitions
* @param array<int|string,bool|string> $values
* @param string $label Either "argument" or "option", used in messages
*
* @return array<int,string>
*/
protected function validateDefinitions(array $definitions, array $values, string $label) : array
{
$errors = [];
$translator = isset($this->console) ? $this->console->getLanguage() : null;
if ($translator) {
$label = $translator->render('cli', $label, []);
}
foreach ($definitions as $key => $definition) {
$displayName = $this->definitionLabel($key, $definition);
$value = $values[$key] ?? null;
if ($value === null || $value === false) {
if (!empty($definition['required'])) {
$errors[] = $translator
? $translator->render('cli', 'validation.required', [$label, $displayName])
: $label . ' "' . $displayName . '" is required.';
}
continue;
}
$type = $definition['type'] ?? 'string';
if (!\is_string($type)) {
$type = 'string';
}
if ($type === 'flag') {
continue;
}
if (!\is_string($value) || $type === 'string') {
if (!\is_string($value)) {
// A boolean (true) means the option was passed without a
// value, which is only valid for "flag" definitions
$errors[] = $translator
? $translator->render('cli', 'validation.type', [$label, $displayName, $type])
: $label . ' "' . $displayName . '" must be of type ' . $type . '.';
}
continue;
}
$valid = match ($type) {
'int' => (bool) \preg_match('/^-?\d+$/', $value),
'float' => \is_numeric($value),
'numeric' => \is_numeric($value),
default => true,
};
if (!$valid) {
$errors[] = $translator
? $translator->render('cli', 'validation.type', [$label, $displayName, $type])
: $label . ' "' . $displayName . '" must be of type ' . $type . '.';
}
}
return $errors;
}
/**
* Resolve the display label of a definition: its "name" when given,
* otherwise the array key (the argument position or option name).
*
* @param int|string $key The definition key
* @param array<string,mixed> $definition The definition
*
* @return string
*/
#[Pure]
protected function definitionLabel(int | string $key, array $definition) : string
{
$name = $definition['name'] ?? null;
if (\is_string($name) && $name !== '') {
return $name;
}
return (string) $key;
}
/**
* Get the exit code the command wants the process to use.
*
* Returns 0 unless the command called setExitCode() during run().
*
* @return int
*/
#[Pure]
public function getExitCode() : int
{
return $this->exitCode;
}
/**
* Set the exit code the process should use after this command runs.
*
* Lets a command fail, or succeed with a specific code, without writing
* to STDERR or terminating the process itself.
*
* @param int $code The exit code, 0 means success
*
* @return static
*/
public function setExitCode(int $code) : static
{
$this->exitCode = $code;
return $this;
}
/**
* Tells if the command is active.
*
* @return bool
*/
#[Pure]
public function isActive() : bool
{
return $this->active;
}
/**
* Activate the command.
*
* @return static
*/
public function activate() : static
{
$this->active = true;
return $this;
}
/**
* Deactivate the command.
*
* @return static
*/
public function deactivate() : static
{
$this->active = false;
return $this;
}
}