8000 [Console] Escape % in command name & description from getDefault*() · symfony/symfony@7b6a485 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b6a485

Browse files
committed
[Console] Escape % in command name & description from getDefault*()
1 parent 18aea63 commit 7b6a485

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function process(ContainerBuilder $container)
6767
if (!$r->isSubclassOf(Command::class)) {
6868
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
6969
}
70-
$aliases = $class::getDefaultName();
70+
$aliases = str_replace('%', '%%', $class::getDefaultName());
7171
}
7272

7373
$aliases = explode('|', $aliases ?? '');
@@ -124,7 +124,7 @@ public function process(ContainerBuilder $container)
124124
if (!$r->isSubclassOf(Command::class)) {
125125
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
126126
}
127-
$description = $class::getDefaultDescription();
127+
$description = str_replace('%', '%%', $class::getDefaultDescription());
128128
}
129129

130130
if ($description) {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,33 @@ public function testProcessFallsBackToDefaultDescription()
153153
$this->assertSame(1 + $initCounter, DescribedCommand::$initCounter);
154154
}
155155

156+
public function testEscapesDefaultFromPhp()
157+
{
158+
$container = new ContainerBuilder();
159+
$container
160+
->register('to-escape', EscapedDefaultsFromPhpCommand::class)
161+
->addTag('console.command')
162+
;
163+
164+
$pass = new AddConsoleCommandPass();
165+
$pass->process($container);
166+
167+
$commandLoader = $container->getDefinition('console.command_loader');
168+
$commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
169+
170+
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
171+
$this->assertSame(['%%cmd%%' => 'to-escape', '%%cmdalias%%' => 'to-escape'], $commandLoader->getArgument(1));
172+
$this->assertEquals([['to-escape' => new ServiceClosureArgument(new Reference('.to-escape.lazy'))]], $commandLocator->getArguments());
173+
$this->assertSame([], $container->getParameter('console.command.ids'));
174+
175+
$command = $container->get('console.command_loader')->get('%%cmd%%');
176+
177+
$this->assertInstanceOf(LazyCommand::class, $command);
178+
$this->assertSame('%cmd%', $command->getName());
179+
$this->assertSame(['%cmdalias%'], $command->getAliases());
180+
$this->assertSame('Creates a 80% discount', $command->getDescription());
181+
}
182+
156183
public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
157184
{
158185
$this->expectException(\InvalidArgumentException::class);
@@ -286,6 +313,12 @@ class NamedCommand extends Command
286313
protected static $defaultName = 'default';
287314
}
288315

316+
class EscapedDefaultsFromPhpCommand extends Command
317+
{
318+
protected static $defaultName = '%cmd%|%cmdalias%';
319+
protected static $defaultDescription = 'Creates a 80% discount';
320+
}
321+
289322
class DescribedCommand extends Command
290323
{
291324
public static $initCounter = 0;

0 commit comments

Comments
 (0)
0