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

Skip to content

Commit 8806628

Browse files
committed
bug #22579 [Console][HttpKernel] Avoid reflection-based registration for command public services (chalasr)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Console][HttpKernel] Avoid reflection-based registration for command public services | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22410 (comment) | License | MIT | Doc PR | n/a By mapping commands ids by their alias in `console.command.ids` (even if the alias is not registered in the container for public services), then skipping reflection if the predictable alias exists as a key of `console.command.ids`. Please note that the whole command service registration process is far from ideal. I'm working on changing this for 3.4 in a transparent way regarding end users, leveraging PSR-11 to make the console component DI friendly, allowing to register commands as true private services (no more public aliases) and providing laziness for those. Commits ------- 6c1b384 Avoid reflection-based registration for command public services
2 parents ceabf11 + 6c1b384 commit 8806628

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function testProcess($public)
5353
}
5454

5555
$this->assertTrue($container->hasParameter('console.command.ids'));
56-
$this->assertSame(array($id), $container->getParameter('console.command.ids'));
56+
$this->assertSame(array($alias => $id), $container->getParameter('console.command.ids'));
5757
}
5858

5959
public function visibilityProvider()

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +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+
$commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
43+
if ($container->hasAlias($commandId) || isset($serviceIds[$commandId])) {
44+
$commandId = $commandId.'_'.$id;
45+
}
4246
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;
47+
$container->setAlias($commandId, $id);
48+
$id = $commandId;
4949
}
5050

51-
$serviceIds[] = $id;
51+
$serviceIds[$commandId] = $id;
5252
}
5353

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testProcess($public)
5050
}
5151

5252
$this->assertTrue($container->hasParameter('console.command.ids'));
53-
$this->assertSame(array($id), $container->getParameter('console.command.ids'));
53+
$this->assertSame(array($alias => $id), $container->getParameter('console.command.ids'));
5454
}
5555

5656
public function visibilityProvider()

src/Symfony/Component/HttpKernel/Bundle/Bundle.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ public function registerCommands(Application $application)
183183
}
184184
$class = $ns.'\\'.$file->getBasename('.php');
185185
if ($this->container) {
186+
$commandIds = $this->container->hasParameter('console.command.ids') ? $this->container->getParameter('console.command.ids') : array();
186187
$alias = 'console.command.'.strtolower(str_replace('\\', '_', $class));
187-
if ($this->container->has($alias)) {
188+
if (isset($commandIds[$alias]) || $this->container->has($alias)) {
188189
continue;
189190
}
190191
}

0 commit comments

Comments
 (0)