Don't create unused aliases for public command

This commit is contained in:
Robin Chalas 2017-04-12 23:03:09 +02:00
parent 4f0daa740a
commit cd4a01c247
3 changed files with 53 additions and 15 deletions

View File

@ -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';

View File

@ -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);

View File

@ -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