From 8a71aa31bbb1f6be8f7e507ff5273e9d775333b2 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Mon, 17 Jul 2017 18:13:39 +0200 Subject: [PATCH] Fix registering lazy command services with autoconfigure enabled --- .../DependencyInjection/AddConsoleCommandPass.php | 13 ++++--------- .../Component/Console/Tests/ApplicationTest.php | 5 +++-- .../AddConsoleCommandPassTest.php | 8 +++++--- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php b/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php index 1ceed91573..67034098c2 100644 --- a/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php +++ b/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php @@ -70,20 +70,15 @@ class AddConsoleCommandPass implements CompilerPassInterface $serviceIds[$commandId] = false; $commandName = $tags[0]['command']; + unset($tags[0]); $lazyCommandMap[$commandName] = $id; $lazyCommandRefs[$id] = new TypedReference($id, $class); $aliases = array(); foreach ($tags as $tag) { - if (!isset($tag['command'])) { - throw new InvalidArgumentException(sprintf('Missing "command" attribute on tag "%s" for service "%s".', $this->commandTag, $id)); - } - if ($commandName !== $tag['command']) { - throw new InvalidArgumentException(sprintf('The "command" attribute must be the same on each "%s" tag for service "%s".', $this->commandTag, $id)); - } - if (isset($tag['alias'])) { - $aliases[] = $tag['alias']; - $lazyCommandMap[$tag['alias']] = $id; + if (isset($tag['command'])) { + $aliases[] = $tag['command']; + $lazyCommandMap[$tag['command']] = $id; } } diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 7bf76ddc8e..4409d8dedf 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -1431,8 +1431,9 @@ class ApplicationTest extends TestCase $container->addCompilerPass(new AddConsoleCommandPass()); $container ->register('lazy-command', LazyCommand::class) - ->addTag('console.command', array('command' => 'lazy:command', 'alias' => 'lazy:alias')) - ->addTag('console.command', array('command' => 'lazy:command', 'alias' => 'lazy:alias2')); + ->addTag('console.command', array('command' => 'lazy:command')) + ->addTag('console.command', array('command' => 'lazy:alias')) + ->addTag('console.command', array('command' => 'lazy:alias2')); $container->compile(); $application = new Application(); diff --git a/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php b/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php index 6454dd63df..596d9e0d2d 100644 --- a/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php +++ b/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php @@ -56,13 +56,14 @@ class AddConsoleCommandPassTest extends TestCase $this->assertSame(array($alias => $id), $container->getParameter('console.command.ids')); } - public function testProcessRegisterLazyCommands() + public function testProcessRegistersLazyCommands() { $container = new ContainerBuilder(); - $container + $command = $container ->register('my-command', MyCommand::class) ->setPublic(false) - ->addTag('console.command', array('command' => 'my:command', 'alias' => 'my:alias')) + ->addTag('console.command', array('command' => 'my:command')) + ->addTag('console.command', array('command' => 'my:alias')) ; (new AddConsoleCommandPass())->process($container); @@ -74,6 +75,7 @@ class AddConsoleCommandPassTest extends TestCase $this->assertSame(array('my:command' => 'my-command', 'my:alias' => 'my-command'), $commandLoader->getArgument(1)); $this->assertEquals(array(array('my-command' => new ServiceClosureArgument(new TypedReference('my-command', MyCommand::class)))), $commandLocator->getArguments()); $this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_mycommand' => false), $container->getParameter('console.command.ids')); + $this->assertSame(array(array('setName', array('my:command')), array('setAliases', array(array('my:alias')))), $command->getMethodCalls()); } public function visibilityProvider()