Skip to content

Commit bbe269f

Browse files
committed
minor symfony#22410 [FrameworkBundle] Don't create unused alias if the command is public (chalasr)
This PR was merged into the 3.3-dev branch. Discussion ---------- [FrameworkBundle] Don't create unused alias if the command is public | Q | A | ------------- | --- | Branch? | maste | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Currently, a public alias is created for any `console.command` tagged service, but this alias is unused if the service is public (as it would be useless). Commits ------- cd4a01c Don't create unused aliases for public command
2 parents 4f0daa7 + cd4a01c commit bbe269f

File tree

3 files changed

+53
-15
lines changed

3 files changed

+53
-15
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ public function testProcess($public)
4040
$container->compile();
4141

4242
$alias = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
43-
if ($container->hasAlias($alias)) {
44-
$this->assertSame('my-command', (string) $container->getAlias($alias));
43+
44+
if ($public) {
45+
$this->assertFalse($container->hasAlias($alias));
46+
$id = 'my-command';
4547
} else {
48+
$id = $alias;
4649
// The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
4750
// in case the original service is private
4851
$this->assertFalse($container->hasDefinition('my-command'));
4952
$this->assertTrue($container->hasDefinition($alias));
5053
}
5154

52-
$id = $public ? 'my-command' : 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
5355
$this->assertTrue($container->hasParameter('console.command.ids'));
5456
$this->assertSame(array($id), $container->getParameter('console.command.ids'));
5557
}
@@ -95,22 +97,21 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
9597
$container->compile();
9698
}
9799

98-
public function testProcessServicesWithSameCommand()
100+
public function testProcessPrivateServicesWithSameCommand()
99101
{
100102
$container = new ContainerBuilder();
101-
$container->addCompilerPass(new AddConsoleCommandPass());
102103
$className = 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand';
103104

104105
$definition1 = new Definition($className);
105-
$definition1->addTag('console.command');
106+
$definition1->addTag('console.command')->setPublic(false);
106107

107108
$definition2 = new Definition($className);
108-
$definition2->addTag('console.command');
109+
$definition2->addTag('console.command')->setPublic(false);
109110

110111
$container->setDefinition('my-command1', $definition1);
111112
$container->setDefinition('my-command2', $definition2);
112113

113-
$container->compile();
114+
(new AddConsoleCommandPass())->process($container);
114115

115116
$alias1 = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
116117
$alias2 = $alias1.'_my-command2';

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ public function process(ContainerBuilder $container)
3939
throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "%s".', $id, Command::class));
4040
}
4141

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

5054
$container->setParameter('console.command.ids', $serviceIds);

src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class AddConsoleCommandPassTest extends TestCase
2626
public function testProcess($public)
2727
{
2828
$container = new ContainerBuilder();
29-
$container->setResourceTracking(false);
3029
$container->addCompilerPass(new AddConsoleCommandPass());
3130
$container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
3231

@@ -37,7 +36,19 @@ public function testProcess($public)
3736

3837
$container->compile();
3938

40-
$id = $public ? 'my-command' : 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
39+
$alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
40+
41+
if ($public) {
42+
$this->assertFalse($container->hasAlias($alias));
43+
$id = 'my-command';
44+
} else {
45+
$id = $alias;
46+
// The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
47+
// in case the original service is private
48+
$this->assertFalse($container->hasDefinition('my-command'));
49+
$this->assertTrue($container->hasDefinition($alias));
50+
}
51+
4152
$this->assertTrue($container->hasParameter('console.command.ids'));
4253
$this->assertSame(array($id), $container->getParameter('console.command.ids'));
4354
}
@@ -84,6 +95,28 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
8495

8596
$container->compile();
8697
}
98+
99+
public function testProcessPrivateServicesWithSameCommand()
100+
{
101+
$container = new ContainerBuilder();
102+
$className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';
103+
104+
$definition1 = new Definition($className);
105+
$definition1->addTag('console.command')->setPublic(false);
106+
107+
$definition2 = new Definition($className);
108+
$definition2->addTag('console.command')->setPublic(false);
109+
110+
$container->setDefinition('my-command1', $definition1);
111+
$container->setDefinition('my-command2', $definition2);
112+
113+
(new AddConsoleCommandPass())->process($container);
114+
115+
$alias1 = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
116+
$alias2 = $alias1.'_my-command2';
117+
$this->assertTrue($container->hasAlias($alias1));
118+
$this->assertTrue($container->hasAlias($alias2));
119+
}
87120
}
88121

89122
class MyCommand extends Command

0 commit comments

Comments
 (0)