Skip to content

Commit a272ae3

Browse files
committed
up: support fire event on sub-cmd and set app to sub-cmd
1 parent 2a39058 commit a272ae3

File tree

5 files changed

+66
-40
lines changed

5 files changed

+66
-40
lines changed

src/Application.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use function array_unshift;
3030
use function class_exists;
3131
use function implode;
32+
use function is_dir;
3233
use function is_object;
3334
use function is_string;
3435
use function str_replace;
@@ -405,6 +406,10 @@ public function commands(array $commands): void
405406
*/
406407
public function registerCommands(string $namespace, string $basePath): static
407408
{
409+
if (!is_dir($basePath)) {
410+
return $this;
411+
}
412+
408413
$this->debugf('register commands from the namespace: %s', $namespace);
409414

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

435443
$length = strlen($basePath) + 1;

src/Command.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ abstract class Command extends AbstractHandler implements CommandInterface
4040
protected ?Controller $group = null;
4141

4242
/**
43+
* command argument rules
44+
*
45+
* eg:
46+
*
47+
* [
48+
* 'arg1' => 'type;desc',
49+
* ]
50+
*
4351
* @return array
4452
*/
4553
protected function getArguments(): array
@@ -63,7 +71,7 @@ protected function beforeInitFlagsParser(FlagsParser $fs): void
6371
*/
6472
protected function afterInitFlagsParser(FlagsParser $fs): void
6573
{
66-
$this->debugf('cmd: %s - load command flags configure', $this->getRealCName());
74+
$this->debugf('cmd: %s - load command flags configure, class: %s', $this->getRealCName(), static::class);
6775
$this->configure();
6876
$this->configFlags($fs);
6977

src/Decorate/AttachApplicationTrait.php

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ public function getApp(): Application
4545
}
4646

4747
/**
48-
* @param Application $app
48+
* @param Application|null $app
4949
*/
50-
public function setApp(Application $app): void
50+
public function setApp(Application|null $app): void
5151
{
52-
$this->app = $app;
52+
if ($app !== null) {
53+
$this->app = $app;
5354

54-
// auto setting $attached
55-
$this->attached = true;
55+
// auto setting $attached
56+
$this->attached = true;
57+
}
5658
}
5759

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

169171
Console::log($level, $message, $extra);
170172
}
171-
172-
/**************************************************************************
173-
* wrap trigger events
174-
**************************************************************************/
175-
176-
/**
177-
* @param string $event
178-
* @param mixed ...$args
179-
*
180-
* @return bool
181-
*/
182-
public function fire(string $event, ...$args): bool
183-
{
184-
$this->debugf("fire event: $event");
185-
186-
// if has application instance
187-
if ($this->attached) {
188-
$stop = $this->app->fire($event, ...$args);
189-
if ($stop === false) {
190-
return false;
191-
}
192-
}
193-
194-
return $this->parentFire($event, ...$args);
195-
}
196173
}

src/Decorate/SubCommandsWareTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ protected function subCommands(): array
117117
protected function dispatchSub(string $name, array $args): mixed
118118
{
119119
$subInfo = $this->commands[$name];
120-
$this->debugf('cmd: %s - dispatch the attached subcommand: %s', $this->getRealName(), $name);
120+
$this->debugf('cmd: %s - dispatch the subcommand: %s', $this->getRealName(), $name);
121121

122122
// create and init sub-command
123123
$subCmd = $this->createSubCommand($subInfo);
124124
$subCmd->setParent($this);
125+
$subCmd->setApp($this->getApp());
125126
$subCmd->setPath($this->path);
126127
$subCmd->setInputOutput($this->input, $this->output);
127128

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

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

173174
$this->validateName($name);

src/Handler/AbstractHandler.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@ public static function aliases(): array
130130
*/
131131
public function __construct(Input $input = null, Output $output = null)
132132
{
133-
$this->input = $input;
134-
$this->output = $output;
133+
// init io stream
134+
$input && $this->setInput($input);
135+
$output && $this->setOutput($output);
135136

136137
// init an flags object
137-
$this->flags = new SFlags();
138+
$this->setFlags(new SFlags());
138139
$this->init();
139140
}
140141

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

319320
return $this->doRun($args);
320321
} catch (Throwable $e) {
322+
$this->log(Console::VERB_DEBUG, "cmd: $name - run error: " . $e->getMessage(), ['args' => $args]);
323+
321324
if ($this->isDetached()) {
322325
ErrorHandler::new()->handle($e);
323326
} else {
@@ -348,7 +351,7 @@ protected function doRun(array $args): mixed
348351
}
349352

350353
// only fire for alone command run.
351-
if ($this->isAlone()) {
354+
if ($this->isAloneCmd()) {
352355
$this->fire(ConsoleEvent::COMMAND_RUN_BEFORE, $this);
353356
}
354357

@@ -375,6 +378,8 @@ protected function doExecute(): mixed
375378
*/
376379
public function coExecute(): int
377380
{
381+
/** @noinspection PhpFullyQualifiedNameUsageInspection */
382+
/** @noinspection PhpUndefinedNamespaceInspection */
378383
$cid = \Swoole\Coroutine\run(function (): void {
379384
$this->execute($this->input, $this->output);
380385
});
@@ -432,6 +437,7 @@ protected function prepare(): bool
432437
if (function_exists('cli_set_process_title')) {
433438
cli_set_process_title($this->processTitle);
434439
} elseif (function_exists('setproctitle')) {
440+
/** @noinspection PhpUndefinedFunctionInspection */
435441
setproctitle($this->processTitle);
436442
}
437443

@@ -444,6 +450,32 @@ protected function prepare(): bool
444450
return true;
445451
}
446452

453+
/**************************************************************************
454+
* wrap trigger events
455+
**************************************************************************/
456+
457+
/**
458+
* @param string $event
459+
* @param mixed ...$args
460+
*
461+
* @return bool
462+
*/
463+
public function fire(string $event, ...$args): bool
464+
{
465+
$this->debugf("fire event: $event");
466+
467+
// if has application instance
468+
if ($this->attached) {
469+
$stop = $this->app->fire($event, ...$args);
470+
if ($stop === false) {
471+
return false;
472+
}
473+
}
474+
// TODO pop fire event to parent and app
475+
476+
return $this->parentFire($event, ...$args);
477+
}
478+
447479
/**************************************************************************
448480
* helper methods
449481
**************************************************************************/
@@ -496,7 +528,7 @@ public function loadRulesByDocblock(string $method, FlagsParser $fs): void
496528
$rftMth = PhpHelper::reflectMethod($this, $method);
497529

498530
// parse doc for get flag rules
499-
$dr = DocblockRules::newByDocblock($rftMth->getDocComment());
531+
$dr = DocblockRules::newByDocblock((string)$rftMth->getDocComment());
500532
$dr->parse();
501533

502534
$fs->addArgsByRules($dr->getArgRules());
@@ -582,11 +614,11 @@ public function getRealDesc(): string
582614
/**
583615
* @param bool $useReal
584616
*
585-
* @return string
617+
* @return string top:sub
586618
*/
587619
public function getCommandId(bool $useReal = true): string
588620
{
589-
return $useReal ? self::getName() : $this->commandName;
621+
return $this->getPath(':');
590622
}
591623

592624
/**

0 commit comments

Comments
 (0)