8000 bug #35696 [Console] Don't load same-namespace alternatives on exact … · symfony/symfony@648d488 · GitHub
[go: up one dir, main page]

Skip to content

Commit 648d488

Browse files
committed
bug #35696 [Console] Don't load same-namespace alternatives on exact match (chalasr)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Don't load same-namespace alternatives on exact match | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #35479 | License | MIT | Doc PR | - <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/roadmap): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch master. --> Commits ------- 707c5ba [Console] Don't load same-namespace alternatives on exact match found
2 parents 7f92a16 + 707c5ba commit 648d488

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,15 @@ public function find($name)
645645
// filter out aliases for commands which are already on the list
646646
if (\count($commands) > 1) {
647647
$commandList = $this->commandLoader ? array_merge(array_flip($this->commandLoader->getNames()), $this->commands) : $this->commands;
648-
$commands = array_unique(array_filter($commands, function ($nameOrAlias) use (&$commandList, $commands, &$aliases) {
648+
649+
if (isset($commandList[$name])) {
650+
return $this->get($name);
651+
}
652+
653+
foreach ($commands as $k => $nameOrAlias) {
654+
if ($nameOrAlias === $name) {
655+
return $this->get($nameOrAlias);
656+
}
649657
if (!$commandList[$nameOrAlias] instanceof Command) {
650658
$commandList[$nameOrAlias] = $this->commandLoader->get($nameOrAlias);
651659
}
@@ -654,8 +662,14 @@ public function find($name)
654662

655663
$aliases[$nameOrAlias] = $commandName;
656664

657-
return $commandName === $nameOrAlias || !\in_array($commandName, $commands);
658-
}));
665+
if ($commandName === $nameOrAlias || !\in_array($commandName, $commands)) {
666+
continue;
667+
}
668+
669+
unset($commands[$k]);
670+
}
671+
672+
$commands = array_unique($commands);
659673
}
660674

661675
$exact = \in_array($name, $commands, true) || isset($aliases[$name]);

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDi 8000 ff line change
@@ -1642,6 +1642,31 @@ public function testAllExcludesDisabledLazyCommand()
16421642
$this->assertArrayNotHasKey('disabled', $application->all());
16431643
}
16441644

1645+
public function testFindAlternativesDoesNotLoadSameNamespaceCommandsOnExactMatch()
1646+
{
1647+
$application = new Application();
1648+
$application->setAutoExit(false);
1649+
1650+
$loaded = [];
1651+
1652+
$application->setCommandLoader(new FactoryCommandLoader([
1653+
'foo:bar' => function () use (&$loaded) {
1654+
$loaded['foo:bar'] = true;
1655+
1656+
return (new Command('foo:bar'))->setCode(function () {});
1657+
},
1658+
'foo' => function () use (&$loaded) {
1659+
$loaded['foo'] = true;
1660+
1661+
return (new Command('foo'))->setCode(function () {});
1662+
},
1663+
]));
1664+
1665+
$application->run(new ArrayInput(['command' => 'foo']), new NullOutput());
1666+
1667+
$this->assertSame(['foo' => true], $loaded);
1668+
}
1669+
16451670
protected function getDispatcher($skipCommand = false)
16461671
{
16471672
$dispatcher = new EventDispatcher();

0 commit comments

Comments
 (0)
0