2828use Toolkit \Stdlib \Obj \ObjectHelper ;
2929use Toolkit \Stdlib \Str ;
3030use function array_flip ;
31- use function array_keys ;
3231use function array_shift ;
3332use function implode ;
3433use 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 */
0 commit comments