Skip to content

Commit

Permalink
[Console] Don't load same-namespace alternatives on exact match found
Browse files Browse the repository at this point in the history
  • Loading branch information
chalasr committed Feb 13, 2020
1 parent 9e0a39e commit 707c5ba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -645,7 +645,15 @@ public function find($name)
// filter out aliases for commands which are already on the list
if (\count($commands) > 1) {
$commandList = $this->commandLoader ? array_merge(array_flip($this->commandLoader->getNames()), $this->commands) : $this->commands;
$commands = array_unique(array_filter($commands, function ($nameOrAlias) use (&$commandList, $commands, &$aliases) {

if (isset($commandList[$name])) {
return $this->get($name);
}

foreach ($commands as $k => $nameOrAlias) {
if ($nameOrAlias === $name) {
return $this->get($nameOrAlias);
}
if (!$commandList[$nameOrAlias] instanceof Command) {
$commandList[$nameOrAlias] = $this->commandLoader->get($nameOrAlias);
}
Expand All @@ -654,8 +662,14 @@ public function find($name)

$aliases[$nameOrAlias] = $commandName;

return $commandName === $nameOrAlias || !\in_array($commandName, $commands);
}));
if ($commandName === $nameOrAlias || !\in_array($commandName, $commands)) {
continue;
}

unset($commands[$k]);
}

$commands = array_unique($commands);
}

$exact = \in_array($name, $commands, true) || isset($aliases[$name]);
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -1642,6 +1642,31 @@ public function testAllExcludesDisabledLazyCommand()
$this->assertArrayNotHasKey('disabled', $application->all());
}

public function testFindAlternativesDoesNotLoadSameNamespaceCommandsOnExactMatch()
{
$application = new Application();
$application->setAutoExit(false);

$loaded = [];

$application->setCommandLoader(new FactoryCommandLoader([
'foo:bar' => function () use (&$loaded) {
$loaded['foo:bar'] = true;

return (new Command('foo:bar'))->setCode(function () {});
},
'foo' => function () use (&$loaded) {
$loaded['foo'] = true;

return (new Command('foo'))->setCode(function () {});
},
]));

$application->run(new ArrayInput(['command' => 'foo']), new NullOutput());

$this->assertSame(['foo' => true], $loaded);
}

protected function getDispatcher($skipCommand = false)
{
$dispatcher = new EventDispatcher();
Expand Down

0 comments on commit 707c5ba

Please sign in to comment.