Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2b82fcb

Browse filesBrowse files
SenseExceptionfabpot
authored andcommitted
[FrameworkBundle] Multiple services on one Command class
1 parent 4927993 commit 2b82fcb
Copy full SHA for 2b82fcb

File tree

Expand file treeCollapse file tree

2 files changed

+28
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+28
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php
+23Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php
+5-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.