From 323ae9b3dcff0a31b602f4d72189cb5d29592b9c Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 14 Oct 2021 22:16:29 +0800 Subject: [PATCH] feat: add more common methods for group/command --- src/Application.php | 24 ++++++--- src/Command.php | 11 ++-- src/Contract/CommandHandlerInterface.php | 27 +++++++++- src/Controller.php | 67 +++++++++++++++++++----- src/Handler/AbstractHandler.php | 49 +++++++++++++++++ src/Router.php | 33 +++++++----- 6 files changed, 169 insertions(+), 42 deletions(-) diff --git a/src/Application.php b/src/Application.php index 7653cb6..b8a55e6 100644 --- a/src/Application.php +++ b/src/Application.php @@ -303,7 +303,7 @@ public function dispatch(string $name, array $args = []) // is command if ($info['type'] === Router::TYPE_SINGLE) { - return $this->runCommand($info['name'], $info['handler'], $cmdOptions, $args); + return $this->runCommand($info, $cmdOptions, $args); } // is controller/group @@ -313,16 +313,18 @@ public function dispatch(string $name, array $args = []) /** * run a independent command * - * @param string $name Command name - * @param Closure|string $handler Command class or handler func + * @param array{name: string, handler: mixed, realName: string} $info * @param array $options * @param array $args * * @return mixed * @throws Throwable */ - protected function runCommand(string $name, $handler, array $options, array $args) + protected function runCommand(array $info, array $options, array $args) { + /** @var Closure|string $handler Command class or handler func */ + $handler = $info['handler']; + if (is_object($handler) && method_exists($handler, '__invoke')) { $fs = SFlags::new(); $fs->addOptsByRules(GlobalOption::getAloneOptions()); @@ -344,13 +346,13 @@ protected function runCommand(string $name, $handler, array $options, array $arg /** @var Command $object */ $object = new $handler($this->input, $this->output); - if (!($object instanceof Command)) { Helper::throwInvalidArgument("The console command class [$handler] must instanceof the " . Command::class); } - $object::setName($name); + $object::setName($info['cmdId']); // real command name. $object->setApp($this); + $object->setCommandName($info['name']); $result = $object->run($args); } @@ -380,8 +382,8 @@ protected function runAction(array $info, array $options, array $args, bool $det $controller->setDetached(); } - if ($info['action']) { - array_unshift($args, $info['action']); + if ($info['sub']) { + array_unshift($args, $info['sub']); } // Command method, no suffix @@ -441,6 +443,12 @@ protected function createController(array $info): Controller // force set name and description $handler::setName($group); $handler->setApp($this); + + // set input name + if ($inputName = $info['name'] ?? '') { + $handler->setGroupName($inputName); + } + $handler->setDelimiter($this->delimiter); // cache object diff --git a/src/Command.php b/src/Command.php index 306424b..7c3dffc 100644 --- a/src/Command.php +++ b/src/Command.php @@ -38,11 +38,6 @@ abstract class Command extends AbstractHandler implements CommandInterface */ protected $parent; - /** - * @var string - */ - protected $commandName = ''; - protected function init(): void { $this->commandName = self::getName(); @@ -70,7 +65,7 @@ protected function beforeInitFlagsParser(FlagsParser $fs): void */ protected function afterInitFlagsParser(FlagsParser $fs): void { - $this->debugf('load flags configure for command: %s', $this->getRealName()); + $this->debugf('load flags configure for command: %s', $this->getRealCName()); $this->configure(); $isEmpty = $this->flags->isEmpty(); @@ -131,8 +126,8 @@ protected function showHelp(): bool /** * @return string */ - public function getCommandName(): string + public function getRealCName(): string { - return $this->commandName; + return self::getName(); } } diff --git a/src/Contract/CommandHandlerInterface.php b/src/Contract/CommandHandlerInterface.php index 48db6c1..eefd7bd 100644 --- a/src/Contract/CommandHandlerInterface.php +++ b/src/Contract/CommandHandlerInterface.php @@ -45,22 +45,47 @@ public function run(array $args); public function getApp(): AbstractApplication; /** + * The input group name. + * * @return string */ public function getGroupName(): string; /** - * Alias of the getName() + * The real group or command name. Alias of the getName() * * @return string */ public function getRealName(): string; /** + * The real group name. + * + * @return string + */ + public function getRealGName(): string; + + /** + * The real command name. + * + * @return string + */ + public function getRealCName(): string; + + /** + * The input command/subcommand name. + * * @return string */ public function getCommandName(): string; + /** + * @param bool $useReal + * + * @return string + */ + public function getCommandId(bool $useReal = true): string; + /** * @return string */ diff --git a/src/Controller.php b/src/Controller.php index 7afb421..e64cdd1 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -80,19 +80,21 @@ abstract class Controller extends AbstractHandler implements ControllerInterface private $action = ''; /** + * The action method name on the controller. + * * @var string */ private $actionMethod = ''; /** - * Input subcommand name. + * The input group name. * * @var string */ - private $commandName = ''; + private $groupName = ''; /** - * eg: '/' ':' + * The delimiter. eg: '/' ':' * * @var string */ @@ -181,6 +183,7 @@ protected function init(): void $list = $this->disabledCommands(); + $this->groupName = $this->getRealGName(); // save to property $this->disabledCommands = $list ? array_flip($list) : []; @@ -575,14 +578,6 @@ public function resolveAlias(string $alias): string return $map[$alias] ?? $alias; } - /** - * @return string - */ - public function getGroupName(): string - { - return self::getName(); - } - /** * @param string $name * @@ -648,7 +643,31 @@ public function getCommandAliases(string $name = ''): array /** * @return string */ - public function getAction(): string + public function getGroupName(): string + { + return $this->groupName; + } + + /** + * @return string + */ + public function getRealGName(): string + { + return self::getName(); + } + + /** + * @param string $groupName + */ + public function setGroupName(string $groupName): void + { + $this->groupName = $groupName; + } + + /** + * @return string + */ + public function getRealCName(): string { return $this->action; } @@ -656,11 +675,33 @@ public function getAction(): string /** * @return string */ - public function getCommandName(): string + public function getSubName(): string { return $this->commandName; } + /** + * @param bool $useReal + * + * @return string + */ + public function getCommandId(bool $useReal = true): string + { + if ($useReal) { + return self::getName() . $this->delimiter . $this->action; + } + + return $this->groupName . $this->delimiter . $this->commandName; + } + + /** + * @return string + */ + public function getAction(): string + { + return $this->action; + } + /** * @param string $action * diff --git a/src/Handler/AbstractHandler.php b/src/Handler/AbstractHandler.php index 9c07e0b..e056ea5 100644 --- a/src/Handler/AbstractHandler.php +++ b/src/Handler/AbstractHandler.php @@ -95,6 +95,13 @@ abstract class AbstractHandler implements CommandHandlerInterface */ protected $params; + /** + * The input command name. maybe is an alias name. + * + * @var string + */ + protected $commandName = ''; + /** * @var array Command options */ @@ -523,6 +530,38 @@ public function getGroupName(): string return ''; } + /** + * @return string + */ + public function getRealGName(): string + { + return ''; + } + + /** + * @return string + */ + public function getRealCName(): string + { + return ''; + } + + /** + * @param string $commandName + */ + public function setCommandName(string $commandName): void + { + $this->commandName = $commandName; + } + + /** + * @return string + */ + public function getCommandName(): string + { + return $this->commandName; + } + /** * @return string */ @@ -531,6 +570,16 @@ public function getRealName(): string return self::getName(); } + /** + * @param bool $useReal + * + * @return string + */ + public function getCommandId(bool $useReal = true): string + { + return $useReal ? self::getName() : $this->commandName; + } + /** * @return array */ diff --git a/src/Router.php b/src/Router.php index 3c34921..041acab 100644 --- a/src/Router.php +++ b/src/Router.php @@ -261,18 +261,25 @@ public function addControllers(array $controllers): void **********************************************************/ /** - * @param string $name The input command name - * - * @return array return route info array. If not found, will return empty array. - * [ - * type => 1, // 1 group 2 command - * name => '', // formatted $name + * ```php + * return [ + * type => 1, // 1 group 2 command + * name => '', // input group/command name. + * cmdId => '', // format and resolved $name + * // for group + * group => '', // group name. + * sub => '', // input subcommand name. on name is group. + * // common info * handler => handler class/object/func ... * options => [ * aliases => [], * description => '', * ], * ] + * ``` + * @param string $name The input command name + * + * @return array return route info array. If not found, will return empty array. */ public function match(string $name): array { @@ -283,18 +290,20 @@ public function match(string $name): array // is a command name if ($route = $this->commands[$realName] ?? []) { - $route['name'] = $route['cmdId'] = $realName; + $route['name'] = $name; + $route['cmdId'] = $realName; return $route; } // maybe is a controller/group name $action = ''; - $group = $realName; + $iptGrp = $group = $realName; // like 'home:index' if (strpos($realName, $sep) > 0) { [$group, $action] = explode($sep, $realName, 2); + $iptGrp = $group; $action = trim($action, ': '); // resolve alias $group = $this->resolveAlias($group); @@ -302,10 +311,10 @@ public function match(string $name): array // is group name if ($route = $this->controllers[$group] ?? []) { - $route['name'] = $realName; - $route['group'] = $group; - $route['action'] = $action; - $route['cmdId'] = $group . $sep . $action; + $route['name'] = $iptGrp; + $route['group'] = $group; + $route['sub'] = $action; + $route['cmdId'] = $group . $sep . $action; return $route; }