55namespace Yiisoft \Yii \Console ;
66
77use Psr \Container \ContainerInterface ;
8+ use ReflectionClass ;
89use RuntimeException ;
10+ use Symfony \Component \Console \Attribute \AsCommand ;
911use Symfony \Component \Console \Command \Command ;
1012use Symfony \Component \Console \Command \LazyCommand ;
1113use Symfony \Component \Console \CommandLoader \CommandLoaderInterface ;
@@ -31,6 +33,8 @@ final class CommandLoader implements CommandLoaderInterface
3133 */
3234 private array $ commandNames ;
3335
36+ private ?bool $ symfonyConsoleVersionLess8 = null ;
37+
3438 /**
3539 * @param array $commandMap An array with command names as keys and service ids as values.
3640 *
@@ -52,12 +56,7 @@ public function get(string $name): Command
5256 $ commandClass = $ this ->commandMap [$ name ]['class ' ];
5357 $ commandHidden = $ this ->commandMap [$ name ]['hidden ' ];
5458
55- /**
56- * @see https://github.com/yiisoft/yii-console/issues/229
57- * @psalm-suppress DeprecatedMethod
58- */
59- $ description = $ commandClass ::getDefaultDescription ();
60-
59+ $ description = $ this ->getCommandDescription ($ commandClass );
6160 if ($ description === null ) {
6261 return $ this ->getCommandInstance ($ name );
6362 }
@@ -154,4 +153,27 @@ private function validateAliases(array $aliases): void
154153 }
155154 }
156155 }
156+
157+ /**
158+ * @psalm-param class-string<Command> $commandClass
159+ */
160+ private function getCommandDescription (string $ commandClass ): ?string
161+ {
162+ $ this ->symfonyConsoleVersionLess8 ??= method_exists (Command::class, 'getDefaultDescription ' );
163+
164+ if ($ this ->symfonyConsoleVersionLess8 ) {
165+ /**
166+ * @see https://github.com/yiisoft/yii-console/issues/229
167+ * @psalm-suppress DeprecatedMethod, UndefinedMethod
168+ * @var string|null
169+ */
170+ return $ commandClass ::getDefaultDescription ();
171+ }
172+
173+ if ($ attribute = (new ReflectionClass ($ commandClass ))->getAttributes (AsCommand::class)) {
174+ return $ attribute [0 ]->newInstance ()->description ;
175+ }
176+
177+ return null ;
178+ }
157179}
0 commit comments