[FrameworkBundle] Multiple services on one Command class

This commit is contained in:
Claudio Zizza 2016-07-03 23:35:32 +02:00 committed by Fabien Potencier
parent 4927993835
commit 2b82fcb437
2 changed files with 28 additions and 1 deletions

View File

@ -92,6 +92,29 @@ class AddConsoleCommandPassTest extends TestCase
$container->compile();
}
public function testProcessServicesWithSameCommand()
{
$container = new ContainerBuilder();
$container->addCompilerPass(new AddConsoleCommandPass());
$className = 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand';
$definition1 = new Definition($className);
$definition1->addTag('console.command');
$definition2 = new Definition($className);
$definition2->addTag('console.command');
$container->setDefinition('my-command1', $definition1);
$container->setDefinition('my-command2', $definition2);
$container->compile();
$alias1 = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
$alias2 = $alias1.'_my-command2';
$this->assertTrue($container->hasAlias($alias1));
$this->assertTrue($container->hasAlias($alias2));
}
}
class MyCommand extends Command

View File

@ -44,7 +44,11 @@ class AddConsoleCommandPass implements CompilerPassInterface
throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "%s".', $id, Command::class));
}
$container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
$serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
if ($container->hasAlias($serviceId)) {
$serviceId = $serviceId.'_'.$id;
}
$container->setAlias($serviceId, $id);
$serviceIds[] = $definition->isPublic() ? $id : $serviceId;
}