-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathCommand.php
153 lines (134 loc) · 3.31 KB
/
Command.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
<?php declare(strict_types=1);
/**
* The file is part of inhere/console
*
* @author https://github.com/inhere
* @homepage https://github.com/inhere/php-console
* @license https://github.com/inhere/php-console/blob/master/LICENSE
*/
namespace Inhere\Console;
use Inhere\Console\Contract\CommandInterface;
use Inhere\Console\Handler\AbstractHandler;
use ReflectionException;
use Toolkit\PFlag\FlagsParser;
use function array_shift;
/**
* Class Command
*
* @package Inhere\Console
*
* ```php
* class MyCommand extends Command
* {
* protected function execute(Input $input, Output $output)
* {
* // some logic ...
* }
* }
* ```
*/
abstract class Command extends AbstractHandler implements CommandInterface
{
public const METHOD = 'execute';
/**
* @var Controller|null
*/
protected ?Controller $group = null;
/**
* command argument rules
*
* eg:
*
* [
* 'arg1' => 'type;desc',
* ]
*
* @return array
*/
protected function getArguments(): array
{
return [];
}
/**
* @param FlagsParser $fs
*/
protected function beforeInitFlagsParser(FlagsParser $fs): void
{
$fs->addArgsByRules($this->getArguments());
// $fs->setStopOnFistArg(false);
}
/**
* @param FlagsParser $fs
*
* @throws ReflectionException
*/
protected function afterInitFlagsParser(FlagsParser $fs): void
{
$this->debugf('cmd: %s - load command flags configure, class: %s', $this->getRealCName(), static::class);
$this->configure();
$this->configFlags($fs);
$isEmpty = $this->flags->isEmpty();
// load built in options
$fs->addOptsByRules(GlobalOption::getAloneOptions());
// not config flags. load rules from method doc-comments
if ($isEmpty) {
$this->loadRulesByDocblock(self::METHOD, $fs);
}
}
/**
* @param array $args
*
* @return mixed
*/
protected function doRun(array $args): mixed
{
// if input sub-command name
if (isset($args[0])) {
$first = $args[0];
$rName = $this->resolveAlias($first);
if ($this->isSub($rName)) {
array_shift($args);
return $this->dispatchSub($rName, $args);
}
}
return parent::doRun($args);
}
/**
* Show help information
*
* @return bool
*/
protected function showHelp(): bool
{
$aliases = $this->getAliases();
$this->logf(Console::VERB_CRAZY, 'display help info for the command: %s', $this->commandName);
// $execMethod = self::METHOD;
// return $this->showHelpByAnnotations($execMethod, '', $aliases) !== 0;
return $this->showHelpByFlagsParser($this->flags, $aliases) !== 0;
}
/**
* @return string
*/
public function getRealCName(): string
{
return self::getName();
}
/**
* Get the group
*
* @return Controller|null
*/
public function getGroup(): ?Controller
{
return $this->group;
}
/**
* Set the value of group
*
* @param Controller $group
*/
public function setGroup(Controller $group): void
{
$this->group = $group;
}
}