4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\TabCompletion\Matcher;
14 use Psy\Command\Command;
17 * A Psy Command tab completion Matcher.
19 * This matcher provides completion for all registered Psy Command names and
22 * @author Marc Garcia <markcial@gmail.com>
24 class CommandsMatcher extends AbstractMatcher
27 protected $commands = array();
30 * CommandsMatcher constructor.
32 * @param Command[] $commands
34 public function __construct(array $commands)
36 $this->setCommands($commands);
40 * Set Commands for completion.
42 * @param Command[] $commands
44 public function setCommands(array $commands)
47 foreach ($commands as $command) {
48 $names = array_merge(array($command->getName()), $names);
49 $names = array_merge($command->getAliases(), $names);
51 $this->commands = $names;
55 * Check whether a command $name is defined.
61 protected function isCommand($name)
63 return in_array($name, $this->commands);
67 * Check whether input matches a defined command.
73 protected function matchCommand($name)
75 foreach ($this->commands as $cmd) {
76 if ($this->startsWith($name, $cmd)) {
87 public function getMatches(array $tokens, array $info = array())
89 $input = $this->getInput($tokens);
91 return array_filter($this->commands, function ($command) use ($input) {
92 return AbstractMatcher::startsWith($input, $command);
99 public function hasMatched(array $tokens)
101 /* $openTag */ array_shift($tokens);
102 $command = array_shift($tokens);
105 case self::tokenIs($command, self::T_STRING) &&
106 !$this->isCommand($command[1]) &&
107 $this->matchCommand($command[1]) &&