From ea2906819f1e4ac758a4e9bea2c39b0cb29613a4 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 13:12:19 +0100 Subject: [PATCH] Update ConsoleExtension.php Lazy loading using the service tag does not work. --- src/DI/ConsoleExtension.php | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/DI/ConsoleExtension.php b/src/DI/ConsoleExtension.php index d4093e8..0efac00 100644 --- a/src/DI/ConsoleExtension.php +++ b/src/DI/ConsoleExtension.php @@ -144,17 +144,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; }