Skip to content

Commit 995f25c

Browse files
committed
[Console] Optimisation on getting the command
1 parent c83f84b commit 995f25c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,10 @@ public function find($name)
623623
}
624624
}
625625

626+
if ($this->has($name)) {
627+
return $this->get($name);
628+
}
629+
626630
$allCommands = $this->commandLoader ? array_merge($this->commandLoader->getNames(), array_keys($this->commands)) : array_keys($this->commands);
627631
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
628632
$commands = preg_grep('{^'.$expr.'}', $allCommands);
@@ -663,7 +667,6 @@ public function find($name)
663667
}));
664668
}
665669

666-
$exact = \in_array($name, $commands, true) || isset($aliases[$name]);
667670
if (\count($commands) > 1) {
668671
$usableWidth = $this->terminal->getWidth() - 10;
669672
$abbrevs = array_values($commands);
@@ -684,7 +687,7 @@ public function find($name)
684687
throw new CommandNotFoundException(sprintf("Command \"%s\" is ambiguous.\nDid you mean one of these?\n%s", $name, $suggestions), array_values($commands));
685688
}
686689

687-
return $this->get($exact ? $name : reset($commands));
690+
return $this->get($name);
688691
}
689692

690693
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
class TestAmbiguousCommandRegistering2 extends Command
8+
{
9+
protected function configure()
10+
{
11+
$this
12+
->setName('test-ambiguous2')
13+
->setDescription('The test-ambiguous2 command')
14+
;
15+
}
16+
17+
protected function execute(InputInterface $input, OutputInterface $output)
18+
{
19+
$output->write('test-ambiguous2');
20+
}
21+
}

0 commit comments

Comments
 (0)