Skip to content

Commit

Permalink
[Console] Escape % in command name & description from getDefault*()
Browse files Browse the repository at this point in the history
  • Loading branch information
ogizanagi committed Jun 6, 2022
1 parent cce7a9f commit b4a1d2d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DependencyInjection/AddConsoleCommandPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function process(ContainerBuilder $container)
if (!$r->isSubclassOf(Command::class)) {
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must be a subclass of "%s".', $id, $this->commandTag, Command::class));
}
$commandName = $class::getDefaultName();
$commandName = $class::getDefaultName() !== null ? str_replace('%', '%%', $class::getDefaultName()) : null;
}

if (null === $commandName) {
Expand Down
29 changes: 29 additions & 0 deletions Tests/DependencyInjection/AddConsoleCommandPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ public function visibilityProvider()
];
}

public function testEscapesDefaultFromPhp()
{
$container = new ContainerBuilder();
$container
->register('to-escape', EscapedDefaultsFromPhpCommand::class)
->addTag('console.command')
;

$pass = new AddConsoleCommandPass();
$pass->process($container);

$commandLoader = $container->getDefinition('console.command_loader');
$commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));

$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
$this->assertSame(['%%cmd%%' => 'to-escape'], $commandLoader->getArgument(1));
$this->assertEquals([['to-escape' => new ServiceClosureArgument(new TypedReference('to-escape', EscapedDefaultsFromPhpCommand::class))]], $commandLocator->getArguments());
$this->assertSame([], $container->getParameter('console.command.ids'));

$command = $container->get('console.command_loader')->get('%%cmd%%');

$this->assertSame('%cmd%', $command->getName());
}

public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
{
$this->expectException(\InvalidArgumentException::class);
Expand Down Expand Up @@ -250,3 +274,8 @@ class NamedCommand extends Command
{
protected static $defaultName = 'default';
}

class EscapedDefaultsFromPhpCommand extends Command
{
protected static $defaultName = '%cmd%';
}

0 comments on commit b4a1d2d

Please sign in to comment.