-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathApplication.php
455 lines (392 loc) · 13.6 KB
/
Application.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
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
<?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 Closure;
use Inhere\Console\Component\Router;
use Inhere\Console\Contract\CommandInterface;
use Inhere\Console\Contract\ControllerInterface;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Console\Util\Helper;
use InvalidArgumentException;
use RuntimeException;
use Throwable;
use Toolkit\FsUtil\Dir;
use Toolkit\PFlag\FlagsParser;
use Toolkit\PFlag\SFlags;
use Toolkit\Stdlib\Helper\Assert;
use Toolkit\Stdlib\Helper\DataHelper;
use Toolkit\Stdlib\Str;
use function array_shift;
use function array_unshift;
use function class_exists;
use function implode;
use function is_dir;
use function is_object;
use function is_string;
use function str_replace;
use function strlen;
use function strpos;
use function substr;
use function trim;
/**
* Class App
*
* @package Inhere\Console
*/
class Application extends AbstractApplication
{
/**
* Class constructor.
*
* @param array $config
* @param Input|null $input
* @param Output|null $output
*/
public function __construct(array $config = [], ?Input $input = null, ?Output $output = null)
{
Console::setApp($this);
parent::__construct($config, $input, $output);
}
/****************************************************************************
* Dispatch and run console controller/command
****************************************************************************/
/**
* @param string $name command name or command ID or command path.
* @param array $args
*
* @return mixed
* @throws Throwable
*/
public function dispatch(string $name, array $args = []): mixed
{
if (!$name = trim($name)) {
throw new InvalidArgumentException('cannot dispatch an empty command');
}
$cmdId = $name;
$this->debugf('app - begin dispatch the input command: "%s", args: %s', $name, DataHelper::toString($args));
// format is: `group action` or `top sub sub2`
if (strpos($name, ' ') > 0) {
$names = Str::splitTrimmed($name, ' ');
$cmdId = array_shift($names);
// prepend elements to the beginning of $args
array_unshift($args, ...$names);
}
// match handler by input name
$info = $this->router->match($cmdId);
// command not found
if (!$info) {
$evtName = ConsoleEvent::ON_NOT_FOUND;
if (true === $this->fire($evtName, $cmdId, $this)) {
$this->debugf('user custom handle the not found command: %s, event: %s', $name, $evtName);
return 0;
}
$commands = $this->router->getAllNames();
$this->output->error("The command '$name' is not exists!");
// find similar command names by similar_text()
if ($similar = Helper::findSimilar($name, $commands)) {
$this->output->printf("\nMaybe what you mean is:\n <info>%s</info>", implode(', ', $similar));
} else {
// $this->showCommandList();
$scriptName = $this->getScriptName();
$this->output->colored("\nPlease use '$scriptName --help' for see all available commands");
}
return 2;
}
// save command ID
$cmdConf = $info['config'];
unset($info['config']);
$this->input->setCommandId($info['cmdId']);
// is command
if ($info['type'] === Router::TYPE_SINGLE) {
return $this->runCommand($info, $cmdConf, $args);
}
// is controller/group
return $this->runAction($info, $cmdConf, $args);
}
/**
* run a independent command
*
* @param array{name: string, handler: mixed, realName: string} $info
* @param array{aliases: array, desc: string, options: array, arguments: array} $config The config.
* @param array $args
*
* @return mixed
* @throws Throwable
*/
protected function runCommand(array $info, array $config, array $args): mixed
{
/** @var Closure|class-string{Command} $handler Command class or handler func */
$handler = $info['handler'];
$iptName = $info['name'];
if (is_object($handler)) {
// Command object
if ($handler instanceof Command) {
$handler->setInputOutput($this->input, $this->output);
$result = $handler->run($args);
} else { // Closure
$fs = SFlags::new();
$fs->setName($iptName);
$fs->addOptsByRules(GlobalOption::getAloneOptions());
// command flags load
if ($cmdOpts = $config['options'] ?? null) {
$fs->addOptsByRules($cmdOpts);
}
if ($cmdArgs = $config['arguments'] ?? null) {
$fs->addArgsByRules($cmdArgs);
}
$fs->setDesc($config['desc'] ?? 'No command description message');
// save to input object
$this->input->setFs($fs);
if (!$fs->parse($args)) {
return 0; // render help
}
$result = $handler($fs, $this->output);
}
} else {
Assert::isTrue(class_exists($handler), "The console command class [$handler] not exists!");
/** @var $cmd Command */
$cmd = new $handler($this->input, $this->output);
Assert::isTrue($cmd instanceof Command, "Command class [$handler] must instanceof the " . Command::class);
$cmd::setName($info['cmdId']); // real command name.
$cmd->setApp($this);
$cmd->setCommandName($iptName);
$result = $cmd->run($args);
}
return $result;
}
/**
* Execute an action in a group command(controller)
*
* @param array{action: string} $info Matched route info
* @param array $config
* @param array $args
* @param bool $detachedRun
*
* @return mixed
* @throws Throwable
*/
protected function runAction(array $info, array $config, array $args, bool $detachedRun = false): mixed
{
$controller = $this->createController($info);
$controller::setDesc($config['desc'] ?? '');
if ($detachedRun) {
$controller->setDetached();
}
if ($info['sub']) {
array_unshift($args, $info['sub']);
}
// Command method, no suffix
return $controller->run($args);
}
/**
* @param string $name
*
* @return Controller
*/
public function getController(string $name): Controller
{
$info = $this->router->getControllerInfo($name);
if (!$info) {
throw new RuntimeException('the group controller not exist. name: ' . $name);
}
$info['group'] = $name;
return $this->createController($info);
}
/**
* @param array{name: string, group: string, handler: mixed} $info
*
* @return Controller
*/
protected function createController(array $info): Controller
{
$group = $info['group']; // The group name
if (isset($this->groupObjects[$group])) {
$this->debugf('load the "%s" controller object form cache', $group);
return $this->groupObjects[$group];
}
$this->debugf('create the "%s" controller object and cache it', $group);
/** @var Controller $handler */
$handler = $info['handler']; // The controller class or object
if (is_string($handler)) {
$class = $handler;
Assert::isTrue(class_exists($class), "The console controller class '$class' not exists!");
// create group object
$handler = new $class();
}
if (!$handler instanceof Controller) {
Helper::throwInvalidArgument(
'The console controller class [%s] must instanceof the %s',
$handler,
Controller::class
);
}
// force set name and description
$handler::setName($group);
$handler->setApp($this);
$handler->setInputOutput($this->input, $this->output);
// set input name
if ($inputName = $info['name'] ?? '') {
$handler->setGroupName($inputName);
}
$handler->setDelimiter($this->delimiter);
// cache object
$this->groupObjects[$group] = $handler;
return $handler;
}
/****************************************************************************
* register console controller/command
****************************************************************************/
/**
* @param string $name
* @param ControllerInterface|string|null $class
* @param array $config
*
* @return $this
*/
public function controller(string $name, ControllerInterface|string|null $class = null, array $config = []): static
{
$this->logf(Console::VERB_CRAZY, 'register group controller: %s', $name);
$this->router->addGroup($name, $class, $config);
return $this;
}
/**
* Add group/controller
*
* @param string|class-string $name
* @param string|ControllerInterface|null $class The controller class
* @param array $config
*
* @return static
* @see controller()
*/
public function addGroup(string $name, ControllerInterface|string|null $class = null, array $config = []): static
{
return $this->controller($name, $class, $config);
}
/**
* @param string $name
* @param string|ControllerInterface|null $class The controller class
* @param array $config
*
* @return $this
* @see controller()
*/
public function addController(string $name, ControllerInterface|string|null $class = null, array $config = []): static
{
return $this->controller($name, $class, $config);
}
/**
* @param array $controllers
*/
public function controllers(array $controllers): void
{
$this->addControllers($controllers);
}
/**
* @param array $controllers
*/
public function addControllers(array $controllers): void
{
$this->router->addControllers($controllers);
}
/**
* @param string $name
* @param class-string|CommandInterface|null|Closure(FlagsParser, Output):void $handler
* @param array{desc: string, aliases: array, options: array, arguments: array} $config config the command.
*
* @return Application
*/
public function command(string $name, string|Closure|CommandInterface|null $handler = null, array $config = []): static
{
$this->logf(Console::VERB_CRAZY, 'register alone command: %s', $name);
$this->router->addCommand($name, $handler, $config);
return $this;
}
/**
* add command
*
* @param string $name
* @param class-string|CommandInterface|null|Closure(FlagsParser, Output):void $handler
* @param array{desc: string, aliases: array, options: array, arguments: array} $config config the command.
*
* @return Application
* @see command()
*/
public function addCommand(string $name, string|Closure|CommandInterface|null $handler = null, array $config = []): static
{
return $this->command($name, $handler, $config);
}
/**
* @param array{string, mixed} $commands
*/
public function addCommands(array $commands): void
{
$this->router->addCommands($commands);
}
/**
* @param array{string, mixed} $commands
*/
public function commands(array $commands): void
{
$this->addCommands($commands);
}
/**
* auto register commands from a dir.
*
* ```php
* $app->registerCommands('SwagPhp\Command', dirname(__DIR__) . '/src/Command');
* ```
*
* @param string $namespace
* @param string $basePath
*
* @return $this
*/
public function registerCommands(string $namespace, string $basePath): static
{
if (!is_dir($basePath)) {
return $this;
}
$this->debugf('register commands from the namespace: %s', $namespace);
$length = strlen($basePath) + 1;
// $iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
$iter = Dir::getIterator($basePath, Dir::getPhpFileFilter());
foreach ($iter as $file) {
$subPath = substr($file->getPathName(), $length, -4);
$fullClass = $namespace . '\\' . str_replace('/', '\\', $subPath);
$this->addCommand($fullClass);
}
return $this;
}
/**
* auto register controllers from a dir.
*
* @param string $namespace
* @param string $basePath
*
* @return $this
*/
public function registerGroups(string $namespace, string $basePath): self
{
if (!is_dir($basePath)) {
return $this;
}
$this->debugf('register groups from the namespace: %s', $namespace);
$length = strlen($basePath) + 1;
// $iterator = Helper::directoryIterator($basePath, $this->getFileFilter());
$iter = Dir::getIterator($basePath, Dir::getPhpFileFilter());
foreach ($iter as $file) {
$subPath = substr($file->getPathName(), $length, -4);
$fullClass = $namespace . '\\' . str_replace('/', '\\', $subPath);
$this->addController($fullClass);
}
return $this;
}
}