Skip to content

Commit

Permalink
up: modify the group action not found logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 10, 2021
1 parent f278bcf commit 9c92316
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function run(string $command = '')

// if enable swoole coroutine
if (static::isCoroutine() && Helper::isSupportCoroutine()) {
$result = $this->coroutineRun();
$result = $this->coExecute();
} else { // when not enable coroutine
$result = $this->execute($this->input, $this->output);
}
Expand All @@ -272,7 +272,7 @@ public function run(string $command = '')
*
* @return bool
*/
public function coroutineRun(): bool
public function coExecute(): bool
{
// $ch = new Coroutine\Channel(1);
$ok = Coroutine::create(function () {
Expand Down
119 changes: 81 additions & 38 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ protected function onNotFound(string $action): bool
}

/**
* @param string $command command in the group
* @param string $command
*
* @return int|mixed
* @return string
*/
public function run(string $command = '')
protected function findCommandName(string $command): string
{
if (!$command = trim($command, $this->delimiter)) {
$command = $this->defaultAction;
Expand All @@ -214,6 +214,19 @@ public function run(string $command = '')
}
}

return $command;
}

/**
* @param string $command command in the group
*
* @return int|mixed
* @throws ReflectionException
*/
public function run(string $command = '')
{
$command = $this->findCommandName($command);

// if not input sub-command, render group help.
if (!$command) {
$this->debugf('sub-command is empty, display help for the group: %s', self::getName());
Expand All @@ -226,13 +239,27 @@ public function run(string $command = '')
$command = $this->getRealCommandName($command);

// convert 'boo-foo' to 'booFoo'
$this->action = Str::camelCase($command);
$this->debugf('will run the group action: %s, sub-command: %s', $this->action, $command);
$this->action = $action = Str::camelCase($command);
$this->debugf("will run the '%s' group action: %s, sub-command: %s", static::getName(), $this->action, $command);

$this->beforeRun();

// check method not exist
$method = $this->getMethodName($action);

// if command method not exists.
if (!method_exists($this, $method)) {
return $this->handleNotFound(static::getName(), $action);
}

// do running
return parent::run($command);
}

protected function beforeRun(): void
{
}

/**
* Load command configure
*/
Expand Down Expand Up @@ -288,7 +315,6 @@ protected function afterAction(): void
* @param Output $output
*
* @return mixed
* @throws ReflectionException
*/
final public function execute($input, $output)
{
Expand All @@ -301,40 +327,47 @@ final public function execute($input, $output)
return -1;
}

$method = $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;
$method = $this->getMethodName($action);

// the action method exists and only allow access public method.
// if (method_exists($this, $method) && (($rfm = new ReflectionMethod($this, $method)) && $rfm->isPublic())) {
if (method_exists($this, $method)) {
// before run action
if (!$this->beforeAction()) {
$this->debugf('beforeAction() returns FALSE, interrupt processing continues');
return 0;
}
// if (method_exists($this, $method)) {
// before run action
if (!$this->beforeAction()) {
$this->debugf('beforeAction() returns FALSE, interrupt processing continues');
return 0;
}

if (method_exists($this, $beforeFunc = 'before' . ucfirst($action))) {
$beforeOk = $this->$beforeFunc($input, $output);
if ($beforeOk === false) {
$this->debugf('%s() returns FALSE, interrupt processing continues', $beforeFunc);
return 0;
}
if (method_exists($this, $beforeFunc = 'before' . ucfirst($action))) {
$beforeOk = $this->$beforeFunc($input, $output);
if ($beforeOk === false) {
$this->debugf('%s() returns FALSE, interrupt processing continues', $beforeFunc);
return 0;
}
}

// run action
$result = $this->$method($input, $output);

// after run action
if (method_exists($this, $after = 'after' . ucfirst($action))) {
$this->$after($input, $output);
}
// run action
$result = $this->$method($input, $output);

$this->afterAction();
return $result;
// after run action
if (method_exists($this, $after = 'after' . ucfirst($action))) {
$this->$after($input, $output);
}

$this->afterAction();
return $result;
}

/**
* @param string $group
* @param string $action
*
* @return int
*/
protected function handleNotFound(string $group, string $action): int
{
// if user custom handle not found logic.
if ($this->onNotFound($action)) {
$this->debugf('user custom handle the action "%s" not found logic', $action);
$this->debugf('user custom handle the "%s" action "%s" not found', $group, $action);
return 0;
}

Expand All @@ -344,22 +377,33 @@ final public function execute($input, $output)
// if (($notFoundCallback = $this->notFoundCallback) && method_exists($this, $notFoundCallback)) {
// $result = $this->{$notFoundCallback}($action);
// } else {
$output->liteError("Sorry, The command '$action' not exist of the group '{$group}'!");
$this->output->liteError("Sorry, The command '$action' not exist of the group '$group'!");

// find similar command names
$similar = Helper::findSimilar($action, $this->getAllCommandMethods(null, true));

if ($similar) {
$output->write(sprintf("\nMaybe what you mean is:\n <info>%s</info>", implode(', ', $similar)));
$this->output->writef("\nMaybe what you mean is:\n <info>%s</info>", implode(', ', $similar));
} else {
$this->showCommandList();
}

return -1;
}

/**
* @param string $action
*
* @return string
*/
protected function getMethodName(string $action): string
{
return $this->actionSuffix ? $action . ucfirst($this->actionSuffix) : $action;
}

/**
* @return bool
* @throws ReflectionException
*/
protected function showHelp(): bool
{
Expand Down Expand Up @@ -402,13 +446,13 @@ public function getGroupOptions(): array
* -s, --search Search command by input keywords
* --format Set the help information dump format(raw, xml, json, markdown)
* @return int
* @throws ReflectionException
* @example
* {script} {name} -h
* {script} {name}:help
* {script} {name}:help index
* {script} {name}:index -h
* {script} {name} index
*
*/
final public function helpCommand(): int
{
Expand Down Expand Up @@ -519,8 +563,8 @@ final public function showCommandList(): void
$name = $sName . $this->delimiter;
// $usage = "$script {$name}<info>{command}</info> [--options ...] [arguments ...]";
$usage = [
"$script {$name}<info>{command}</info> [--options ...] [arguments ...]",
"$script {$sName} <info>{command}</info> [--options ...] [arguments ...]",
"$script $name<info>{command}</info> [--options ...] [arguments ...]",
"$script $sName <info>{command}</info> [--options ...] [arguments ...]",
];
}

Expand Down Expand Up @@ -709,18 +753,17 @@ public function setActionSuffix(string $actionSuffix): void

/**
* @return bool
* @deprecated
*/
public function isExecutionAlone(): bool
{
throw new RuntimeException('please call isAttached() instead');
}

/**
* @param bool $executionAlone
*
* @deprecated
*/
public function setExecutionAlone($executionAlone = true): void
public function setExecutionAlone(): void
{
throw new RuntimeException('please call setAttached() instead');
}
Expand Down

0 comments on commit 9c92316

Please sign in to comment.