From c1c36475de17f340758a5aa752e2c1cda0b1d755 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Fri, 25 Oct 2024 12:21:38 +0200 Subject: [PATCH] CommandCall: fix command name when AutoPrix or AutoDiscovery --- src/CliProgram/CliHelper.php | 41 +++++++++++++++++++ src/CliProgram/CommandCall/CommandCall.php | 5 ++- src/CliProgram/OutputHelper.php | 22 ---------- .../Requirements/TRequirementsApplication.php | 4 +- 4 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 src/CliProgram/CliHelper.php delete mode 100644 src/CliProgram/OutputHelper.php diff --git a/src/CliProgram/CliHelper.php b/src/CliProgram/CliHelper.php new file mode 100644 index 0000000..0a1646d --- /dev/null +++ b/src/CliProgram/CliHelper.php @@ -0,0 +1,41 @@ +getErrorOutput() : $output; + } + + /** + * Get the name of a command class + * + * @param Application $application The application + * @param class-string $commandClass The command class + * + * @return string|null The command name ; Null if not found + */ + public static final function getCommandNameFromClass (Application $application, string $commandClass): ?string { + foreach ($application->all() as $possibleCommand) { + if ($possibleCommand::class == $commandClass) { + return $possibleCommand->getName(); + } + } + return null; + } +} \ No newline at end of file diff --git a/src/CliProgram/CommandCall/CommandCall.php b/src/CliProgram/CommandCall/CommandCall.php index 46258fc..93f77f2 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 (string|Command $commandName): self { - $this->commandName = $commandName instanceof Command ? $commandName->getName() : $commandName; + $this->commandName = $commandName instanceof Command + ? CliHelper::getCommandNameFromClass($commandName->getApplication(), $commandName::class) + : $commandName; return $this; } diff --git a/src/CliProgram/OutputHelper.php b/src/CliProgram/OutputHelper.php deleted file mode 100644 index b92944b..0000000 --- a/src/CliProgram/OutputHelper.php +++ /dev/null @@ -1,22 +0,0 @@ -getErrorOutput() : $output; - } -} \ No newline at end of file diff --git a/src/CliProgram/Requirements/TRequirementsApplication.php b/src/CliProgram/Requirements/TRequirementsApplication.php index 7ced306..7a651cc 100644 --- a/src/CliProgram/Requirements/TRequirementsApplication.php +++ b/src/CliProgram/Requirements/TRequirementsApplication.php @@ -2,7 +2,7 @@ namespace jrosset\CliProgram\Requirements; -use jrosset\CliProgram\OutputHelper; +use jrosset\CliProgram\CliHelper; use ReflectionAttribute; use ReflectionClass; use Symfony\Component\Console\ConsoleEvents; @@ -62,7 +62,7 @@ trait TRequirementsApplication { //endregion } catch (Throwable $exception) { - $this->renderThrowable($exception, OutputHelper::getErrorOutput($commandOutput)); + $this->renderThrowable($exception, CliHelper::getErrorOutput($commandOutput)); $event->disableCommand(); $event->stopPropagation();