Skip to content

Commit ec208f6

Browse files
committed
new feature: alias support in the controller command
1 parent dd3f5e9 commit ec208f6

File tree

5 files changed

+109
-24
lines changed

5 files changed

+109
-24
lines changed

examples/Controllers/HomeController.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,27 @@ class HomeController extends Controller
1919
{
2020
protected static $description = 'default command controller. there are some command usage examples(2)';
2121

22+
/**
23+
* @return array
24+
*/
25+
protected static function commandMap()
26+
{
27+
return [
28+
'i' => 'index',
29+
'prg' => 'progress',
30+
];
31+
}
32+
2233
/**
2334
* this is a command's description message
2435
* the second line text
2536
* @usage usage message
2637
* @arguments
27-
* arg1 argument description 1
28-
* arg2 argument description 2
38+
* arg1 argument description 1
39+
* arg2 argument description 2
2940
* @options
30-
* --long,-s option description 1
31-
* --opt option description 2
41+
* -s, --long option description 1
42+
* --opt option description 2
3243
* @example example text one
3344
* the second line example
3445
*/
@@ -229,7 +240,7 @@ public function aListCommand()
229240
}
230241

231242
/**
232-
* a example for display a table
243+
* output format message: table
233244
*/
234245
public function tableCommand()
235246
{
@@ -285,7 +296,7 @@ public function tableCommand()
285296
}
286297

287298
/**
288-
* a example use padding() for show data
299+
* output format message: padding
289300
*/
290301
public function paddingCommand()
291302
{
@@ -299,7 +310,7 @@ public function paddingCommand()
299310
}
300311

301312
/**
302-
* a example for dump, print, json data
313+
* output format message: dump
303314
*/
304315
public function jsonCommand()
305316
{
@@ -375,7 +386,7 @@ public function defArgCommand()
375386
}
376387

377388
/**
378-
* This is a demo for use <red>Interact::confirm</red> method
389+
* This is a demo for use <magenta>Interact::confirm</magenta> method
379390
*/
380391
public function confirmCommand()
381392
{
@@ -456,7 +467,7 @@ public function limitedAskCommand()
456467
}
457468

458469
/**
459-
* a example for input password on command line. use: <magenta>Interact::askPassword()</magenta>
470+
* This is a demo for input password on command line. use: <magenta>Interact::askPassword()</magenta>
460471
* @usage {fullCommand}
461472
*/
462473
public function pwdCommand()

src/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ protected function dispatch($name)
268268

269269
// like 'home:index'
270270
if (strpos($name, $sep) > 0) {
271-
$input = array_filter(explode($sep, $name));
271+
$input = array_values(array_filter(explode($sep, $name)));
272272
list($name, $action) = \count($input) > 2 ? array_splice($input, 2) : $input;
273273
}
274274

src/Base/AbstractCommand.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public function run($command = '')
139139
return $this->showHelp();
140140
}
141141

142+
// some prepare check
142143
if (true !== $this->prepare()) {
143144
return -1;
144145
}
@@ -183,8 +184,7 @@ protected function afterExecute()
183184
*/
184185
protected function showHelp()
185186
{
186-
// 创建了 InputDefinition , 则使用它的信息。
187-
// 不会再解析和使用命令的注释。
187+
// 创建了 InputDefinition , 则使用它的信息。此时不会再解析和使用命令的注释。
188188
if ($def = $this->getDefinition()) {
189189
$cmd = $this->input->getCommand();
190190
$spt = $this->input->getScript();
@@ -202,17 +202,17 @@ protected function showHelp()
202202

203203
/**
204204
* prepare run
205+
* @throws \RuntimeException
205206
*/
206207
protected function prepare()
207208
{
208209
if ($this->processTitle) {
209210
if (\function_exists('cli_set_process_title')) {
210211
if (false === @cli_set_process_title($this->processTitle)) {
211-
if ('Darwin' === PHP_OS) {
212-
$this->output->writeln('<comment>Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.</comment>');
213-
} else {
214-
$error = error_get_last();
215-
trigger_error($error['message'], E_USER_WARNING);
212+
$error = error_get_last();
213+
214+
if ($error && 'Darwin' !== PHP_OS) {
215+
throw new \RuntimeException($error['message']);
216216
}
217217
}
218218
} elseif (\function_exists('setproctitle')) {
@@ -326,9 +326,10 @@ protected function handleAnnotationVars($str)
326326
* show help by parse method annotation
327327
* @param string $method
328328
* @param null|string $action
329+
* @param array $aliases
329330
* @return int
330331
*/
331-
protected function showHelpByMethodAnnotation($method, $action = null)
332+
protected function showHelpByMethodAnnotation($method, $action = null, array $aliases = [])
332333
{
333334
$ref = new \ReflectionClass($this);
334335
$name = $this->input->getCommand();
@@ -342,12 +343,15 @@ protected function showHelpByMethodAnnotation($method, $action = null)
342343
// is a console controller command
343344
if ($action && !$ref->getMethod($method)->isPublic()) {
344345
$this->write("The command [<info>$name</info>] don't allow access in the class.");
346+
345347
return 0;
346348
}
347349

348350
$doc = $ref->getMethod($method)->getDocComment();
349351
$tags = Annotation::tagList($this->handleAnnotationVars($doc));
350352

353+
$this->output->startBuffer();
354+
351355
foreach ($tags as $tag => $msg) {
352356
if (!$msg || !\is_string($msg)) {
353357
continue;
@@ -371,6 +375,8 @@ protected function showHelpByMethodAnnotation($method, $action = null)
371375
}
372376
}
373377

378+
$this->output->flush();
379+
374380
return 0;
375381
}
376382

src/Command.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
/**
1515
* Class Command
1616
* @package Inhere\Console
17+
*
18+
* ```php
19+
* In sub class:
20+
*
21+
* protected function execute($input, $output)
22+
* {
23+
* // some logic ...
24+
* }
25+
*
26+
* ```
1727
*/
1828
abstract class Command extends AbstractCommand implements CommandInterface
1929
{
@@ -28,7 +38,7 @@ abstract class Command extends AbstractCommand implements CommandInterface
2838
// // something logic ...
2939
// }
3040

31-
/**
41+
/*
3242
* configure
3343
*/
3444
// protected function configure()

src/Controller.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@
2222
*/
2323
abstract class Controller extends AbstractCommand implements ControllerInterface
2424
{
25+
/**
26+
* @var array
27+
*/
28+
private static $commandMap;
29+
2530
/** @var string */
2631
private $action;
2732

33+
/** @var bool */
34+
private $standAlone = false;
35+
2836
/** @var string */
2937
private $defaultAction = 'help';
3038

@@ -37,16 +45,27 @@ abstract class Controller extends AbstractCommand implements ControllerInterface
3745
/** @var string */
3846
protected $delimiter = ':'; // '/' ':'
3947

40-
/** @var bool */
41-
private $standAlone = false;
48+
/**
49+
* define command alias map
50+
* @return array
51+
*/
52+
protected static function commandMap()
53+
{
54+
return [
55+
// alias => command
56+
// 'i' => 'install',
57+
];
58+
}
4259

4360
/**
4461
* @param string $command
4562
* @return int
4663
*/
4764
public function run($command = '')
4865
{
49-
if (!$this->action = trim($command)) {
66+
$this->action = $this->getRealCommandName(trim($command, $this->delimiter));
67+
68+
if (!$this->action) {
5069
return $this->showHelp();
5170
}
5271

@@ -56,7 +75,7 @@ public function run($command = '')
5675
/**
5776
* load command configure
5877
*/
59-
protected function configure()
78+
final protected function configure()
6079
{
6180
if ($action = $this->action) {
6281
$method = $action . 'Configure';
@@ -148,9 +167,10 @@ final public function helpCommand()
148167

149168
$action = FormatUtil::camelCase($action);
150169
$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;
170+
$aliases = $this->getCommandAliases($action);
151171

152172
// show help info for a command.
153-
return $this->showHelpByMethodAnnotation($method, $action);
173+
return $this->showHelpByMethodAnnotation($method, $action, $aliases);
154174
}
155175

156176
/**
@@ -234,10 +254,48 @@ protected function getAllCommandMethods(\ReflectionClass $ref = null)
234254
}
235255
}
236256

257+
/**
258+
* @param string $name
259+
* @return mixed|string
260+
*/
261+
protected function getRealCommandName(string $name)
262+
{
263+
if (!$name) {
264+
return $name;
265+
}
266+
267+
$map = self::getCommandMap();
268+
269+
return $map[$name] ?? $name;
270+
}
271+
272+
/**
273+
* @param string $name
274+
* @return array
275+
*/
276+
protected function getCommandAliases(string $name)
277+
{
278+
$map = self::getCommandMap();
279+
280+
return $map ? array_keys($map, $name, true) : [];
281+
}
282+
237283
/**************************************************************************
238284
* getter/setter methods
239285
**************************************************************************/
240286

287+
/**
288+
* @return array
289+
*/
290+
public static function getCommandMap()
291+
{
292+
if (null === self::$commandMap) {
293+
self::$commandMap = static::commandMap();
294+
}
295+
296+
return self::$commandMap;
297+
}
298+
241299
/**
242300
* @return string
243301
*/

0 commit comments

Comments
 (0)