Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
200 lars 6
 * (c) 2012-2023 Justin Hileman
148 lars 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
    protected function isCommand(string $name): bool
60
    {
61
        return \in_array($name, $this->commands);
62
    }
63
 
64
    /**
65
     * Check whether input matches a defined command.
66
     *
67
     * @param string $name
68
     */
69
    protected function matchCommand(string $name): bool
70
    {
71
        foreach ($this->commands as $cmd) {
72
            if ($this->startsWith($name, $cmd)) {
73
                return true;
74
            }
75
        }
76
 
77
        return false;
78
    }
79
 
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getMatches(array $tokens, array $info = []): array
84
    {
85
        $input = $this->getInput($tokens);
86
 
87
        return \array_filter($this->commands, function ($command) use ($input) {
88
            return AbstractMatcher::startsWith($input, $command);
89
        });
90
    }
91
 
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function hasMatched(array $tokens): bool
96
    {
97
        /* $openTag */ \array_shift($tokens);
98
        $command = \array_shift($tokens);
99
 
100
        switch (true) {
101
            case self::tokenIs($command, self::T_STRING) &&
102
                !$this->isCommand($command[1]) &&
103
                $this->matchCommand($command[1]) &&
104
                empty($tokens):
105
                return true;
106
        }
107
 
108
        return false;
109
    }
110
}