From 07ec27b85c4cd40d059cc769686d4bbc642d97e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Z=C3=A1ruba?= <79913795+janzarubadek@users.noreply.github.com> Date: Thu, 4 Jan 2024 20:53:18 +0100 Subject: [PATCH] ConsoleExtension: support commad tags (#76) --- src/DI/ConsoleExtension.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/DI/ConsoleExtension.php b/src/DI/ConsoleExtension.php index d4093e8..0a5bff0 100644 --- a/src/DI/ConsoleExtension.php +++ b/src/DI/ConsoleExtension.php @@ -13,6 +13,7 @@ use Nette\Http\RequestFactory; use Nette\Schema\Expect; use Nette\Schema\Schema; +use Nette\Utils\Arrays; use stdClass; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; @@ -144,17 +145,27 @@ public function beforeCompile(): void // Iterate over all commands and build commandMap foreach ($commands as $serviceName => $service) { - $commandName = call_user_func([$service->getType(), 'getDefaultName']); // @phpstan-ignore-line - - if ($commandName === null) { - throw new ServiceCreationException( - sprintf( - 'Command "%s" missing #[AsCommand] attribute', - $service->getType(), - ) - ); + $tags = $service->getTags(); + $commandDef = $tags['console.command'] ?? null; + $commandName = null; + if ($commandDef !== null) { + if (is_string($commandDef)) { + $commandName = $commandDef; + } elseif (is_array($commandDef)) { + $commandName = Arrays::get($tags['console.command'], 'name', null); + } + } else { + $commandName = call_user_func([$service->getType(), 'getDefaultName']); // @phpstan-ignore-line + + if ($commandName === null) { + throw new ServiceCreationException( + sprintf( + 'Command "%s" missing #[AsCommand] attribute', + $service->getType(), + ) + ); + } } - // Append service to command map $commandMap[$commandName] = $serviceName; }