Skip to content

Commit 323ae9b

Browse files
committed
feat: add more common methods for group/command
1 parent bd7e047 commit 323ae9b

File tree

6 files changed

+169
-42
lines changed

6 files changed

+169
-42
lines changed

src/Application.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public function dispatch(string $name, array $args = [])
303303

304304
// is command
305305
if ($info['type'] === Router::TYPE_SINGLE) {
306-
return $this->runCommand($info['name'], $info['handler'], $cmdOptions, $args);
306+
return $this->runCommand($info, $cmdOptions, $args);
307307
}
308308

309309
// is controller/group
@@ -313,16 +313,18 @@ public function dispatch(string $name, array $args = [])
313313
/**
314314
* run a independent command
315315
*
316-
* @param string $name Command name
317-
* @param Closure|string $handler Command class or handler func
316+
* @param array{name: string, handler: mixed, realName: string} $info
318317
* @param array $options
319318
* @param array $args
320319
*
321320
* @return mixed
322321
* @throws Throwable
323322
*/
324-
protected function runCommand(string $name, $handler, array $options, array $args)
323+
protected function runCommand(array $info, array $options, array $args)
325324
{
325+
/** @var Closure|string $handler Command class or handler func */
326+
$handler = $info['handler'];
327+
326328
if (is_object($handler) && method_exists($handler, '__invoke')) {
327329
$fs = SFlags::new();
328330
$fs->addOptsByRules(GlobalOption::getAloneOptions());
@@ -344,13 +346,13 @@ protected function runCommand(string $name, $handler, array $options, array $arg
344346

345347
/** @var Command $object */
346348
$object = new $handler($this->input, $this->output);
347-
348349
if (!($object instanceof Command)) {
349350
Helper::throwInvalidArgument("The console command class [$handler] must instanceof the " . Command::class);
350351
}
351352

352-
$object::setName($name);
353+
$object::setName($info['cmdId']); // real command name.
353354
$object->setApp($this);
355+
$object->setCommandName($info['name']);
354356
$result = $object->run($args);
355357
}
356358

@@ -380,8 +382,8 @@ protected function runAction(array $info, array $options, array $args, bool $det
380382
$controller->setDetached();
381383
}
382384

383-
if ($info['action']) {
384-
array_unshift($args, $info['action']);
385+
if ($info['sub']) {
386+
array_unshift($args, $info['sub']);
385387
}
386388

387389
// Command method, no suffix
@@ -441,6 +443,12 @@ protected function createController(array $info): Controller
441443
// force set name and description
442444
$handler::setName($group);
443445
$handler->setApp($this);
446+
447+
// set input name
448+
if ($inputName = $info['name'] ?? '') {
449+
$handler->setGroupName($inputName);
450+
}
451+
444452
$handler->setDelimiter($this->delimiter);
445453

446454
// cache object

src/Command.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ abstract class Command extends AbstractHandler implements CommandInterface
3838
*/
3939
protected $parent;
4040

41-
/**
42-
* @var string
43-
*/
44-
protected $commandName = '';
45-
4641
protected function init(): void
4742
{
4843
$this->commandName = self::getName();
@@ -70,7 +65,7 @@ protected function beforeInitFlagsParser(FlagsParser $fs): void
7065
*/
7166
protected function afterInitFlagsParser(FlagsParser $fs): void
7267
{
73-
$this->debugf('load flags configure for command: %s', $this->getRealName());
68+
$this->debugf('load flags configure for command: %s', $this->getRealCName());
7469
$this->configure();
7570

7671
$isEmpty = $this->flags->isEmpty();
@@ -131,8 +126,8 @@ protected function showHelp(): bool
131126
/**
132127
* @return string
133128
*/
134-
public function getCommandName(): string
129+
public function getRealCName(): string
135130
{
136-
return $this->commandName;
131+
return self::getName();
137132
}
138133
}

src/Contract/CommandHandlerInterface.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,47 @@ public function run(array $args);
4545
public function getApp(): AbstractApplication;
4646

4747
/**
48+
* The input group name.
49+
*
4850
* @return string
4951
*/
5052
public function getGroupName(): string;
5153

5254
/**
53-
* Alias of the getName()
55+
* The real group or command name. Alias of the getName()
5456
*
5557
* @return string
5658
*/
5759
public function getRealName(): string;
5860

5961
/**
62+
* The real group name.
63+
*
64+
* @return string
65+
*/
66+
public function getRealGName(): string;
67+
68+
/**
69+
* The real command name.
70+
*
71+
* @return string
72+
*/
73+
public function getRealCName(): string;
74+
75+
/**
76+
* The input command/subcommand name.
77+
*
6078
* @return string
6179
*/
6280
public function getCommandName(): string;
6381

82+
/**
83+
* @param bool $useReal
84+
*
85+
* @return string
86+
*/
87+
public function getCommandId(bool $useReal = true): string;
88+
6489
/**
6590
* @return string
6691
*/

src/Controller.php

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,21 @@ abstract class Controller extends AbstractHandler implements ControllerInterface
8080
private $action = '';
8181

8282
/**
83+
* The action method name on the controller.
84+
*
8385
* @var string
8486
*/
8587
private $actionMethod = '';
8688

8789
/**
88-
* Input subcommand name.
90+
* The input group name.
8991
*
9092
* @var string
9193
*/
92-
private $commandName = '';
94+
private $groupName = '';
9395

9496
/**
95-
* eg: '/' ':'
97+
* The delimiter. eg: '/' ':'
9698
*
9799
* @var string
98100
*/
@@ -181,6 +183,7 @@ protected function init(): void
181183

182184
$list = $this->disabledCommands();
183185

186+
$this->groupName = $this->getRealGName();
184187
// save to property
185188
$this->disabledCommands = $list ? array_flip($list) : [];
186189

@@ -575,14 +578,6 @@ public function resolveAlias(string $alias): string
575578
return $map[$alias] ?? $alias;
576579
}
577580

578-
/**
579-
* @return string
580-
*/
581-
public function getGroupName(): string
582-
{
583-
return self::getName();
584-
}
585-
586581
/**
587582
* @param string $name
588583
*
@@ -648,19 +643,65 @@ public function getCommandAliases(string $name = ''): array
648643
/**
649644
* @return string
650645
*/
651-
public function getAction(): string
646+
public function getGroupName(): string
647+
{
648+
return $this->groupName;
649+
}
650+
651+
/**
652+
* @return string
653+
*/
654+
public function getRealGName(): string
655+
{
656+
return self::getName();
657+
}
658+
659+
/**
660+
* @param string $groupName
661+
*/
662+
public function setGroupName(string $groupName): void
663+
{
664+
$this->groupName = $groupName;
665+
}
666+
667+
/**
668+
* @return string
669+
*/
670+
public function getRealCName(): string
652671
{
653672
return $this->action;
654673
}
655674

656675
/**
657676
* @return string
658677
*/
659-
public function getCommandName(): string
678+
public function getSubName(): string
660679
{
661680
return $this->commandName;
662681
}
663682

683+
/**
684+
* @param bool $useReal
685+
*
686+
* @return string
687+
*/
688+
public function getCommandId(bool $useReal = true): string
689+
{
690+
if ($useReal) {
691+
return self::getName() . $this->delimiter . $this->action;
692+
}
693+
694+
return $this->groupName . $this->delimiter . $this->commandName;
695+
}
696+
697+
/**
698+
* @return string
699+
*/
700+
public function getAction(): string
701+
{
702+
return $this->action;
703+
}
704+
664705
/**
665706
* @param string $action
666707
*

src/Handler/AbstractHandler.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ abstract class AbstractHandler implements CommandHandlerInterface
9595
*/
9696
protected $params;
9797

98+
/**
99+
* The input command name. maybe is an alias name.
100+
*
101+
* @var string
102+
*/
103+
protected $commandName = '';
104+
98105
/**
99106
* @var array Command options
100107
*/
@@ -523,6 +530,38 @@ public function getGroupName(): string
523530
return '';
524531
}
525532

533+
/**
534+
* @return string
535+
*/
536+
public function getRealGName(): string
537+
{
538+
return '';
539+
}
540+
541+
/**
542+
* @return string
543+
*/
544+
public function getRealCName(): string
545+
{
546+
return '';
547+
}
548+
549+
/**
550+
* @param string $commandName
551+
*/
552+
public function setCommandName(string $commandName): void
553+
{
554+
$this->commandName = $commandName;
555+
}
556+
557+
/**
558+
* @return string
559+
*/
560+
public function getCommandName(): string
561+
{
562+
return $this->commandName;
563+
}
564+
526565
/**
527566
* @return string
528567
*/
@@ -531,6 +570,16 @@ public function getRealName(): string
531570
return self::getName();
532571
}
533572

573+
/**
574+
* @param bool $useReal
575+
*
576+
* @return string
577+
*/
578+
public function getCommandId(bool $useReal = true): string
579+
{
580+
return $useReal ? self::getName() : $this->commandName;
581+
}
582+
534583
/**
535584
* @return array
536585
*/

0 commit comments

Comments
 (0)