Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42e5b4e

Browse files
committed
feature #22120 [FrameworkBundle] Multiple services on one Command class (SenseException)
This PR was squashed before being merged into the 3.3-dev branch (closes #22120). Discussion ---------- [FrameworkBundle] Multiple services on one Command class rebased version of #19305 Commits ------- 2b82fcb [FrameworkBundle] Multiple services on one Command class
2 parents ce9cd71 + 2b82fcb commit 42e5b4e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
9292

9393
$container->compile();
9494
}
95+
96+
public function testProcessServicesWithSameCommand()
97+
{
98+
$container = new ContainerBuilder();
99+
$container->addCompilerPass(new AddConsoleCommandPass());
100+
$className = 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand';
101+
102+
$definition1 = new Definition($className);
103+
$definition1->addTag('console.command');
104+
105+
$definition2 = new Definition($className);
106+
$definition2->addTag('console.command');
107+
108+
$container->setDefinition('my-command1', $definition1);
109+
$container->setDefinition('my-command2', $definition2);
110+
111+
$container->compile();
112+
113+
$alias1 = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
114+
$alias2 = $alias1.'_my-command2';
115+
$this->assertTrue($container->hasAlias($alias1));
116+
$this->assertTrue($container->hasAlias($alias2));
117+
}
95118
}
96119

97120
class MyCommand extends Command

src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ public function process(ContainerBuilder $container)
4444
throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "%s".', $id, Command::class));
4545
}
4646

47-
$container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
47+
$serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
48+
if ($container->hasAlias($serviceId)) {
49+
$serviceId = $serviceId.'_'.$id;
50+
}
51+
$container->setAlias($serviceId, $id);
4852
$serviceIds[] = $definition->isPublic() ? $id : $serviceId;
4953
}
5054

0 commit comments

Comments
 (0)