Skip to content

Commit

Permalink
up: support fire event on sub-cmd and set app to sub-cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 2, 2022
1 parent 2a39058 commit a272ae3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
8 changes: 8 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use function array_unshift;
use function class_exists;
use function implode;
use function is_dir;
use function is_object;
use function is_string;
use function str_replace;
Expand Down Expand Up @@ -405,6 +406,10 @@ public function commands(array $commands): void
*/
public function registerCommands(string $namespace, string $basePath): static
{
if (!is_dir($basePath)) {
return $this;
}

$this->debugf('register commands from the namespace: %s', $namespace);

$length = strlen($basePath) + 1;
Expand All @@ -430,6 +435,9 @@ public function registerCommands(string $namespace, string $basePath): static
*/
public function registerGroups(string $namespace, string $basePath): self
{
if (!is_dir($basePath)) {
return $this;
}
$this->debugf('register groups from the namespace: %s', $namespace);

$length = strlen($basePath) + 1;
Expand Down
10 changes: 9 additions & 1 deletion src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ abstract class Command extends AbstractHandler implements CommandInterface
protected ?Controller $group = null;

/**
* command argument rules
*
* eg:
*
* [
* 'arg1' => 'type;desc',
* ]
*
* @return array
*/
protected function getArguments(): array
Expand All @@ -63,7 +71,7 @@ protected function beforeInitFlagsParser(FlagsParser $fs): void
*/
protected function afterInitFlagsParser(FlagsParser $fs): void
{
$this->debugf('cmd: %s - load command flags configure', $this->getRealCName());
$this->debugf('cmd: %s - load command flags configure, class: %s', $this->getRealCName(), static::class);
$this->configure();
$this->configFlags($fs);

Expand Down
37 changes: 7 additions & 30 deletions src/Decorate/AttachApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ public function getApp(): Application
}

/**
* @param Application $app
* @param Application|null $app
*/
public function setApp(Application $app): void
public function setApp(Application|null $app): void
{
$this->app = $app;
if ($app !== null) {
$this->app = $app;

// auto setting $attached
$this->attached = true;
// auto setting $attached
$this->attached = true;
}
}

/**
Expand Down Expand Up @@ -168,29 +170,4 @@ public function log(int $level, string $message, array $extra = []): void

Console::log($level, $message, $extra);
}

/**************************************************************************
* wrap trigger events
**************************************************************************/

/**
* @param string $event
* @param mixed ...$args
*
* @return bool
*/
public function fire(string $event, ...$args): bool
{
$this->debugf("fire event: $event");

// if has application instance
if ($this->attached) {
$stop = $this->app->fire($event, ...$args);
if ($stop === false) {
return false;
}
}

return $this->parentFire($event, ...$args);
}
}
5 changes: 3 additions & 2 deletions src/Decorate/SubCommandsWareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ protected function subCommands(): array
protected function dispatchSub(string $name, array $args): mixed
{
$subInfo = $this->commands[$name];
$this->debugf('cmd: %s - dispatch the attached subcommand: %s', $this->getRealName(), $name);
$this->debugf('cmd: %s - dispatch the subcommand: %s', $this->getRealName(), $name);

// create and init sub-command
$subCmd = $this->createSubCommand($subInfo);
$subCmd->setParent($this);
$subCmd->setApp($this->getApp());
$subCmd->setPath($this->path);
$subCmd->setInputOutput($this->input, $this->output);

Expand Down Expand Up @@ -167,7 +168,7 @@ public function addSub(string $name, string|CommandInterface $handler = null, ar
$name = $handler::getName();
}

Assert::isFalse(!$name || !$handler, "Command 'name' and 'handler' cannot be empty! name: $name");
Assert::isFalse(!$name || !$handler, "Command 'name' and 'handler' cannot be empty! name: $name, handler: $handler");
Assert::isFalse(isset($this->commands[$name]), "Command '$name' have been registered!");

$this->validateName($name);
Expand Down
46 changes: 39 additions & 7 deletions src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ public static function aliases(): array
*/
public function __construct(Input $input = null, Output $output = null)
{
$this->input = $input;
$this->output = $output;
// init io stream
$input && $this->setInput($input);
$output && $this->setOutput($output);

// init an flags object
$this->flags = new SFlags();
$this->setFlags(new SFlags());
$this->init();
}

Expand Down Expand Up @@ -318,6 +319,8 @@ public function run(array $args): mixed

return $this->doRun($args);
} catch (Throwable $e) {
$this->log(Console::VERB_DEBUG, "cmd: $name - run error: " . $e->getMessage(), ['args' => $args]);

if ($this->isDetached()) {
ErrorHandler::new()->handle($e);
} else {
Expand Down Expand Up @@ -348,7 +351,7 @@ protected function doRun(array $args): mixed
}

// only fire for alone command run.
if ($this->isAlone()) {
if ($this->isAloneCmd()) {
$this->fire(ConsoleEvent::COMMAND_RUN_BEFORE, $this);
}

Expand All @@ -375,6 +378,8 @@ protected function doExecute(): mixed
*/
public function coExecute(): int
{
/** @noinspection PhpFullyQualifiedNameUsageInspection */
/** @noinspection PhpUndefinedNamespaceInspection */
$cid = \Swoole\Coroutine\run(function (): void {
$this->execute($this->input, $this->output);
});
Expand Down Expand Up @@ -432,6 +437,7 @@ protected function prepare(): bool
if (function_exists('cli_set_process_title')) {
cli_set_process_title($this->processTitle);
} elseif (function_exists('setproctitle')) {
/** @noinspection PhpUndefinedFunctionInspection */
setproctitle($this->processTitle);
}

Expand All @@ -444,6 +450,32 @@ protected function prepare(): bool
return true;
}

/**************************************************************************
* wrap trigger events
**************************************************************************/

/**
* @param string $event
* @param mixed ...$args
*
* @return bool
*/
public function fire(string $event, ...$args): bool
{
$this->debugf("fire event: $event");

// if has application instance
if ($this->attached) {
$stop = $this->app->fire($event, ...$args);
if ($stop === false) {
return false;
}
}
// TODO pop fire event to parent and app

return $this->parentFire($event, ...$args);
}

/**************************************************************************
* helper methods
**************************************************************************/
Expand Down Expand Up @@ -496,7 +528,7 @@ public function loadRulesByDocblock(string $method, FlagsParser $fs): void
$rftMth = PhpHelper::reflectMethod($this, $method);

// parse doc for get flag rules
$dr = DocblockRules::newByDocblock($rftMth->getDocComment());
$dr = DocblockRules::newByDocblock((string)$rftMth->getDocComment());
$dr->parse();

$fs->addArgsByRules($dr->getArgRules());
Expand Down Expand Up @@ -582,11 +614,11 @@ public function getRealDesc(): string
/**
* @param bool $useReal
*
* @return string
* @return string top:sub
*/
public function getCommandId(bool $useReal = true): string
{
return $useReal ? self::getName() : $this->commandName;
return $this->getPath(':');
}

/**
Expand Down

0 comments on commit a272ae3

Please sign in to comment.