Skip to content

Commit

Permalink
Merge branch '3.4' into 4.4
Browse files Browse the repository at this point in the history
* 3.4:
  fix unix root dir issue
  sync validator translation files with master
  fix anchor
  fix links to releases page (formerly known as "roadmap")
  [Console] Don't load same-namespace alternatives on exact match found
  • Loading branch information
fabpot committed Feb 14, 2020
2 parents a65caf4 + 5afdac5 commit db75aa0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
20 changes: 17 additions & 3 deletions Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,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 @@ -681,8 +689,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);
}

if (\count($commands) > 1) {
Expand Down
25 changes: 25 additions & 0 deletions Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,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 db75aa0

Please sign in to comment.