diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php index ac30740397..bc9fc58d45 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php @@ -40,16 +40,18 @@ class AddConsoleCommandPassTest extends TestCase $container->compile(); $alias = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand'; - if ($container->hasAlias($alias)) { - $this->assertSame('my-command', (string) $container->getAlias($alias)); + + if ($public) { + $this->assertFalse($container->hasAlias($alias)); + $id = 'my-command'; } else { + $id = $alias; // The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass // in case the original service is private $this->assertFalse($container->hasDefinition('my-command')); $this->assertTrue($container->hasDefinition($alias)); } - $id = $public ? 'my-command' : 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand'; $this->assertTrue($container->hasParameter('console.command.ids')); $this->assertSame(array($id), $container->getParameter('console.command.ids')); } @@ -95,22 +97,21 @@ class AddConsoleCommandPassTest extends TestCase $container->compile(); } - public function testProcessServicesWithSameCommand() + public function testProcessPrivateServicesWithSameCommand() { $container = new ContainerBuilder(); - $container->addCompilerPass(new AddConsoleCommandPass()); $className = 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand'; $definition1 = new Definition($className); - $definition1->addTag('console.command'); + $definition1->addTag('console.command')->setPublic(false); $definition2 = new Definition($className); - $definition2->addTag('console.command'); + $definition2->addTag('console.command')->setPublic(false); $container->setDefinition('my-command1', $definition1); $container->setDefinition('my-command2', $definition2); - $container->compile(); + (new AddConsoleCommandPass())->process($container); $alias1 = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand'; $alias2 = $alias1.'_my-command2'; diff --git a/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php b/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php index c663910bda..46795949d3 100644 --- a/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php +++ b/src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php @@ -39,12 +39,16 @@ class AddConsoleCommandPass implements CompilerPassInterface throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "%s".', $id, Command::class)); } - $serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)); - if ($container->hasAlias($serviceId)) { - $serviceId = $serviceId.'_'.$id; + if (!$definition->isPublic()) { + $serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)); + if ($container->hasAlias($serviceId)) { + $serviceId = $serviceId.'_'.$id; + } + $container->setAlias($serviceId, $id); + $id = $serviceId; } - $container->setAlias($serviceId, $id); - $serviceIds[] = $definition->isPublic() ? $id : $serviceId; + + $serviceIds[] = $id; } $container->setParameter('console.command.ids', $serviceIds); diff --git a/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php b/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php index c6af56a0a1..f5f814e7c4 100644 --- a/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php +++ b/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php @@ -26,7 +26,6 @@ class AddConsoleCommandPassTest extends TestCase public function testProcess($public) { $container = new ContainerBuilder(); - $container->setResourceTracking(false); $container->addCompilerPass(new AddConsoleCommandPass()); $container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand'); @@ -37,7 +36,19 @@ class AddConsoleCommandPassTest extends TestCase $container->compile(); - $id = $public ? 'my-command' : 'console.command.symfony_component_console_tests_dependencyinjection_mycommand'; + $alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand'; + + if ($public) { + $this->assertFalse($container->hasAlias($alias)); + $id = 'my-command'; + } else { + $id = $alias; + // The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass + // in case the original service is private + $this->assertFalse($container->hasDefinition('my-command')); + $this->assertTrue($container->hasDefinition($alias)); + } + $this->assertTrue($container->hasParameter('console.command.ids')); $this->assertSame(array($id), $container->getParameter('console.command.ids')); } @@ -84,6 +95,28 @@ class AddConsoleCommandPassTest extends TestCase $container->compile(); } + + public function testProcessPrivateServicesWithSameCommand() + { + $container = new ContainerBuilder(); + $className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand'; + + $definition1 = new Definition($className); + $definition1->addTag('console.command')->setPublic(false); + + $definition2 = new Definition($className); + $definition2->addTag('console.command')->setPublic(false); + + $container->setDefinition('my-command1', $definition1); + $container->setDefinition('my-command2', $definition2); + + (new AddConsoleCommandPass())->process($container); + + $alias1 = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand'; + $alias2 = $alias1.'_my-command2'; + $this->assertTrue($container->hasAlias($alias1)); + $this->assertTrue($container->hasAlias($alias2)); + } } class MyCommand extends Command