Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console;
4
 
5
use InvalidArgumentException;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
 
9
class Parser
10
{
11
    /**
12
     * Parse the given console command definition into an array.
13
     *
14
     * @param  string  $expression
15
     * @return array
16
     *
17
     * @throws \InvalidArgumentException
18
     */
19
    public static function parse($expression)
20
    {
21
        $name = static::name($expression);
22
 
23
        if (preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches) && count($matches[1])) {
24
            return array_merge([$name], static::parameters($matches[1]));
25
        }
26
 
27
        return [$name, [], []];
28
    }
29
 
30
    /**
31
     * Extract the name of the command from the expression.
32
     *
33
     * @param  string  $expression
34
     * @return string
35
     *
36
     * @throws \InvalidArgumentException
37
     */
38
    protected static function name($expression)
39
    {
40
        if (! preg_match('/[^\s]+/', $expression, $matches)) {
41
            throw new InvalidArgumentException('Unable to determine command name from signature.');
42
        }
43
 
44
        return $matches[0];
45
    }
46
 
47
    /**
48
     * Extract all of the parameters from the tokens.
49
     *
50
     * @param  array  $tokens
51
     * @return array
52
     */
53
    protected static function parameters(array $tokens)
54
    {
55
        $arguments = [];
56
 
57
        $options = [];
58
 
59
        foreach ($tokens as $token) {
60
            if (preg_match('/-{2,}(.*)/', $token, $matches)) {
61
                $options[] = static::parseOption($matches[1]);
62
            } else {
63
                $arguments[] = static::parseArgument($token);
64
            }
65
        }
66
 
67
        return [$arguments, $options];
68
    }
69
 
70
    /**
71
     * Parse an argument expression.
72
     *
73
     * @param  string  $token
74
     * @return \Symfony\Component\Console\Input\InputArgument
75
     */
76
    protected static function parseArgument($token)
77
    {
78
        [$token, $description] = static::extractDescription($token);
79
 
80
        switch (true) {
81
            case str_ends_with($token, '?*'):
82
                return new InputArgument(trim($token, '?*'), InputArgument::IS_ARRAY, $description);
83
            case str_ends_with($token, '*'):
84
                return new InputArgument(trim($token, '*'), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description);
85
            case str_ends_with($token, '?'):
86
                return new InputArgument(trim($token, '?'), InputArgument::OPTIONAL, $description);
87
            case preg_match('/(.+)\=\*(.+)/', $token, $matches):
88
                return new InputArgument($matches[1], InputArgument::IS_ARRAY, $description, preg_split('/,\s?/', $matches[2]));
89
            case preg_match('/(.+)\=(.+)/', $token, $matches):
90
                return new InputArgument($matches[1], InputArgument::OPTIONAL, $description, $matches[2]);
91
            default:
92
                return new InputArgument($token, InputArgument::REQUIRED, $description);
93
        }
94
    }
95
 
96
    /**
97
     * Parse an option expression.
98
     *
99
     * @param  string  $token
100
     * @return \Symfony\Component\Console\Input\InputOption
101
     */
102
    protected static function parseOption($token)
103
    {
104
        [$token, $description] = static::extractDescription($token);
105
 
106
        $matches = preg_split('/\s*\|\s*/', $token, 2);
107
 
108
        if (isset($matches[1])) {
109
            $shortcut = $matches[0];
110
            $token = $matches[1];
111
        } else {
112
            $shortcut = null;
113
        }
114
 
115
        switch (true) {
116
            case str_ends_with($token, '='):
117
                return new InputOption(trim($token, '='), $shortcut, InputOption::VALUE_OPTIONAL, $description);
118
            case str_ends_with($token, '=*'):
119
                return new InputOption(trim($token, '=*'), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description);
120
            case preg_match('/(.+)\=\*(.+)/', $token, $matches):
121
                return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description, preg_split('/,\s?/', $matches[2]));
122
            case preg_match('/(.+)\=(.+)/', $token, $matches):
123
                return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2]);
124
            default:
125
                return new InputOption($token, $shortcut, InputOption::VALUE_NONE, $description);
126
        }
127
    }
128
 
129
    /**
130
     * Parse the token into its token and description segments.
131
     *
132
     * @param  string  $token
133
     * @return array
134
     */
135
    protected static function extractDescription($token)
136
    {
137
        $parts = preg_split('/\s+:\s+/', trim($token), 2);
138
 
139
        return count($parts) === 2 ? $parts : [$token, ''];
140
    }
141
}