1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cop package. |
5
|
|
|
* |
6
|
|
|
* (c) Phalcon Team <[email protected]> |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Phalcon\Cop; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Phalcon\Cop\Parser |
18
|
|
|
* |
19
|
|
|
* @package Phalcon\Cop |
20
|
|
|
*/ |
21
|
|
|
class Parser |
22
|
|
|
{ |
23
|
|
|
/** @var array */ |
24
|
|
|
private array $parsedCommands = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get value from parsed parameters. |
28
|
|
|
* |
29
|
|
|
* @param string|int $key The parameter's "key" |
30
|
|
|
* @param mixed $default A default value in case the key is not set |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function get(int|string $key, mixed $default = null): mixed |
35
|
|
|
{ |
36
|
|
|
if (!$this->has($key)) { |
37
|
|
|
return $default; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $this->parsedCommands[$key]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
33 |
|
* Get boolean from parsed parameters. |
45
|
|
|
* |
46
|
33 |
|
* @param string $key The parameter's "key" |
47
|
|
|
* @param bool $default A default value in case the key is not set |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function getBoolean(string $key, bool $default = false): bool |
52
|
|
|
{ |
53
|
|
|
if (!$this->has($key)) { |
54
|
|
|
return $default; |
55
|
|
|
} |
56
|
21 |
|
|
57
|
|
|
if ( |
58
|
21 |
|
is_bool($this->parsedCommands[$key]) || |
59
|
14 |
|
is_int($this->parsedCommands[$key]) |
60
|
|
|
) { |
61
|
|
|
return (bool)$this->parsedCommands[$key]; |
62
|
7 |
|
} |
63
|
|
|
|
64
|
|
|
return match ($this->parsedCommands[$key]) { |
65
|
|
|
'y', |
66
|
|
|
'yes', |
67
|
|
|
'true', |
68
|
|
|
'1', |
69
|
|
|
'on' => true, |
70
|
|
|
'n', |
71
|
|
|
'no', |
72
|
|
|
'false', |
73
|
12 |
|
'0', |
74
|
|
|
'off' => false, |
75
|
12 |
|
default => $default, |
76
|
1 |
|
}; |
77
|
|
|
} |
78
|
|
|
|
79
|
11 |
|
/** |
80
|
1 |
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
10 |
|
public function getParsedCommands(): array |
84
|
|
|
{ |
85
|
|
|
return $this->parsedCommands; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check if parsed parameters has param. |
90
|
|
|
* |
91
|
|
|
* @param string|int $key The parameter's "key" |
92
|
48 |
|
* |
93
|
|
|
* @return bool |
94
|
48 |
|
*/ |
95
|
1 |
|
public function has(int|string $key): bool |
96
|
|
|
{ |
97
|
|
|
return isset($this->parsedCommands[$key]); |
98
|
48 |
|
} |
99
|
48 |
|
|
100
|
|
|
/** |
101
|
48 |
|
* Parse console input. |
102
|
|
|
* |
103
|
|
|
* @param array $argv Arguments to parse. Defaults to empty array |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function parse(array $argv = []): array |
108
|
|
|
{ |
109
|
1 |
|
if (empty($argv)) { |
110
|
|
|
$argv = $this->getArgvFromServer(); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
array_shift($argv); |
114
|
|
|
$this->parsedCommands = []; |
115
|
|
|
|
116
|
|
|
return $this->handleArguments($argv); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets array of arguments passed from the input. |
121
|
|
|
* |
122
|
10 |
|
* @return array |
123
|
|
|
*/ |
124
|
10 |
|
protected function getArgvFromServer(): array |
125
|
|
|
{ |
126
|
|
|
return empty($_SERVER['argv']) ? [] : $_SERVER['argv']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $arg The argument passed |
131
|
|
|
* @param int $eqPos The position of where the equals sign is located |
132
|
10 |
|
* |
133
|
|
|
* @return array |
134
|
10 |
|
*/ |
135
|
10 |
|
protected function getParamWithEqual(string $arg, int $eqPos): array |
136
|
|
|
{ |
137
|
10 |
|
$out = []; |
138
|
|
|
$key = $this->stripSlashes(substr($arg, 0, $eqPos)); |
139
|
|
|
$out[$key] = substr($arg, $eqPos + 1); |
140
|
|
|
|
141
|
|
|
return $out; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Handle received parameters |
146
|
48 |
|
* |
147
|
|
|
* @param array $argv The array with the arguments passed in the CLI |
148
|
48 |
|
* |
149
|
|
|
* @return array |
150
|
48 |
|
*/ |
151
|
27 |
|
protected function handleArguments(array $argv): array |
152
|
10 |
|
{ |
153
|
|
|
$count = count($argv); |
154
|
|
|
for ($i = 0, $j = $count; $i < $j; $i++) { |
155
|
27 |
|
// --foo --bar=baz |
156
|
27 |
|
if (str_starts_with($argv[$i], '--')) { |
157
|
20 |
|
if ($this->parseAndMergeCommandWithEqualSign($argv[$i])) {// --bar=baz |
158
|
20 |
|
continue; |
159
|
20 |
|
} |
160
|
|
|
|
161
|
12 |
|
$key = $this->stripSlashes($argv[$i]); |
162
|
12 |
|
if ($i + 1 < $j && $argv[$i + 1][0] !== '-') {// --foo value |
163
|
|
|
$this->parsedCommands[$key] = $argv[$i + 1]; |
164
|
|
|
$i++; |
165
|
|
|
continue; |
166
|
26 |
|
} |
167
|
20 |
|
$this->parsedCommands[$key] = $this->parsedCommands[$key] ?? true; // --foo |
168
|
5 |
|
continue; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// -k=value -abc |
172
|
20 |
|
if (str_starts_with($argv[$i], '-')) { |
173
|
20 |
|
if ($this->parseAndMergeCommandWithEqualSign($argv[$i])) {// -k=value |
174
|
20 |
|
continue; |
175
|
|
|
} |
176
|
|
|
|
177
|
20 |
|
// -a value1 -abc value2 -abc |
178
|
15 |
|
$hasNextElementDash = !($i + 1 < $j && $argv[$i + 1][0] !== '-'); |
179
|
|
|
foreach (str_split(substr($argv[$i], 1)) as $char) { |
180
|
20 |
|
$this->parsedCommands[$char] = $hasNextElementDash |
181
|
|
|
? true |
182
|
|
|
: $argv[$i + 1]; |
183
|
11 |
|
} |
184
|
|
|
|
185
|
|
|
if (!$hasNextElementDash) {// -a value1 -abc value2 |
186
|
48 |
|
$i++; |
187
|
|
|
} |
188
|
|
|
continue; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->parsedCommands[] = $argv[$i]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this->parsedCommands; |
195
|
42 |
|
} |
196
|
|
|
|
197
|
42 |
|
/** |
198
|
|
|
* Parse command `foo=bar` |
199
|
42 |
|
* |
200
|
10 |
|
* @param string $command |
201
|
|
|
* |
202
|
10 |
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
protected function parseAndMergeCommandWithEqualSign(string $command): bool |
205
|
42 |
|
{ |
206
|
|
|
$eqPos = strpos($command, '='); |
207
|
|
|
|
208
|
|
|
if ($eqPos !== false) { |
209
|
|
|
$this->parsedCommands = array_merge( |
210
|
|
|
$this->parsedCommands, |
211
|
|
|
$this->getParamWithEqual($command, $eqPos) |
212
|
|
|
); |
213
|
|
|
|
214
|
27 |
|
return true; |
215
|
|
|
} |
216
|
27 |
|
|
217
|
27 |
|
return false; |
218
|
|
|
} |
219
|
|
|
|
220
|
27 |
|
/** |
221
|
|
|
* Delete dashes from param |
222
|
27 |
|
* |
223
|
|
|
* @param string $argument |
224
|
|
|
* |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
protected function stripSlashes(string $argument): string |
228
|
|
|
{ |
229
|
8 |
|
if (!str_starts_with($argument, '-')) { |
230
|
|
|
return $argument; |
231
|
8 |
|
} |
232
|
|
|
|
233
|
|
|
$argument = substr($argument, 1); |
234
|
|
|
|
235
|
|
|
return $this->stripSlashes($argument); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|