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