comparison vendor/psy/psysh/src/TabCompletion/Matcher/CommandsMatcher.php @ 13:5fb285c0d0e3

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