File tree Expand file tree Collapse file tree 3 files changed +26
-39
lines changed
src/Symfony/Component/Console Expand file tree Collapse file tree 3 files changed +26
-39
lines changed Original file line number Diff line number Diff line change @@ -551,23 +551,8 @@ public function addCommand(callable|Command $command): ?Command
551551 {
552552 $ this ->init ();
553553
554- if (!$ command instanceof Command) {
555- if (!\is_object ($ command ) || $ command instanceof \Closure) {
556- throw new InvalidArgumentException (\sprintf ('The command must be an instance of "%s" or an invokable object. ' , Command::class));
557- }
558-
559- /** @var AsCommand $attribute */
560- $ attribute = ((new \ReflectionObject ($ command ))->getAttributes (AsCommand::class)[0 ] ?? null )?->newInstance()
561- ?? throw new LogicException (\sprintf ('The command must use the "%s" attribute. ' , AsCommand::class));
562-
563- $ command = (new Command ($ attribute ->name ))
564- ->setDescription ($ attribute ->description ?? '' )
565- ->setHelp ($ attribute ->help ?? '' )
566- ->setCode ($ command );
567-
568- foreach ($ attribute ->usages as $ usage ) {
569- $ command ->addUsage ($ usage );
570- }
554+ if (is_callable ($ command )) {
555+ $ command = new Command ($ command );
571556 }
572557
573558 $ command ->setApplication ($ this );
Original file line number Diff line number Diff line change @@ -83,14 +83,35 @@ public static function getDefaultDescription(): ?string
8383 }
8484
8585 /**
86- * @param string|null $name The name of the command; passing null means it must be set in configure()
86+ * @param string|callable| null $name The name of the command or an invokable object that uses AsCommand attribute ; passing null means it must be set in configure()
8787 *
8888 * @throws LogicException When the command name is empty
8989 */
90- public function __construct (? string $ name = null )
90+ public function __construct (null | callable | string $ name = null )
9191 {
9292 $ this ->definition = new InputDefinition ();
9393
94+ if (is_callable ($ name )) {
95+ if (!\is_object ($ name ) || $ name instanceof \Closure) {
96+ throw new InvalidArgumentException (\sprintf ('The command must be an instance of "%s" or an invokable object. ' , Command::class));
97+ }
98+
99+ /** @var AsCommand $attribute */
100+ $ attribute = ((new \ReflectionObject ($ name ))->getAttributes (AsCommand::class)[0 ] ?? null )?->newInstance()
101+ ?? throw new LogicException (\sprintf ('The command must use the "%s" attribute. ' , AsCommand::class));
102+
103+ $ this ->setName ($ attribute ->name )
104+ ->setDescription ($ attribute ->description ?? '' )
105+ ->setHelp ($ attribute ->help ?? '' )
106+ ->setCode ($ name );
107+
108+ foreach ($ attribute ->usages as $ usage ) {
109+ $ this ->addUsage ($ usage );
110+ }
111+
112+ return ;
113+ }
114+
94115 $ attribute = ((new \ReflectionClass (static ::class))->getAttributes (AsCommand::class)[0 ] ?? null )?->newInstance();
95116
96117 if (null === $ name ) {
Original file line number Diff line number Diff line change @@ -32,26 +32,7 @@ class CommandTester
3232 public function __construct (
3333 callable |Command $ command ,
3434 ) {
35- if (!$ command instanceof Command) {
36- if (!\is_object ($ command ) || $ command instanceof \Closure) {
37- throw new InvalidArgumentException (\sprintf ('The command must be an instance of "%s" or an invokable object. ' , Command::class));
38- }
39-
40- /** @var AsCommand $attribute */
41- $ attribute = ((new \ReflectionObject ($ command ))->getAttributes (AsCommand::class)[0 ] ?? null )?->newInstance()
42- ?? throw new LogicException (\sprintf ('The command must use the "%s" attribute. ' , AsCommand::class));
43-
44- $ command = (new Command ($ attribute ->name ))
45- ->setDescription ($ attribute ->description ?? '' )
46- ->setHelp ($ attribute ->help ?? '' )
47- ->setCode ($ command );
48-
49- foreach ($ attribute ->usages as $ usage ) {
50- $ command ->addUsage ($ usage );
51- }
52- }
53-
54- $ this ->command = $ command ;
35+ $ this ->command = is_callable ($ command ) ? new Command ($ command ) : $ command ;
5536 }
5637
5738 /**
You can’t perform that action at this time.
0 commit comments