[FrameworkBundle] Multiple services on one Command class
This commit is contained in:
parent
4927993835
commit
2b82fcb437
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user