Skip to content

Commit 2572653

Browse files
committed
some update: add command aliases support.
1 parent 447d8e4 commit 2572653

File tree

9 files changed

+175
-31
lines changed

9 files changed

+175
-31
lines changed

Something.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# some idea
2+
3+
## controller
4+
5+
```php
6+
protected function commandConfigure($definition)
7+
{
8+
// old: own create.
9+
$this->createDefinition()->addArgument();
10+
11+
// maybe: get by argument.
12+
$definition->addArgument();
13+
14+
// ....
15+
}
16+
```
17+
18+
```php
19+
/**
20+
* the group controller metadata. to define name, description
21+
* @return array
22+
*/
23+
public static function metadata()
24+
{
25+
return [
26+
'name' => 'model',
27+
'description' => 'some console command handle for model user data.',
28+
29+
// for command
30+
'aliases' => [
31+
'i', 'in',
32+
],
33+
34+
// for controller
35+
'aliases' => [
36+
'i' => 'install',
37+
'up' => 'update',
38+
]
39+
];
40+
}
41+
```

examples/DemoCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class DemoCommand extends Command
2323

2424
/**
2525
* {@inheritDoc}
26+
* @throws \LogicException
2627
*/
2728
protected function configure()
2829
{

examples/routes.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
$out->info('hello, this is a test command: ' . $cmd);
2323
});
2424

25-
$app->command('test', TestCommand::class);
25+
$app->command('test', TestCommand::class, [
26+
'aliases' => ['t']
27+
]);
28+
2629
$app->command('prg', function () {
2730
$i = 0;
2831
$total = 120;
@@ -39,5 +42,8 @@
3942

4043
}, 'a description message');
4144

42-
$app->controller('home', HomeController::class);
45+
$app->controller('home', HomeController::class, [
46+
'aliases' => ['h']
47+
]);
48+
4349
$app->controller(PharController::class);

src/Application.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ class Application extends AbstractApplication
2525
* Register a app group command(by controller)
2626
* @param string $name The controller name
2727
* @param string $class The controller class
28+
* @param null|array|string $option
2829
* @return static
2930
* @throws \InvalidArgumentException
3031
*/
31-
public function controller(string $name, string $class = null)
32+
public function controller(string $name, string $class = null, $option = null)
3233
{
3334
if (!$class && class_exists($name)) {
3435
/** @var Controller $class */
@@ -54,6 +55,18 @@ public function controller(string $name, string $class = null)
5455

5556
$this->controllers[$name] = $class;
5657

58+
if (!$option) {
59+
return $this;
60+
}
61+
62+
// have option information
63+
if (\is_string($option)) {
64+
$this->addCommandMessage($name, $option);
65+
} elseif (\is_array($option)) {
66+
$this->addCommandAliases($name, $option['aliases'] ?? null);
67+
$this->addCommandMessage($name, $option['description'] ?? null);
68+
}
69+
5770
return $this;
5871
}
5972

@@ -79,11 +92,11 @@ public function controllers(array $controllers)
7992
* Register a app independent console command
8093
* @param string|Command $name
8194
* @param string|\Closure|Command $handler
82-
* @param null|string $description
95+
* @param null|array|string $option
8396
* @return $this
8497
* @throws \InvalidArgumentException
8598
*/
86-
public function command(string $name, $handler = null, $description = null)
99+
public function command(string $name, $handler = null, $option = null)
87100
{
88101
if (!$handler && class_exists($name)) {
89102
/** @var Command $name */
@@ -119,8 +132,16 @@ public function command(string $name, $handler = null, $description = null)
119132
// is an class name string
120133
$this->commands[$name] = $handler;
121134

122-
if ($description) {
123-
$this->addCommandMessage($name, $description);
135+
if (!$option) {
136+
return $this;
137+
}
138+
139+
// have option information
140+
if (\is_string($option)) {
141+
$this->addCommandMessage($name, $option);
142+
} elseif (\is_array($option)) {
143+
$this->addCommandAliases($name, $option['aliases'] ?? null);
144+
$this->addCommandMessage($name, $option['description'] ?? null);
124145
}
125146

126147
return $this;
@@ -231,26 +252,30 @@ protected function getFileFilter()
231252
*/
232253
protected function dispatch($name)
233254
{
234-
$sep = $this->delimiter ?: '/';
255+
$sep = $this->delimiter ?: ':';
235256

236257
//// is a command name
237258

238-
if ($this->isCommand($name)) {
239-
return $this->runCommand($name, true);
259+
$realName = $this->getRealCommandName($name);
260+
261+
if ($this->isCommand($realName)) {
262+
return $this->runCommand($realName, true);
240263
}
241264

242265
//// is a controller name
243266

244267
$action = '';
245268

246-
// like 'home/index'
269+
// like 'home:index'
247270
if (strpos($name, $sep) > 0) {
248271
$input = array_filter(explode($sep, $name));
249272
list($name, $action) = \count($input) > 2 ? array_splice($input, 2) : $input;
250273
}
251274

252-
if ($this->isController($name)) {
253-
return $this->runAction($name, $action, true);
275+
$realName = $this->getRealCommandName($name);
276+
277+
if ($this->isController($realName)) {
278+
return $this->runAction($realName, $action, true);
254279
}
255280

256281
// command not found

src/Base/AbstractApplication.php

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,20 @@ abstract class AbstractApplication implements ApplicationInterface
6767
/** @var string Command delimiter. e.g dev:serve */
6868
public $delimiter = ':'; // '/' ':'
6969

70-
/** @var array The group commands */
71-
protected $controllers = [];
70+
/** @var string Current command name */
71+
private $commandName;
72+
73+
/** @var array Some message for command */
74+
private $commandMessages = [];
75+
76+
/** @var array Save command aliases */
77+
private $commandAliases = [];
7278

7379
/** @var array The independent commands */
7480
protected $commands = [];
7581

76-
/** @var array */
77-
private $commandMessages = [];
78-
79-
/** @var string */
80-
private $commandName;
82+
/** @var array The group commands */
83+
protected $controllers = [];
8184

8285
/**
8386
* App constructor.
@@ -470,11 +473,44 @@ public function getCommandMessage($name, $default = null)
470473
/**
471474
* @param string $name The command name
472475
* @param string $message
473-
* @return string
476+
* @return $this
474477
*/
475478
public function addCommandMessage($name, $message)
476479
{
477-
return $this->commandMessages[$name] = $message;
480+
if (!$name || !$message) {
481+
$this->commandMessages[$name] = $message;
482+
}
483+
484+
return $this;
485+
}
486+
487+
/**
488+
* @param string $name
489+
* @param string|array $aliases
490+
* @return $this
491+
*/
492+
public function addCommandAliases(string $name, $aliases)
493+
{
494+
if (!$name || !$aliases) {
495+
return $this;
496+
}
497+
498+
foreach ((array)$aliases as $alias) {
499+
if ($alias = trim($alias)) {
500+
$this->commandAliases[$alias] = $name;
501+
}
502+
}
503+
504+
return $this;
505+
}
506+
507+
/**
508+
* @param string $name
509+
* @return string
510+
*/
511+
protected function getRealCommandName(string $name)
512+
{
513+
return $this->commandAliases[$name] ?? $name;
478514
}
479515

480516
/**********************************************************
@@ -643,4 +679,20 @@ public function setCommandMessages(array $commandMessages)
643679
{
644680
$this->commandMessages = $commandMessages;
645681
}
682+
683+
/**
684+
* @return array
685+
*/
686+
public function getCommandAliases(): array
687+
{
688+
return $this->commandAliases;
689+
}
690+
691+
/**
692+
* @param array $commandAliases
693+
*/
694+
public function setCommandAliases(array $commandAliases)
695+
{
696+
$this->commandAliases = $commandAliases;
697+
}
646698
}

src/Base/AbstractCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ protected function handleAnnotationVars($str)
320320
* @param string $method
321321
* @param null|string $action
322322
* @return int
323-
* @throws \ReflectionException
324323
*/
325324
protected function showHelpByMethodAnnotation($method, $action = null)
326325
{
@@ -391,7 +390,7 @@ final public static function getName(): string
391390
/**
392391
* @return string
393392
*/
394-
final public static function getDescription(): ?string
393+
final public static function getDescription()
395394
{
396395
return static::$description;
397396
}

src/Base/ApplicationInterface.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,33 @@ public function runCommand($name, $believable = false);
4646
*/
4747
public function runAction($name, $action, $believable = false, $standAlone = false);
4848

49-
public function controller(string $name, string $controller = null);
49+
/**
50+
* Register a app group command(by controller)
51+
* @param string $name The controller name
52+
* @param string $class The controller class
53+
* @param null|array|string $option
54+
* string: define the description message.
55+
* array:
56+
* - aliases The command aliases
57+
* - description The description message
58+
* @return static
59+
* @throws \InvalidArgumentException
60+
*/
61+
public function controller(string $name, string $class = null, $option = null);
5062

51-
public function command(string $name, $handler = null, $description = null);
63+
/**
64+
* Register a app independent console command
65+
* @param string|CommandInterface $name
66+
* @param string|\Closure|CommandInterface $handler
67+
* @param null|array|string $option
68+
* string: define the description message.
69+
* array:
70+
* - aliases The command aliases
71+
* - description The description message
72+
* @return $this
73+
* @throws \InvalidArgumentException
74+
*/
75+
public function command(string $name, $handler = null, $option = null);
5276

5377
public function showCommandList($quit = true);
5478
}

src/Base/CommandInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ public static function getName(): string;
4141
/**
4242
* @return string
4343
*/
44-
public static function getDescription(): ?string;
44+
public static function getDescription();
4545
}

src/Controller.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ protected function configure()
7171
* @param Input $input
7272
* @param Output $output
7373
* @return mixed
74-
* @throws \ReflectionException
7574
*/
7675
protected function execute($input, $output)
7776
{
@@ -114,7 +113,6 @@ protected function execute($input, $output)
114113

115114
/**
116115
* @return int
117-
* @throws \ReflectionException
118116
*/
119117
protected function showHelp()
120118
{
@@ -136,7 +134,6 @@ protected function showHelp()
136134
* {script} {name} index
137135
*
138136
* @return int
139-
* @throws \ReflectionException
140137
*/
141138
final public function helpCommand()
142139
{
@@ -157,7 +154,6 @@ final public function helpCommand()
157154

158155
/**
159156
* show command list of the controller class
160-
* @throws \ReflectionException
161157
*/
162158
final public function showCommandList()
163159
{

0 commit comments

Comments
 (0)