Skip to content

Commit

Permalink
refactor: update handler construct sign, add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 25, 2021
1 parent 4dfe19f commit 1119e7f
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 60 deletions.
64 changes: 35 additions & 29 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,40 +296,46 @@ protected function runCommand(array $info, array $config, array $args): mixed
$handler = $info['handler'];
$iptName = $info['name'];

if (is_object($handler) && method_exists($handler, '__invoke')) {
$fs = SFlags::new();
$fs->setName($iptName);
$fs->addOptsByRules(GlobalOption::getAloneOptions());

// command flags load
if ($cmdOpts = $config['options'] ?? null) {
$fs->addOptsByRules($cmdOpts);
if (is_object($handler)) {
// Command object
if ($handler instanceof Command) {
$handler->setInputOutput($this->input, $this->output);
$result = $handler->run($args);
} else { // Closure
$fs = SFlags::new();
$fs->setName($iptName);
$fs->addOptsByRules(GlobalOption::getAloneOptions());

// command flags load
if ($cmdOpts = $config['options'] ?? null) {
$fs->addOptsByRules($cmdOpts);
}
if ($cmdArgs = $config['arguments'] ?? null) {
$fs->addArgsByRules($cmdArgs);
}

$fs->setDesc($config['desc'] ?? 'No command description message');

// save to input object
$this->input->setFs($fs);

if (!$fs->parse($args)) {
return 0; // render help
}

$result = $handler($fs, $this->output);
}
if ($cmdArgs = $config['arguments'] ?? null) {
$fs->addArgsByRules($cmdArgs);
}

$fs->setDesc($config['desc'] ?? 'No command description message');

// save to input object
$this->input->setFs($fs);

if (!$fs->parse($args)) {
return 0; // render help
}

$result = $handler($fs, $this->output);
} else {
Assert::isTrue(class_exists($handler), "The console command class [$handler] not exists!");

$object = new $handler($this->input, $this->output);
Assert::isTrue($object instanceof Command, "Command class [$handler] must instanceof the " . Command::class);
/** @var $cmd Command */
$cmd = new $handler($this->input, $this->output);
Assert::isTrue($cmd instanceof Command, "Command class [$handler] must instanceof the " . Command::class);

/** @var Command $object */
$object::setName($info['cmdId']); // real command name.
$object->setApp($this);
$object->setCommandName($iptName);
$result = $object->run($args);
$cmd::setName($info['cmdId']); // real command name.
$cmd->setApp($this);
$cmd->setCommandName($iptName);
$result = $cmd->run($args);
}

return $result;
Expand Down
10 changes: 9 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ protected function init(): void
parent::init();
}

/**
* @return array
*/
public function getArguments(): array
{
return [];
}

/**
* @param FlagsParser $fs
*/
Expand Down Expand Up @@ -139,7 +147,7 @@ public function getRealCName(): string
/**
* Get the group
*
* @return Controller
* @return Controller|null
*/
public function getGroup(): ?Controller
{
Expand Down
33 changes: 21 additions & 12 deletions src/Component/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,10 @@ public function addCommand(string $name, string|Closure|CommandInterface $handle
$config['aliases'] = isset($config['aliases']) ? (array)$config['aliases'] : [];

if (is_string($handler)) {
if (!class_exists($handler)) {
Helper::throwInvalidArgument("The console command class [$handler] not exists!");
}

if (!is_subclass_of($handler, Command::class)) {
Helper::throwInvalidArgument('The console command class must is subclass of the: ' . Command::class);
}
Assert::isTrue(class_exists($handler), "The console command class '$handler' not exists!");
Assert::isTrue(is_subclass_of($handler, Command::class), 'The command class must be subclass of the: ' . Command::class);

// not enable
/** @var Command $handler */
/** @var Command $handler not enable */
if (!$handler::isEnabled()) {
return $this;
}
Expand All @@ -212,10 +206,11 @@ public function addCommand(string $name, string|Closure|CommandInterface $handle
if ($aliases = $handler::aliases()) {
$config['aliases'] = array_merge($config['aliases'], $aliases);
}
} elseif (!is_object($handler) || !method_exists($handler, '__invoke')) {
} elseif (!is_object($handler) || !$this->isValidCmdObject($handler)) {
Helper::throwInvalidArgument(
'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke()',
Command::class
'The command handler must is an subclass of %s OR a Closure OR a sub-object of %s',
Command::class,
Command::class,
);
}

Expand All @@ -235,6 +230,20 @@ public function addCommand(string $name, string|Closure|CommandInterface $handle
return $this;
}

/**
* @param object $handler
*
* @return bool
*/
private function isValidCmdObject(object $handler): bool
{
if ($handler instanceof Command) {
return true;
}

return method_exists($handler, '__invoke');
}

/**
* @param array $commands
*
Expand Down
7 changes: 4 additions & 3 deletions src/Contract/CommandHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Inhere\Console\Contract;

use Inhere\Console\AbstractApplication;
use Inhere\Console\Application;

/**
* Interface CommandHandlerInterface
Expand All @@ -35,14 +36,14 @@ interface CommandHandlerInterface
*
* @param array $args
*
* @return int|mixed return int is exit code. other is command exec result.
* @return mixed return int is exit code. other is command exec result.
*/
public function run(array $args): mixed;

/**
* @return AbstractApplication|ApplicationInterface
* @return Application
*/
public function getApp(): AbstractApplication;
public function getApp(): Application;

/**
* The input group name.
Expand Down
5 changes: 2 additions & 3 deletions src/Decorate/AttachApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Inhere\Console\Decorate;

use Inhere\Console\AbstractApplication;
use Inhere\Console\Application;
use Inhere\Console\Console;
use Toolkit\Stdlib\OS;
Expand Down Expand Up @@ -38,9 +37,9 @@ trait AttachApplicationTrait
private bool $attached = false;

/**
* @return AbstractApplication
* @return Application
*/
public function getApp(): AbstractApplication
public function getApp(): Application
{
return $this->app;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Decorate/InputOutputAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ public function setOutput(Output $output): void
$this->output = $output;
}

/**
* @param Input $input
* @param Output $output
*
* @return static
*/
public function setInputOutput(Input $input, Output $output): static
{
$this->input = $input;
$this->output = $output;
return $this;
}

/**
* @return FlagsParser
*/
Expand Down
22 changes: 15 additions & 7 deletions src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use function cli_set_process_title;
use function error_get_last;
use function function_exists;
use function vdump;
use const PHP_OS;

/**
Expand Down Expand Up @@ -140,11 +141,11 @@ public static function aliases(): array
/**
* Command constructor.
*
* @param Input $input
* @param Output $output
* @param Input|null $input
* @param Output|null $output
*/
// TODO public function __construct(Input $input = null, Output $output = null)
public function __construct(Input $input, Output $output)
// public function __construct(Input $input, Output $output)
public function __construct(Input $input = null, Output $output = null)
{
$this->input = $input;
$this->output = $output;
Expand All @@ -157,8 +158,7 @@ public function __construct(Input $input, Output $output)

protected function init(): void
{
$this->commentsVars = $this->annotationVars();

// $this->commentsVars = $this->annotationVars();
$this->afterInit();
$this->debugf('attach inner subcommands to "%s"', self::getName());
$this->addCommands($this->commands());
Expand Down Expand Up @@ -240,6 +240,12 @@ protected function annotationVars(): array
* running a command
**************************************************************************/

protected function initForRun(): void
{
$this->commentsVars = $this->annotationVars();

}

/**
* @param Input $input
*/
Expand Down Expand Up @@ -299,6 +305,8 @@ public function run(array $args): mixed
$name = self::getName();

try {
$this->initForRun();

$this->initFlagsParser($this->input);

$this->log(Console::VERB_DEBUG, "begin run '$name' - parse options", ['args' => $args]);
Expand Down Expand Up @@ -335,7 +343,7 @@ protected function doRun(array $args): mixed
if (isset($args[0])) {
$first = $args[0];
$rName = $this->resolveAlias($first);

// vdump($first, $rName);
// TODO
// if ($this->isSub($rName)) {
// }
Expand Down
Loading

0 comments on commit 1119e7f

Please sign in to comment.