forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.php
More file actions
342 lines (287 loc) · 8.54 KB
/
Copy pathController.php
File metadata and controls
342 lines (287 loc) · 8.54 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
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2016/12/7
* Time: 13:23
*/
namespace Inhere\Console;
use Inhere\Console\Base\AbstractCommand;
use Inhere\Console\Base\ControllerInterface;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Utils\Helper;
use Inhere\Console\Utils\Annotation;
/**
* Class Controller
* @package Inhere\Console
*/
abstract class Controller extends AbstractCommand implements ControllerInterface
{
/** @var string */
private $action;
/** @var string */
private $defaultAction = 'help';
/** @var string */
private $actionSuffix = 'Command';
/** @var string */
protected $notFoundCallback = 'notFound';
/** @var string */
protected $delimiter = ':'; // '/' ':'
/** @var bool */
private $standAlone = false;
/**
* @param string $command
* @return int
*/
public function run($command = '')
{
if (!$this->action = trim($command)) {
return $this->showHelp();
}
return parent::run($command);
}
/**
* load command configure
*/
protected function configure()
{
if ($action = $this->action) {
$method = $action . 'Configure';
if (method_exists($this, $method)) {
$this->$method();
}
}
}
/**
* 运行控制器的 action
* @param Input $input
* @param Output $output
* @return mixed
* @throws \ReflectionException
*/
protected function execute($input, $output)
{
$action = Helper::camelCase(trim($this->action ?: $this->defaultAction, $this->delimiter));
$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;
// the action method exists and only allow access public method.
if (method_exists($this, $method) && (($rfm = new \ReflectionMethod($this, $method)) && $rfm->isPublic())) {
// run action
$status = $this->$method($input, $output);
// if you defined the method '$this->notFoundCallback' , will call it
} elseif (($notFoundCallback = $this->notFoundCallback) && method_exists($this, $notFoundCallback)) {
$status = $this->{$notFoundCallback}($action);
} else {
$group = static::getName();
$status = -1;
$output->liteError("Sorry, The command '$action' not exist of the group '{$group}'!");
// find similar command names by similar_text()
$similar = [];
foreach ($this->getAllCommandMethods() as $cmd => $refM) {
similar_text($action, $cmd, $percent);
if (45 <= (int)$percent) {
$similar[] = $cmd;
}
}
if ($similar) {
$output->write(sprintf("\nMaybe what you mean is:\n <info>%s</info>", implode(', ', $similar)));
} else {
$this->showCommandList();
}
}
return $status;
}
/**
* @return int
* @throws \ReflectionException
*/
protected function showHelp()
{
if (true === parent::showHelp()) {
return 0;
}
return $this->helpCommand();
}
/**
* Show help of the controller command group or specified command action
* @usage <info>{name}/[command] -h</info> OR <info>{command} [command]</info> OR <info>{name} [command] -h</info>
* @example
* {script} {name} -h
* {script} {name}/help
* {script} {name}/help index
* {script} {name}/index -h
* {script} {name} index
*
* @return int
* @throws \ReflectionException
*/
final public function helpCommand()
{
$action = $this->action;
// show all commands of the controller
if (!$action && !($action = $this->input->getFirstArg())) {
$this->showCommandList();
return 0;
}
$action = Helper::camelCase($action);
$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;
// show help info for a command.
return $this->showHelpByMethodAnnotation($method, $action);
}
/**
* show command list of the controller class
* @throws \ReflectionException
*/
final public function showCommandList()
{
$ref = new \ReflectionClass($this);
$sName = lcfirst(self::getName() ?: $ref->getShortName());
if (!($classDes = self::getDescription())) {
$classDes = Annotation::description($ref->getDocComment()) ?: 'No Description for the console controller';
}
$commands = [];
foreach ($this->getAllCommandMethods($ref) as $cmd => $m) {
$desc = Annotation::firstLine($m->getDocComment());
if ($cmd) {
$commands[$cmd] = $desc;
}
}
// sort commands
ksort($commands);
// move 'help' to last.
if ($helpCmd = $commands['help'] ?? null) {
unset($commands['help']);
$commands['help'] = $helpCmd;
}
$script = $this->getScriptName();
if ($this->standAlone) {
$name = $sName . ' ';
$usage = "$script <info>{command}</info> [arguments] [options]";
} else {
$name = $sName . $this->delimiter;
$usage = "$script {$name}<info>{command}</info> [arguments] [options]";
}
$this->output->mList([
'Description:' => $classDes,
'Usage:' => $usage,
//'Group Name:' => "<info>$sName</info>",
'Commands:' => $commands,
'Options:' => [
'-h,--help' => 'Show help of the command group or specified command action',
],
]);
$this->write(sprintf(
"More information about a command, please use: <cyan>$script $name{command} -h</cyan>",
$this->standAlone ? ' ' . $name : ''
));
}
/**
* @param \ReflectionClass|null $ref
* @return \Generator
*/
protected function getAllCommandMethods(\ReflectionClass $ref = null)
{
$ref = $ref ?: new \ReflectionObject($this);
$suffix = $this->actionSuffix;
$suffixLen = Helper::strLen($suffix);
foreach ($ref->getMethods() as $m) {
$mName = $m->getName();
if ($m->isPublic() && substr($mName, - $suffixLen) === $suffix) {
// suffix is empty ?
$cmd = $suffix ? substr($mName, 0, -$suffixLen) : $mName;
yield $cmd => $m;
}
}
}
/**************************************************************************
* getter/setter methods
**************************************************************************/
/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}
/**
* @param string $action
* @return $this
*/
public function setAction(string $action)
{
if ($action) {
$this->action = Helper::camelCase($action);
}
return $this;
}
/**
* @return string
*/
public function getDefaultAction(): string
{
return $this->defaultAction;
}
/**
* @param string $defaultAction
*/
public function setDefaultAction($defaultAction)
{
$this->defaultAction = $defaultAction;
}
/**
* @return string
*/
public function getActionSuffix(): string
{
return $this->actionSuffix;
}
/**
* @param string $actionSuffix
*/
public function setActionSuffix($actionSuffix)
{
$this->actionSuffix = $actionSuffix;
}
/**
* @return string
*/
public function getNotFoundCallback()
{
return $this->notFoundCallback;
}
/**
* @param string $notFoundCallback
*/
public function setNotFoundCallback($notFoundCallback)
{
$this->notFoundCallback = $notFoundCallback;
}
/**
* @return bool
*/
public function isStandAlone(): bool
{
return $this->standAlone;
}
/**
* @param bool $standAlone
*/
public function setStandAlone($standAlone = true)
{
$this->standAlone = (bool)$standAlone;
}
/**
* @return string
*/
public function getDelimiter(): string
{
return $this->delimiter;
}
/**
* @param string $delimiter
*/
public function setDelimiter(string $delimiter)
{
$this->delimiter = $delimiter;
}
}