Skip to content

Commit d8212cb

Browse files
committed
bug symfony#46595 [Console] Escape % in command name & description from getDefaultName() (ogizanagi)
This PR was merged into the 4.4 branch. Discussion ---------- [Console] Escape % in command name & description from getDefaultName() | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix symfony#46560 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | N/A Backport of symfony#46574 for 4.4 Commits ------- 2ace20a [Console] Escape % in command name & description from getDefault*()
2 parents 924670d + 2ace20a commit d8212cb

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function process(ContainerBuilder $container)
5555
if (!$r->isSubclassOf(Command::class)) {
5656
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
5757
}
58-
$commandName = $class::getDefaultName();
58+
$commandName = $class::getDefaultName() !== null ? str_replace('%', '%%', $class::getDefaultName()) : null;
5959
}
6060

6161
if (null === $commandName) {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,30 @@ public function visibilityProvider()
118118
];
119119
}
120120

121+
public function testEscapesDefaultFromPhp()
122+
{
123+
$container = new ContainerBuilder();
124+
$container
125+
->register('to-escape', EscapedDefaultsFromPhpCommand::class)
126+
->addTag('console.command')
127+
;
128+
129+
$pass = new AddConsoleCommandPass();
130+
$pass->process($container);
131+
132+
$commandLoader = $container->getDefinition('console.command_loader');
133+
$commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
134+
135+
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
136+
$this->assertSame(['%%cmd%%' => 'to-escape'], $commandLoader->getArgument(1));
137+
$this->assertEquals([['to-escape' => new ServiceClosureArgument(new TypedReference('to-escape', EscapedDefaultsFromPhpCommand::class))]], $commandLocator->getArguments());
138+
$this->assertSame([], $container->getParameter('console.command.ids'));
139+
140+
$command = $container->get('console.command_loader')->get('%%cmd%%');
141+
142+
$this->assertSame('%cmd%', $command->getName());
143+
}
144+
121145
public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
122146
{
123147
$this->expectException(\InvalidArgumentException::class);
@@ -250,3 +274,8 @@ class NamedCommand extends Command
250274
{
251275
protected static $defaultName = 'default';
252276
}
277+
278+
class EscapedDefaultsFromPhpCommand extends Command
279+
{
280+
protected static $defaultName = '%cmd%';
281+
}

0 commit comments

Comments
 (0)