Skip to content

Commit

Permalink
feat: add more common methods for group/command
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 14, 2021
1 parent bd7e047 commit 323ae9b
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 42 deletions.
24 changes: 16 additions & 8 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
Expand All @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -131,8 +126,8 @@ protected function showHelp(): bool
/**
* @return string
*/
public function getCommandName(): string
public function getRealCName(): string
{
return $this->commandName;
return self::getName();
}
}
27 changes: 26 additions & 1 deletion src/Contract/CommandHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
67 changes: 54 additions & 13 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -181,6 +183,7 @@ protected function init(): void

$list = $this->disabledCommands();

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

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -648,19 +643,65 @@ 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;
}

/**
* @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
*
Expand Down
49 changes: 49 additions & 0 deletions src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down
Loading

0 comments on commit 323ae9b

Please sign in to comment.