Skip to content

Commit 758b85e

Browse files
committed
[Console] Simplify using invokable commands when the component is used standalone
1 parent e532750 commit 758b85e

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console;
1313

14+
use Symfony\Component\Console\Attribute\AsCommand;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Command\CompleteCommand;
1617
use Symfony\Component\Console\Command\DumpCompletionCommand;
@@ -28,6 +29,7 @@
2829
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
2930
use Symfony\Component\Console\Exception\CommandNotFoundException;
3031
use Symfony\Component\Console\Exception\ExceptionInterface;
32+
use Symfony\Component\Console\Exception\InvalidArgumentException;
3133
use Symfony\Component\Console\Exception\LogicException;
3234
use Symfony\Component\Console\Exception\NamespaceNotFoundException;
3335
use Symfony\Component\Console\Exception\RuntimeException;
@@ -520,12 +522,12 @@ public function register(string $name): Command
520522
*
521523
* If a Command is not enabled it will not be added.
522524
*
523-
* @param Command[] $commands An array of commands
525+
* @param object[] $commands An array of commands
524526
*/
525527
public function addCommands(array $commands): void
526528
{
527529
foreach ($commands as $command) {
528-
$this->add($command);
530+
$this->addCommand($command);
529531
}
530532
}
531533

@@ -565,6 +567,31 @@ public function add(Command $command): ?Command
565567
return $command;
566568
}
567569

570+
public function addCommand(object $command): ?Command
571+
{
572+
if ($command instanceof Command) {
573+
return $this->add($command);
574+
}
575+
576+
if (!\is_callable($command)) {
577+
throw new InvalidArgumentException('The command must be an invokable object.');
578+
}
579+
if ($command instanceof \Closure) {
580+
throw new InvalidArgumentException('The command cannot be an anonymous function.');
581+
}
582+
583+
/** @var AsCommand $attribute */
584+
$attribute = ((new \ReflectionObject($command))->getAttributes(AsCommand::class)[0] ?? null)?->newInstance()
585+
?? throw new LogicException(\sprintf('The command must use the "%s" attribute.', AsCommand::class));
586+
587+
return $this->add(
588+
(new Command($attribute->name))
589+
->setDescription($attribute->description ?? '')
590+
->setHelp($attribute->help ?? '')
591+
->setCode($command)
592+
);
593+
}
594+
568595
/**
569596
* Returns a registered command by name or alias.
570597
*

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CHANGELOG
1515
* Deprecate returning a non-integer value from a `\Closure` function set via `Command::setCode()`
1616
* Mark `#[AsCommand]` attribute as `@final`
1717
* Add support for `SignalableCommandInterface` with invokable commands
18+
* Simplify using invokable commands when the component is used standalone
1819

1920
7.2
2021
---

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Attribute\AsCommand;
1717
use Symfony\Component\Console\Command\Command;
1818
use Symfony\Component\Console\Command\HelpCommand;
19+
use Symfony\Component\Console\Command\InvokableCommand;
1920
use Symfony\Component\Console\Command\LazyCommand;
2021
use Symfony\Component\Console\Command\SignalableCommandInterface;
2122
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
@@ -28,6 +29,8 @@
2829
use Symfony\Component\Console\Event\ConsoleSignalEvent;
2930
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
3031
use Symfony\Component\Console\Exception\CommandNotFoundException;
32+
use Symfony\Component\Console\Exception\InvalidArgumentException;
33+
use Symfony\Component\Console\Exception\LogicException;
3134
use Symfony\Component\Console\Exception\NamespaceNotFoundException;
3235
use Symfony\Component\Console\Helper\FormatterHelper;
3336
use Symfony\Component\Console\Helper\HelperSet;
@@ -239,6 +242,58 @@ public function testAddCommandWithEmptyConstructor()
239242
(new Application())->add(new \Foo5Command());
240243
}
241244

245+
public function testAddCommandWithExtendedCommand()
246+
{
247+
$application = new Application();
248+
$application->add($foo = new \FooCommand());
249+
$commands = $application->all();
250+
251+
$this->assertEquals($foo, $commands['foo:bar']);
252+
}
253+
254+
public function testAddCommandWithInvokableCommand()
255+
{
256+
$application = new Application();
257+
$application->addCommand($foo = new InvokableTestCommand());
258+
$commands = $application->all();
259+
260+
$this->assertInstanceOf(Command::class, $command = $commands['invokable']);
261+
$this->assertEquals(new InvokableCommand($command, $foo), (new \ReflectionObject($command))->getProperty('code')->getValue($command));
262+
}
263+
264+
public function testAddCommandWithInvokableExtendedCommand()
265+
{
266+
$application = new Application();
267+
$application->addCommand($foo = new InvokableExtendedTestCommand());
268+
$commands = $application->all();
269+
270+
$this->assertEquals($foo, $commands['invokable-extended']);
271+
}
272+
273+
/**
274+
* @dataProvider provideInvalidInvokableCommands
275+
*/
276+
public function testAddCommandThrowsExceptionOnInvalidCommand(object $command, string $expectedException, string $expectedExceptionMessage)
277+
{
278+
$application = new Application();
279+
280+
$this->expectException($expectedException);
281+
$this->expectExceptionMessage($expectedExceptionMessage);
282+
283+
$application->addCommand($command);
284+
}
285+
286+
public static function provideInvalidInvokableCommands(): iterable
287+
{
288+
yield 'not a callable' => [new class {}, InvalidArgumentException::class, 'The command must be an invokable object.'];
289+
yield 'a closure' => [function () {}, InvalidArgumentException::class, 'The command cannot be an anonymous function.'];
290+
yield 'without the #[AsCommand] attribute' => [new class {
291+
public function __invoke()
292+
{
293+
}
294+
}, LogicException::class, \sprintf('The command must use the "%s" attribute.', AsCommand::class)];
295+
}
296+
242297
public function testHasGet()
243298
{
244299
$application = new Application();
@@ -2514,6 +2569,22 @@ public function isEnabled(): bool
25142569
}
25152570
}
25162571

2572+
#[AsCommand(name: 'invokable')]
2573+
class InvokableTestCommand
2574+
{
2575+
public function __invoke(): int
2576+
{
2577+
}
2578+
}
2579+
2580+
#[AsCommand(name: 'invokable-extended')]
2581+
class InvokableExtendedTestCommand extends Command
2582+
{
2583+
public function __invoke(): int
2584+
{
2585+
}
2586+
}
2587+
25172588
#[AsCommand(name: 'signal')]
25182589
class BaseSignableCommand extends Command
25192590
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy