|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
4
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class Parser. |
|
8
|
|
|
* |
|
9
|
|
|
* Shameless copy/paste from Taylor Otwell's Laravel |
|
10
|
|
|
*/ |
|
11
|
|
|
class CommandParser |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Parse the given console command definition into an array. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $expression |
|
17
|
|
|
* |
|
18
|
|
|
* @throws \InvalidArgumentException |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function parse($expression) |
|
23
|
|
|
{ |
|
24
|
|
|
if (trim($expression) === '') { |
|
25
|
|
|
throw new InvalidArgumentException('Console command definition is empty.'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
preg_match('/[^\s]+/', $expression, $matches); |
|
29
|
|
|
|
|
30
|
|
|
if (isset($matches[0])) { |
|
31
|
|
|
$name = $matches[0]; |
|
32
|
|
|
} else { |
|
33
|
|
|
throw new InvalidArgumentException('Unable to determine command name from signature.'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches); |
|
37
|
|
|
|
|
38
|
|
|
$tokens = isset($matches[1]) ? $matches[1] : []; |
|
39
|
|
|
|
|
40
|
|
|
if (count($tokens)) { |
|
41
|
|
|
return array_merge([$name], static::parameters($tokens)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return [$name, [], []]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Extract all of the parameters from the tokens. |
|
49
|
|
|
* |
|
50
|
|
|
* @param array $tokens |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected static function parameters(array $tokens) |
|
55
|
|
|
{ |
|
56
|
|
|
$arguments = []; |
|
57
|
|
|
|
|
58
|
|
|
$options = []; |
|
59
|
|
|
|
|
60
|
|
|
foreach ($tokens as $token) { |
|
61
|
|
|
if (!Str::startsWith($token, '--')) { |
|
62
|
|
|
$arguments[] = static::parseArgument($token); |
|
63
|
|
|
} else { |
|
64
|
|
|
$options[] = static::parseOption(ltrim($token, '-')); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return [$arguments, $options]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Parse an argument expression. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $token |
|
75
|
|
|
* |
|
76
|
|
|
* @return \Symfony\Component\Console\Input\InputArgument |
|
77
|
|
|
*/ |
|
78
|
|
|
protected static function parseArgument($token) |
|
79
|
|
|
{ |
|
80
|
|
|
$description = null; |
|
81
|
|
|
|
|
82
|
|
View Code Duplication |
if (Str::contains($token, ' : ')) { |
|
|
|
|
|
|
83
|
|
|
list($token, $description) = explode(' : ', $token, 2); |
|
84
|
|
|
|
|
85
|
|
|
$token = trim($token); |
|
86
|
|
|
|
|
87
|
|
|
$description = trim($description); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
switch (true) { |
|
|
|
|
|
|
91
|
|
|
case Str::endsWith($token, '?*'): |
|
92
|
|
|
return new InputArgument(trim($token, '?*'), InputArgument::IS_ARRAY, $description); |
|
93
|
|
|
|
|
94
|
|
|
case Str::endsWith($token, '*'): |
|
95
|
|
|
return new InputArgument(trim($token, '*'), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description); |
|
96
|
|
|
|
|
97
|
|
|
case Str::endsWith($token, '?'): |
|
98
|
|
|
return new InputArgument(trim($token, '?'), InputArgument::OPTIONAL, $description); |
|
99
|
|
|
|
|
100
|
|
|
case preg_match('/(.+)\=(.+)/', $token, $matches): |
|
101
|
|
|
return new InputArgument($matches[1], InputArgument::OPTIONAL, $description, $matches[2]); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
default: |
|
104
|
|
|
return new InputArgument($token, InputArgument::REQUIRED, $description); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Parse an option expression. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $token |
|
112
|
|
|
* |
|
113
|
|
|
* @return \Symfony\Component\Console\Input\InputOption |
|
114
|
|
|
*/ |
|
115
|
|
|
protected static function parseOption($token) |
|
116
|
|
|
{ |
|
117
|
|
|
$description = null; |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
if (Str::contains($token, ' : ')) { |
|
|
|
|
|
|
120
|
|
|
list($token, $description) = explode(' : ', $token); |
|
121
|
|
|
$token = trim($token); |
|
122
|
|
|
$description = trim($description); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$shortcut = null; |
|
126
|
|
|
|
|
127
|
|
|
$matches = preg_split('/\s*\|\s*/', $token, 2); |
|
128
|
|
|
|
|
129
|
|
|
if (isset($matches[1])) { |
|
130
|
|
|
$shortcut = $matches[0]; |
|
131
|
|
|
$token = $matches[1]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
switch (true) { |
|
|
|
|
|
|
135
|
|
View Code Duplication |
case Str::endsWith($token, '='): |
|
|
|
|
|
|
136
|
|
|
return new InputOption(trim($token, '='), $shortcut, InputOption::VALUE_OPTIONAL, $description); |
|
137
|
|
|
|
|
138
|
|
View Code Duplication |
case Str::endsWith($token, '=*'): |
|
|
|
|
|
|
139
|
|
|
return new InputOption(trim($token, '=*'), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description); |
|
140
|
|
|
|
|
141
|
|
|
case preg_match('/(.+)\=(.+)/', $token, $matches): |
|
142
|
|
|
return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2]); |
|
143
|
|
|
|
|
144
|
|
|
default: |
|
145
|
|
|
return new InputOption($token, $shortcut, InputOption::VALUE_NONE, $description); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.