Skip to content

Commit 3674b03

Browse files
committedJan 13, 2023
refactor: update the sub command alias logic for controller
1 parent 6c44403 commit 3674b03

File tree

4 files changed

+23
-58
lines changed

4 files changed

+23
-58
lines changed
 

‎src/Controller.php

+9-53
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
use Toolkit\Stdlib\Obj\ObjectHelper;
2929
use Toolkit\Stdlib\Str;
3030
use function array_flip;
31-
use function array_keys;
3231
use function array_shift;
3332
use function implode;
3433
use function is_array;
@@ -48,19 +47,6 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
4847
{
4948
use ControllerHelpTrait;
5049

51-
/**
52-
* The sub-command aliases mapping
53-
*
54-
* eg: [
55-
* alias => command,
56-
* alias1 => command1,
57-
* alias2 => command1,
58-
* ]
59-
*
60-
* @var array
61-
*/
62-
private static array $commandAliases = [];
63-
6450
/**
6551
* @var array global options for the group command
6652
*/
@@ -153,7 +139,9 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
153139
/**
154140
* Define command alias mapping. please rewrite it on sub-class.
155141
*
156-
* @return array
142+
* - key is command name, value is aliases.
143+
*
144+
* @return array{string: list<string>}
157145
*/
158146
protected static function commandAliases(): array
159147
{
@@ -177,7 +165,8 @@ protected function init(): void
177165
{
178166
parent::init();
179167

180-
self::loadCommandAliases();
168+
// up: load sub-commands alias
169+
$this->loadCommandAliases();
181170

182171
$list = $this->disabledCommands();
183172

@@ -475,6 +464,7 @@ protected function handleNotFound(string $group, string $command, array $args):
475464
protected function newActionFlags(string $action = ''): FlagsParser
476465
{
477466
$action = $action ?: $this->action;
467+
478468
if (!$fs = $this->getActionFlags($action)) {
479469
$fs = new SFlags(['name' => $action]);
480470
$fs->setStopOnFistArg(false);
@@ -553,21 +543,6 @@ protected function getAllCommandMethods(ReflectionClass $ref = null, bool $onlyN
553543
}
554544
}
555545

556-
/**
557-
* @param string $alias
558-
*
559-
* @return string
560-
*/
561-
public function resolveAlias(string $alias): string
562-
{
563-
if (!$alias) {
564-
return '';
565-
}
566-
567-
$map = $this->getCommandAliases();
568-
return $map[$alias] ?? $alias;
569-
}
570-
571546
/**
572547
* @param string $name
573548
*
@@ -581,27 +556,22 @@ public function isDisabled(string $name): bool
581556
/**
582557
* load sub-commands aliases from sub-class::commandAliases()
583558
*/
584-
public static function loadCommandAliases(): void
559+
public function loadCommandAliases(): void
585560
{
586561
$cmdAliases = static::commandAliases();
587562
if (!$cmdAliases) {
588563
return;
589564
}
590565

591-
$fmtAliases = [];
592566
foreach ($cmdAliases as $name => $item) {
593567
// $name is command, $item is alias list
594568
// eg: ['command1' => ['alias1', 'alias2']]
595569
if (is_array($item)) {
596-
foreach ($item as $alias) {
597-
$fmtAliases[$alias] = $name;
598-
}
570+
$this->setAlias($name, $item, true);
599571
} elseif (is_string($item)) { // $item is command, $name is alias name
600-
$fmtAliases[$name] = $item;
572+
$this->setAlias($item, $name, true);
601573
}
602574
}
603-
604-
self::$commandAliases = $fmtAliases;
605575
}
606576

607577
/**************************************************************************
@@ -616,20 +586,6 @@ public function getDisabledCommands(): array
616586
return $this->disabledCommands;
617587
}
618588

619-
/**
620-
* @param string $name
621-
*
622-
* @return array
623-
*/
624-
public function getCommandAliases(string $name = ''): array
625-
{
626-
if ($name) {
627-
return self::$commandAliases ? array_keys(self::$commandAliases, $name, true) : [];
628-
}
629-
630-
return self::$commandAliases;
631-
}
632-
633589
/**
634590
* @return string
635591
*/

‎src/Decorate/ControllerHelpTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function helpCommand(): int
5959

6060
$action = Str::camelCase($action);
6161
$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;
62-
$aliases = $this->getCommandAliases($action);
62+
$aliases = $this->getNameAliases($action);
6363

6464
// up: find global aliases from app
6565
if ($this->app) {
@@ -133,7 +133,7 @@ public function showCommandList(): void
133133
$desc .= '(<red>DISABLED</red>)';
134134
}
135135

136-
$aliases = $this->getCommandAliases($cmd);
136+
$aliases = $this->getNameAliases($cmd);
137137
$desc .= $aliases ? ColorTag::wrap(' (alias: ' . implode(',', $aliases) . ')', 'info') : '';
138138

139139
$commands[$cmd] = $desc;

‎src/Decorate/SubCommandsWareTrait.php

+8
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,14 @@ public function getCommands(): array
411411
return $this->commands;
412412
}
413413

414+
/**
415+
* @return array
416+
*/
417+
public function getSubAliasMap(): array
418+
{
419+
return $this->aliases;
420+
}
421+
414422
/**
415423
* @return array
416424
*/

‎src/Handler/AbstractHandler.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ protected function initForRun(): void
239239
}
240240

241241
$this->addPathNode($this->commandName);
242+
$this->commentsVars['binWithCmd'] = $this->getPath();
242243
}
243244

244245
/**
@@ -337,12 +338,12 @@ public function run(array $args): mixed
337338
protected function doRun(array $args): mixed
338339
{
339340
// some prepare check
340-
if (true !== $this->prepare()) {
341+
if (!$this->prepare()) {
341342
return -1;
342343
}
343344

344345
// return False to deny goon run.
345-
if (false === $this->beforeExecute()) {
346+
if (!$this->beforeExecute()) {
346347
return -1;
347348
}
348349

@@ -425,7 +426,7 @@ protected function afterExecute(): void
425426
}
426427

427428
/**
428-
* prepare run
429+
* prepare run, return false to stop run.
429430
*/
430431
protected function prepare(): bool
431432
{

0 commit comments

Comments
 (0)