From ca6db43fed1986bfa9b4f7e926fb19905efe4013 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Mon, 28 Oct 2024 11:17:48 +0100 Subject: [PATCH] CommandCall: fix command name when AutoPrix or AutoDiscovery --- src/CliProgram/BaseCommand.php | 48 ++++++++++++++++++---- src/CliProgram/CommandCall/CommandCall.php | 5 ++- tests/Commands/Hello.php | 2 +- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/CliProgram/BaseCommand.php b/src/CliProgram/BaseCommand.php index 43bcb4c..0d07708 100644 --- a/src/CliProgram/BaseCommand.php +++ b/src/CliProgram/BaseCommand.php @@ -14,19 +14,49 @@ use Throwable; */ class BaseCommand extends Command { /** - * Initialize the command - * - * @param string|null $name The command name, Null = class name - * @param string ...$aliases The command aliases + * @return string|null The command default name */ - public function __construct (?string $name = null, string ...$aliases) { - if ($name === null) { - $classShortName = (new ReflectionClass($this))->getShortName(); - $name = mb_strtolower(mb_substr($classShortName, 0, 1)) . mb_substr($classShortName, 1); + public static function getDefaultName (): ?string { + $reflectionClass = new ReflectionClass(static::class); + if (($name = parent::getDefaultName()) !== null) { + return $name; } + $classShortName = $reflectionClass->getShortName(); + return mb_strtolower(mb_substr($classShortName, 0, 1)) . mb_substr($classShortName, 1); + } + /** + * @return string[] The command default aliases + */ + public static function getDefaultAliases (): array { + return []; + } + /** + * @return string|null The command default description + */ + public static function getDefaultDescription (): ?string { + return parent::getDefaultDescription(); + } + /** + * @return bool Is the command hidden from command list by default ? + */ + public static function getDefaultHidden (): bool { + return false; + } + + /** + * Initialize the command + * + * @param string|null $name The command name ; Null if the default one (cf. {@see static::getDefaultName()}) + * @param string[]|string|null $aliases The command aliases ; Null if the default one (cf. {@see static::getDefaultAliases()}) + * @param string|null $description The command description ; Null if the default one (cf. {@see static::getDefaultDescription()}) + * @param bool|null $hidden Is the command hidden from command list ? Null if the default one (cf. {@see static::getDefaultHidden()}) + */ + public function __construct (?string $name = null, $aliases = null, ?string $description = null, ?bool $hidden = null) { parent::__construct($name); - $this->setAliases($aliases); + $this->setAliases(is_string($aliases) ? [$aliases] : $aliases ?? static::getDefaultAliases()); + $this->setDescription($description ?? $this->getDescription()); + $this->setHidden($hidden ?? static::getDefaultHidden()); } /** diff --git a/src/CliProgram/CommandCall/CommandCall.php b/src/CliProgram/CommandCall/CommandCall.php index d1bb2a0..7b2a72e 100644 --- a/src/CliProgram/CommandCall/CommandCall.php +++ b/src/CliProgram/CommandCall/CommandCall.php @@ -3,6 +3,7 @@ namespace jrosset\CliProgram\CommandCall; use Arrayy\Arrayy; +use jrosset\CliProgram\CliHelper; use Symfony\Component\Console\Application; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArrayInput; @@ -74,7 +75,9 @@ class CommandCall { * @return $this */ public function setCommandName ($commandName): self { - $this->commandName = $commandName instanceof Command ? $commandName->getName() : $commandName; + $this->commandName = $commandName instanceof Command + ? CliHelper::getCommandNameFromClass($commandName->getApplication(), get_class($commandName)) + : $commandName; return $this; } diff --git a/tests/Commands/Hello.php b/tests/Commands/Hello.php index 7a2dc9f..5cf7782 100644 --- a/tests/Commands/Hello.php +++ b/tests/Commands/Hello.php @@ -37,7 +37,7 @@ class Hello extends CommandWithValidation implements IRequirements { /** * @inheritDoc */ - protected function configure () { + protected function configure (): void { parent::configure(); $this->addArgument(