From 147eb793d7c8742442bad2b96bf0a9b498a7a784 Mon Sep 17 00:00:00 2001 From: Ener-Getick Date: Thu, 10 Mar 2016 16:31:27 +0100 Subject: [PATCH] Allow to register commands privately --- .../Compiler/AddConsoleCommandPass.php | 10 ++--- .../Compiler/AddConsoleCommandPassTest.php | 37 ++++++++++--------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConsoleCommandPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConsoleCommandPass.php index 4ab0d82ed120a..09e8948ded0f3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConsoleCommandPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConsoleCommandPass.php @@ -24,14 +24,11 @@ class AddConsoleCommandPass implements CompilerPassInterface public function process(ContainerBuilder $container) { $commandServices = $container->findTaggedServiceIds('console.command'); + $serviceIds = array(); foreach ($commandServices as $id => $tags) { $definition = $container->getDefinition($id); - if (!$definition->isPublic()) { - throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be public.', $id)); - } - if ($definition->isAbstract()) { throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id)); } @@ -40,9 +37,10 @@ public function process(ContainerBuilder $container) if (!is_subclass_of($class, 'Symfony\\Component\\Console\\Command\\Command')) { throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id)); } - $container->setAlias('console.command.'.strtolower(str_replace('\\', '_', $class)), $id); + $container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id); + $serviceIds[] = $serviceId; } - $container->setParameter('console.command.ids', array_keys($commandServices)); + $container->setParameter('console.command.ids', $serviceIds); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php index 9750744859966..72902c99cd4d0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php @@ -19,41 +19,42 @@ class AddConsoleCommandPassTest extends \PHPUnit_Framework_TestCase { - public function testProcess() + /** + * @dataProvider visibilityProvider + */ + public function testProcess($public) { $container = new ContainerBuilder(); $container->addCompilerPass(new AddConsoleCommandPass()); $container->setParameter('my-command.class', 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand'); $definition = new Definition('%my-command.class%'); + $definition->setPublic($public); $definition->addTag('console.command'); $container->setDefinition('my-command', $definition); $container->compile(); $alias = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand'; - $this->assertTrue($container->hasAlias($alias)); - $this->assertSame('my-command', (string) $container->getAlias($alias)); + if ($container->hasAlias($alias)) { + $this->assertSame('my-command', (string) $container->getAlias($alias)); + } else { + // 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('my-command'), $container->getParameter('console.command.ids')); + $this->assertSame(array('console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand'), $container->getParameter('console.command.ids')); } - /** - * @expectedException \InvalidArgumentException - * @expectedExceptionMessage The service "my-command" tagged "console.command" must be public. - */ - public function testProcessThrowAnExceptionIfTheServiceIsNotPublic() + public function visibilityProvider() { - $container = new ContainerBuilder(); - $container->addCompilerPass(new AddConsoleCommandPass()); - - $definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand'); - $definition->addTag('console.command'); - $definition->setPublic(false); - $container->setDefinition('my-command', $definition); - - $container->compile(); + return array( + array(true), + array(false), + ); } /**