From 196d8657a27d3c2d796879ffbeff48f235714ad0 Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Sun, 22 Aug 2021 15:55:59 +0200 Subject: [PATCH] add is_enabled option to console.command tag --- src/Symfony/Component/Console/CHANGELOG.md | 1 + .../AddConsoleCommandPass.php | 4 +- .../AddConsoleCommandPassTest.php | 77 +++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index fedb08823e15b..b0643098ba014 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * Add `TesterTrait::assertCommandIsSuccessful()` to test command * Deprecate `HelperSet::setCommand()` and `getCommand()` without replacement + * Add `is_enabled` option to the `console.command` tag 5.3 --- diff --git a/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php b/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php index 743e306d07b9a..e947feef381a8 100644 --- a/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php +++ b/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php @@ -77,6 +77,8 @@ public function process(ContainerBuilder $container) $commandName = array_shift($aliases); } + $isEnabled = \array_key_exists('is_enabled', $tags[0]) ? $tags[0]['is_enabled'] : true; + if (null === $commandName) { if (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag($this->privateTagName)) { $commandId = 'console.command.public_alias.'.$id; @@ -131,7 +133,7 @@ public function process(ContainerBuilder $container) $definition->addMethodCall('setDescription', [$description]); $container->register('.'.$id.'.lazy', LazyCommand::class) - ->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id])]); + ->setArguments([$commandName, $aliases, $description, $isHidden, new ServiceClosureArgument($lazyCommandRefs[$id]), $isEnabled]); $lazyCommandRefs[$id] = new Reference('.'.$id.'.lazy'); } diff --git a/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php b/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php index aa92c76f159c3..8bef9485099ed 100644 --- a/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php +++ b/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php @@ -275,6 +275,63 @@ public function testProcessOnChildDefinitionWithoutClass() $container->compile(); } + + public function testDisabledCommandIsEnabledByDefault() + { + $container = new ContainerBuilder(); + $container + ->register('disabled-command', DisabledCommand::class) + ->addTag('console.command') + ; + + $pass = new AddConsoleCommandPass(); + $pass->process($container); + + $initCounter = DisabledCommand::$initCounter; + $command = $container->get('console.command_loader')->get('disabled'); + + $this->assertInstanceOf(LazyCommand::class, $command); + $this->assertTrue($command->isEnabled()); + $this->assertSame($initCounter, DisabledCommand::$initCounter); + } + + public function testDisabledCommandIsDisabledByOption() + { + $container = new ContainerBuilder(); + $container + ->register('disabled-command', DisabledCommand::class) + ->addTag('console.command', ['is_enabled' => false]) + ; + + $pass = new AddConsoleCommandPass(); + $pass->process($container); + + $initCounter = DisabledCommand::$initCounter; + $command = $container->get('console.command_loader')->get('disabled'); + + $this->assertInstanceOf(LazyCommand::class, $command); + $this->assertFalse($command->isEnabled()); + $this->assertSame($initCounter, DisabledCommand::$initCounter); + } + + public function testDisabledCommandIsDisabledByCommand() + { + $container = new ContainerBuilder(); + $container + ->register('disabled-command', DisabledCommand::class) + ->addTag('console.command', ['is_enabled' => null]) + ; + + $pass = new AddConsoleCommandPass(); + $pass->process($container); + + $initCounter = DisabledCommand::$initCounter; + $command = $container->get('console.command_loader')->get('disabled'); + + $this->assertInstanceOf(LazyCommand::class, $command); + $this->assertFalse($command->isEnabled()); + $this->assertSame($initCounter + 1, DisabledCommand::$initCounter); + } } class MyCommand extends Command @@ -300,3 +357,23 @@ public function __construct() parent::__construct(); } } + +class DisabledCommand extends Command +{ + public static $initCounter = 0; + + protected static $defaultName = 'disabled'; + protected static $defaultDescription = 'This command is disabled'; + + public function __construct() + { + ++self::$initCounter; + + parent::__construct(); + } + + public function isEnabled() + { + return false; + } +}