Skip to content

Commit 1677d9c

Browse files
committed
minor symfony#24073 [Console] Fix BC break in console.command.ids parameter (chalasr)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Fix BC break in console.command.ids parameter | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/flex#142 (comment) | License | MIT | Doc PR | n/a To make command services lazy loaded when their name is known, we need to exclude them from [runtime registration of other (non lazy-loadable) command services](https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Console/Application.php#L176). We also need to have them in the `console.command.ids` parameter in order to [not register them by convention](https://github.com/symfony/symfony/blob/3.4/src/Symfony/Component/HttpKernel/Bundle/Bundle.php#L188). It is managed using `false` as values instead of ids for the parameter, [excluding false values from runtime registration](https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Console/Application.php#L177). Problem is that it makes FrameworkBundle's 3.3 Application incompatible with Console 3.4+ given the check for `false` is missing. This PR fixes it using another parameter referencing ids of command services which can be lazy loaded. I would be happy to not add the parameter if someone has a suggestion that allows to do so. Commits ------- 99e95c3 Fix BC break in console.command.ids parameter
2 parents eff73bd + 99e95c3 commit 1677d9c

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ protected function registerCommands()
173173
}
174174

175175
if ($container->hasParameter('console.command.ids')) {
176+
$lazyCommandIds = $container->hasParameter('console.lazy_command.ids') ? $container->getParameter('console.lazy_command.ids') : array();
176177
foreach ($container->getParameter('console.command.ids') as $id) {
177-
if (false !== $id) {
178+
if (!isset($lazyCommandIds[$id])) {
178179
try {
179180
$this->add($container->get($id));
180181
} catch (\Exception $e) {

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,16 @@ private function getKernel(array $bundles, $useDispatcher = false)
183183
}
184184

185185
$container
186-
->expects($this->once())
186+
->expects($this->exactly(2))
187187
->method('hasParameter')
188-
->with($this->equalTo('console.command.ids'))
189-
->will($this->returnValue(true))
188+
->withConsecutive(array('console.command.ids'), array('console.lazy_command.ids'))
189+
->willReturnOnConsecutiveCalls(true, true)
190190
;
191191
$container
192-
->expects($this->once())
192+
->expects($this->exactly(2))
193193
->method('getParameter')
194-
->with($this->equalTo('console.command.ids'))
195-
->will($this->returnValue(array()))
194+
->withConsecutive(array('console.lazy_command.ids'), array('console.command.ids'))
195+
->willReturnOnConsecutiveCalls(array(), array())
196196
;
197197

198198
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function process(ContainerBuilder $container)
4141
$lazyCommandMap = array();
4242
$lazyCommandRefs = array();
4343
$serviceIds = array();
44+
$lazyServiceIds = array();
4445

4546
foreach ($commandServices as $id => $tags) {
4647
$definition = $container->getDefinition($id);
@@ -73,7 +74,8 @@ public function process(ContainerBuilder $container)
7374
continue;
7475
}
7576

76-
$serviceIds[$commandId] = false;
77+
$serviceIds[$commandId] = $id;
78+
$lazyServiceIds[$id] = true;
7779
unset($tags[0]);
7880
$lazyCommandMap[$commandName] = $id;
7981
$lazyCommandRefs[$id] = new TypedReference($id, $class);
@@ -98,5 +100,6 @@ public function process(ContainerBuilder $container)
98100
->setArguments(array(ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap));
99101

100102
$container->setParameter('console.command.ids', $serviceIds);
103+
$container->setParameter('console.lazy_command.ids', $lazyServiceIds);
101104
}
102105
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public function testProcessRegistersLazyCommands()
7373
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
7474
$this->assertSame(array('my:command' => 'my-command', 'my:alias' => 'my-command'), $commandLoader->getArgument(1));
7575
$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' => false), $container->getParameter('console.command.ids'));
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'));
7778
$this->assertSame(array(array('setName', array('my:command')), array('setAliases', array(array('my:alias')))), $command->getMethodCalls());
7879
}
7980

@@ -95,7 +96,8 @@ public function testProcessFallsBackToDefaultName()
9596
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
9697
$this->assertSame(array('default' => 'with-default-name'), $commandLoader->getArgument(1));
9798
$this->assertEquals(array(array('with-default-name' => new ServiceClosureArgument(new TypedReference('with-default-name', NamedCommand::class)))), $commandLocator->getArguments());
98-
$this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_namedcommand' => false), $container->getParameter('console.command.ids'));
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'));
99101

100102
$container = new ContainerBuilder();
101103
$container

0 commit comments

Comments
 (0)