28
28
use Toolkit \Stdlib \Obj \ObjectHelper ;
29
29
use Toolkit \Stdlib \Str ;
30
30
use function array_flip ;
31
- use function array_keys ;
32
31
use function array_shift ;
33
32
use function implode ;
34
33
use function is_array ;
@@ -48,19 +47,6 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
48
47
{
49
48
use ControllerHelpTrait;
50
49
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
-
64
50
/**
65
51
* @var array global options for the group command
66
52
*/
@@ -153,7 +139,9 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
153
139
/**
154
140
* Define command alias mapping. please rewrite it on sub-class.
155
141
*
156
- * @return array
142
+ * - key is command name, value is aliases.
143
+ *
144
+ * @return array{string: list<string>}
157
145
*/
158
146
protected static function commandAliases (): array
159
147
{
@@ -177,7 +165,8 @@ protected function init(): void
177
165
{
178
166
parent ::init ();
179
167
180
- self ::loadCommandAliases ();
168
+ // up: load sub-commands alias
169
+ $ this ->loadCommandAliases ();
181
170
182
171
$ list = $ this ->disabledCommands ();
183
172
@@ -475,6 +464,7 @@ protected function handleNotFound(string $group, string $command, array $args):
475
464
protected function newActionFlags (string $ action = '' ): FlagsParser
476
465
{
477
466
$ action = $ action ?: $ this ->action ;
467
+
478
468
if (!$ fs = $ this ->getActionFlags ($ action )) {
479
469
$ fs = new SFlags (['name ' => $ action ]);
480
470
$ fs ->setStopOnFistArg (false );
@@ -553,21 +543,6 @@ protected function getAllCommandMethods(ReflectionClass $ref = null, bool $onlyN
553
543
}
554
544
}
555
545
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
-
571
546
/**
572
547
* @param string $name
573
548
*
@@ -581,27 +556,22 @@ public function isDisabled(string $name): bool
581
556
/**
582
557
* load sub-commands aliases from sub-class::commandAliases()
583
558
*/
584
- public static function loadCommandAliases (): void
559
+ public function loadCommandAliases (): void
585
560
{
586
561
$ cmdAliases = static ::commandAliases ();
587
562
if (!$ cmdAliases ) {
588
563
return ;
589
564
}
590
565
591
- $ fmtAliases = [];
592
566
foreach ($ cmdAliases as $ name => $ item ) {
593
567
// $name is command, $item is alias list
594
568
// eg: ['command1' => ['alias1', 'alias2']]
595
569
if (is_array ($ item )) {
596
- foreach ($ item as $ alias ) {
597
- $ fmtAliases [$ alias ] = $ name ;
598
- }
570
+ $ this ->setAlias ($ name , $ item , true );
599
571
} elseif (is_string ($ item )) { // $item is command, $name is alias name
600
- $ fmtAliases [ $ name ] = $ item ;
572
+ $ this -> setAlias ( $ item , $ name , true ) ;
601
573
}
602
574
}
603
-
604
- self ::$ commandAliases = $ fmtAliases ;
605
575
}
606
576
607
577
/**************************************************************************
@@ -616,20 +586,6 @@ public function getDisabledCommands(): array
616
586
return $ this ->disabledCommands ;
617
587
}
618
588
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
-
633
589
/**
634
590
* @return string
635
591
*/
0 commit comments