Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\TabCompletion\Matcher;
|
Chris@13
|
13
|
Chris@13
|
14 use Psy\Command\Command;
|
Chris@13
|
15
|
Chris@13
|
16 /**
|
Chris@13
|
17 * A Psy Command tab completion Matcher.
|
Chris@13
|
18 *
|
Chris@13
|
19 * This matcher provides completion for all registered Psy Command names and
|
Chris@13
|
20 * aliases.
|
Chris@13
|
21 *
|
Chris@13
|
22 * @author Marc Garcia <markcial@gmail.com>
|
Chris@13
|
23 */
|
Chris@13
|
24 class CommandsMatcher extends AbstractMatcher
|
Chris@13
|
25 {
|
Chris@13
|
26 /** @var string[] */
|
Chris@13
|
27 protected $commands = [];
|
Chris@13
|
28
|
Chris@13
|
29 /**
|
Chris@13
|
30 * CommandsMatcher constructor.
|
Chris@13
|
31 *
|
Chris@13
|
32 * @param Command[] $commands
|
Chris@13
|
33 */
|
Chris@13
|
34 public function __construct(array $commands)
|
Chris@13
|
35 {
|
Chris@13
|
36 $this->setCommands($commands);
|
Chris@13
|
37 }
|
Chris@13
|
38
|
Chris@13
|
39 /**
|
Chris@13
|
40 * Set Commands for completion.
|
Chris@13
|
41 *
|
Chris@13
|
42 * @param Command[] $commands
|
Chris@13
|
43 */
|
Chris@13
|
44 public function setCommands(array $commands)
|
Chris@13
|
45 {
|
Chris@13
|
46 $names = [];
|
Chris@13
|
47 foreach ($commands as $command) {
|
Chris@17
|
48 $names = \array_merge([$command->getName()], $names);
|
Chris@17
|
49 $names = \array_merge($command->getAliases(), $names);
|
Chris@13
|
50 }
|
Chris@13
|
51 $this->commands = $names;
|
Chris@13
|
52 }
|
Chris@13
|
53
|
Chris@13
|
54 /**
|
Chris@13
|
55 * Check whether a command $name is defined.
|
Chris@13
|
56 *
|
Chris@13
|
57 * @param string $name
|
Chris@13
|
58 *
|
Chris@13
|
59 * @return bool
|
Chris@13
|
60 */
|
Chris@13
|
61 protected function isCommand($name)
|
Chris@13
|
62 {
|
Chris@17
|
63 return \in_array($name, $this->commands);
|
Chris@13
|
64 }
|
Chris@13
|
65
|
Chris@13
|
66 /**
|
Chris@13
|
67 * Check whether input matches a defined command.
|
Chris@13
|
68 *
|
Chris@13
|
69 * @param string $name
|
Chris@13
|
70 *
|
Chris@13
|
71 * @return bool
|
Chris@13
|
72 */
|
Chris@13
|
73 protected function matchCommand($name)
|
Chris@13
|
74 {
|
Chris@13
|
75 foreach ($this->commands as $cmd) {
|
Chris@13
|
76 if ($this->startsWith($name, $cmd)) {
|
Chris@13
|
77 return true;
|
Chris@13
|
78 }
|
Chris@13
|
79 }
|
Chris@13
|
80
|
Chris@13
|
81 return false;
|
Chris@13
|
82 }
|
Chris@13
|
83
|
Chris@13
|
84 /**
|
Chris@13
|
85 * {@inheritdoc}
|
Chris@13
|
86 */
|
Chris@13
|
87 public function getMatches(array $tokens, array $info = [])
|
Chris@13
|
88 {
|
Chris@13
|
89 $input = $this->getInput($tokens);
|
Chris@13
|
90
|
Chris@17
|
91 return \array_filter($this->commands, function ($command) use ($input) {
|
Chris@13
|
92 return AbstractMatcher::startsWith($input, $command);
|
Chris@13
|
93 });
|
Chris@13
|
94 }
|
Chris@13
|
95
|
Chris@13
|
96 /**
|
Chris@13
|
97 * {@inheritdoc}
|
Chris@13
|
98 */
|
Chris@13
|
99 public function hasMatched(array $tokens)
|
Chris@13
|
100 {
|
Chris@17
|
101 /* $openTag */ \array_shift($tokens);
|
Chris@17
|
102 $command = \array_shift($tokens);
|
Chris@13
|
103
|
Chris@13
|
104 switch (true) {
|
Chris@13
|
105 case self::tokenIs($command, self::T_STRING) &&
|
Chris@13
|
106 !$this->isCommand($command[1]) &&
|
Chris@13
|
107 $this->matchCommand($command[1]) &&
|
Chris@13
|
108 empty($tokens):
|
Chris@13
|
109 return true;
|
Chris@13
|
110 }
|
Chris@13
|
111
|
Chris@13
|
112 return false;
|
Chris@13
|
113 }
|
Chris@13
|
114 }
|