Skip to content

Commit 8535fec

Browse files
author
Robin Chalas
committed
minor symfony#25593 [Console] Simplify parameters in DI (Tobion)
This PR was merged into the 4.0 branch. Discussion ---------- [Console] Simplify parameters in DI | Q | A | ------------- | --- | Branch? | 4.0 | Bug fix? | no | New feature? |no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Currently the container gets filled with alot of ugly params like ``` 'console.command.ids' => array( 'console.command.symfony_bundle_frameworkbundle_command_aboutcommand' => 'console.command.about', 'console.command.symfony_bundle_frameworkbundle_command_assetsinstallcommand' => 'console.command.assets_install', 'console.command.symfony_bundle_frameworkbundle_command_cacheclearcommand' => 'console.command.cache_clear', ... ), 'console.lazy_command.ids' => array( 'console.command.about' => true, 'console.command.assets_install' => true, 'console.command.cache_clear' => true, ... ``` We can get rid of these in 4.0 with a little refactoring. - SF 4.0 does not include the auto-registration of commands anymore which was the reason why the `console.command.ids` used the class name as index to prevent commands already defined as service to not triggger auto-registration. -> The param does not need the index lookup anymore in 4.0 - What I now also changed is that this param only contains the command IDs of services that are NOT lazy loaded. This way, we don't need `console.lazy_command.ids` at all. This is a simplification of symfony#24073 and still ensures framework bundle console application is compatible with console component 3.x and 4.x Commits ------- ae47805 [Console] Simplify parameters in DI
2 parents 1a6cdfe + ae47805 commit 8535fec

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,11 @@ public function process(ContainerBuilder $container)
4141
$lazyCommandMap = array();
4242
$lazyCommandRefs = array();
4343
$serviceIds = array();
44-
$lazyServiceIds = array();
4544

4645
foreach ($commandServices as $id => $tags) {
4746
$definition = $container->getDefinition($id);
4847
$class = $container->getParameterBag()->resolveValue($definition->getClass());
4948

50-
$commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
51-
5249
if (isset($tags[0]['command'])) {
5350
$commandName = $tags[0]['command'];
5451
} else {
@@ -62,20 +59,16 @@ public function process(ContainerBuilder $container)
6259
}
6360

6461
if (null === $commandName) {
65-
if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
66-
$commandId = $commandId.'_'.$id;
67-
}
6862
if (!$definition->isPublic() || $definition->isPrivate()) {
63+
$commandId = 'console.command.public_alias.'.$id;
6964
$container->setAlias($commandId, $id)->setPublic(true);
7065
$id = $commandId;
7166
}
72-
$serviceIds[$commandId] = $id;
67+
$serviceIds[] = $id;
7368

7469
continue;
7570
}
7671

77-
$serviceIds[$commandId] = $id;
78-
$lazyServiceIds[$id] = true;
7972
unset($tags[0]);
8073
$lazyCommandMap[$commandName] = $id;
8174
$lazyCommandRefs[$id] = new TypedReference($id, $class);
@@ -101,6 +94,5 @@ public function process(ContainerBuilder $container)
10194
->setArguments(array(ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap));
10295

10396
$container->setParameter('console.command.ids', $serviceIds);
104-
$container->setParameter('console.lazy_command.ids', $lazyServiceIds);
10597
}
10698
}

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,27 @@ public function testProcess($public)
3131
$container->addCompilerPass(new AddConsoleCommandPass());
3232
$container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
3333

34+
$id = 'my-command';
3435
$definition = new Definition('%my-command.class%');
3536
$definition->setPublic($public);
3637
$definition->addTag('console.command');
37-
$container->setDefinition('my-command', $definition);
38+
$container->setDefinition($id, $definition);
3839

3940
$container->compile();
4041

41-
$alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
42+
$alias = 'console.command.public_alias.my-command';
4243

4344
if ($public) {
4445
$this->assertFalse($container->hasAlias($alias));
45-
$id = 'my-command';
4646
} else {
47-
$id = $alias;
4847
// The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
4948
// in case the original service is private
50-
$this->assertFalse($container->hasDefinition('my-command'));
49+
$this->assertFalse($container->hasDefinition($id));
5150
$this->assertTrue($container->hasDefinition($alias));
5251
}
5352

5453
$this->assertTrue($container->hasParameter('console.command.ids'));
55-
$this->assertSame(array($alias => $id), $container->getParameter('console.command.ids'));
54+
$this->assertSame(array($public ? $id : $alias), $container->getParameter('console.command.ids'));
5655
}
5756

5857
public function testProcessRegistersLazyCommands()
@@ -73,8 +72,7 @@ public function testProcessRegistersLazyCommands()
7372
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
7473
$this->assertSame(array('my:command' => 'my-command', 'my:alias' => 'my-command'), $commandLoader->getArgument(1));
7574
$this->assertEquals(array(array('my-command' => new ServiceClosureArgument(new TypedReference('my-command', MyCommand::class)))), $commandLocator->getArguments());
76-
$this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_mycommand' => 'my-command'), $container->getParameter('console.command.ids'));
77-
$this->assertSame(array('my-command' => true), $container->getParameter('console.lazy_command.ids'));
75+
$this->assertSame(array(), $container->getParameter('console.command.ids'));
7876
$this->assertSame(array(array('setName', array('my:command')), array('setAliases', array(array('my:alias')))), $command->getMethodCalls());
7977
}
8078

@@ -96,8 +94,7 @@ public function testProcessFallsBackToDefaultName()
9694
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
9795
$this->assertSame(array('default' => 'with-default-name'), $commandLoader->getArgument(1));
9896
$this->assertEquals(array(array('with-default-name' => new ServiceClosureArgument(new TypedReference('with-default-name', NamedCommand::class)))), $commandLocator->getArguments());
99-
$this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_namedcommand' => 'with-default-name'), $container->getParameter('console.command.ids'));
100-
$this->assertSame(array('with-default-name' => true), $container->getParameter('console.lazy_command.ids'));
97+
$this->assertSame(array(), $container->getParameter('console.command.ids'));
10198

10299
$container = new ContainerBuilder();
103100
$container
@@ -170,10 +167,9 @@ public function testProcessPrivateServicesWithSameCommand()
170167

171168
(new AddConsoleCommandPass())->process($container);
172169

173-
$alias1 = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
174-
$alias2 = $alias1.'_my-command2';
175-
$this->assertTrue($container->hasAlias($alias1));
176-
$this->assertTrue($container->hasAlias($alias2));
170+
$aliasPrefix = 'console.command.public_alias.';
171+
$this->assertTrue($container->hasAlias($aliasPrefix.'my-command1'));
172+
$this->assertTrue($container->hasAlias($aliasPrefix.'my-command2'));
177173
}
178174
}
179175

0 commit comments

Comments
 (0)