These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Redaxscript\Console; |
||
3 | |||
4 | use Redaxscript\Request; |
||
5 | |||
6 | /** |
||
7 | * parent class to parse the command line interface |
||
8 | * |
||
9 | * @since 3.0.0 |
||
10 | * |
||
11 | * @package Redaxscript |
||
12 | * @category Console |
||
13 | * @author Henry Ruhs |
||
14 | */ |
||
15 | |||
16 | class Parser |
||
17 | { |
||
18 | /** |
||
19 | * instance of the request class |
||
20 | * |
||
21 | * @var Request |
||
22 | */ |
||
23 | |||
24 | protected $_request; |
||
25 | |||
26 | /** |
||
27 | * array of parsed arguments |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | |||
32 | protected $_argumentArray = []; |
||
33 | |||
34 | /** |
||
35 | * array of parsed options |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | |||
40 | protected $_optionArray = []; |
||
41 | |||
42 | /** |
||
43 | * constructor of the class |
||
44 | * |
||
45 | * @since 3.0.0 |
||
46 | * |
||
47 | * @param Request $request instance of the request class |
||
48 | */ |
||
49 | |||
50 | 13 | public function __construct(Request $request) |
|
51 | { |
||
52 | 13 | $this->_request = $request; |
|
53 | 13 | } |
|
54 | |||
55 | /** |
||
56 | * init the class |
||
57 | * |
||
58 | * @param string $mode name of the mode |
||
59 | * |
||
60 | * @since 3.0.0 |
||
61 | */ |
||
62 | |||
63 | 11 | public function init(string $mode = null) |
|
64 | { |
||
65 | 11 | if ($mode === 'cli') |
|
66 | { |
||
67 | 11 | $argumentArray = $this->_request->getServer('argv'); |
|
68 | 11 | unset($argumentArray[0]); |
|
69 | } |
||
70 | else |
||
71 | { |
||
72 | $argumentArray = array_filter(explode(' ', $this->_request->getPost('argv'))); |
||
73 | } |
||
74 | 11 | $this->_parseArgument($argumentArray); |
|
75 | 11 | } |
|
76 | |||
77 | /** |
||
78 | * get the value from arguments |
||
79 | * |
||
80 | * @since 3.0.0 |
||
81 | * |
||
82 | * @param string $key key of the item |
||
83 | * |
||
84 | * @return string|array|null |
||
85 | */ |
||
86 | |||
87 | 7 | public function getArgument(string $key = null) |
|
88 | { |
||
89 | 7 | if (is_array($this->_argumentArray) && array_key_exists($key, $this->_argumentArray)) |
|
90 | { |
||
91 | 1 | return $this->_argumentArray[$key]; |
|
92 | } |
||
93 | 6 | else if (!$key) |
|
94 | { |
||
95 | 5 | return $this->_argumentArray; |
|
96 | } |
||
97 | 1 | return null; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * get the value from options |
||
102 | * |
||
103 | * @since 3.0.0 |
||
104 | * |
||
105 | * @param string $key key of the item |
||
106 | * |
||
107 | * @return string|array|null |
||
108 | */ |
||
109 | |||
110 | 6 | public function getOption(string $key = null) |
|
111 | { |
||
112 | 6 | if (is_array($this->_optionArray) && array_key_exists($key, $this->_optionArray)) |
|
113 | { |
||
114 | 1 | return $this->_optionArray[$key]; |
|
115 | } |
||
116 | 5 | else if (!$key) |
|
117 | { |
||
118 | 4 | return $this->_optionArray; |
|
119 | } |
||
120 | 1 | return null; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * set the value to arguments |
||
125 | * |
||
126 | * @since 3.0.0 |
||
127 | * |
||
128 | * @param string $key key of the item |
||
129 | * @param string|int $value value of the item |
||
130 | */ |
||
131 | |||
132 | 7 | public function setArgument(string $key = null, $value = null) |
|
133 | { |
||
134 | 7 | $this->_argumentArray[$key] = $value; |
|
135 | 7 | } |
|
136 | |||
137 | /** |
||
138 | * set the value to options |
||
139 | * |
||
140 | * @since 3.0.0 |
||
141 | * |
||
142 | * @param string $key key of the item |
||
143 | * @param string|int $value value of the item |
||
144 | */ |
||
145 | |||
146 | 5 | public function setOption(string $key = null, $value = null) |
|
147 | { |
||
148 | 5 | $this->_optionArray[$key] = $value; |
|
149 | 5 | } |
|
150 | |||
151 | /** |
||
152 | * parse raw argument |
||
153 | * |
||
154 | * @since 3.0.0 |
||
155 | * |
||
156 | * @param array|null $argumentArray raw argument to be parsed |
||
157 | */ |
||
158 | |||
159 | 11 | protected function _parseArgument(?array $argumentArray = []) |
|
160 | { |
||
161 | 11 | $doSkip = false; |
|
162 | 11 | $argumentKey = 0; |
|
163 | 11 | if (is_array($argumentArray)) |
|
164 | { |
||
165 | 8 | foreach ($argumentArray as $value) |
|
166 | { |
||
167 | 8 | $next = next($argumentArray); |
|
168 | 8 | if (substr($value, 0, 1) === '-') |
|
169 | { |
||
170 | 4 | $offset = substr($value, 0, 2) === '--' ? 2 : 1; |
|
171 | 4 | $optionArray = $this->_parseOption($value, $next, $offset); |
|
172 | 4 | $doSkip = $optionArray['value'] === $next; |
|
173 | 4 | $this->setOption($optionArray['key'], $optionArray['value']); |
|
174 | } |
||
175 | 8 | else if ($value && !$doSkip) |
|
176 | { |
||
177 | 8 | $this->setArgument($argumentKey++, $value); |
|
178 | } |
||
179 | } |
||
180 | } |
||
181 | 11 | } |
|
182 | |||
183 | /** |
||
184 | * parse raw option |
||
185 | * |
||
186 | * @since 3.0.0 |
||
187 | * |
||
188 | * @param string $option raw option to be parsed |
||
189 | * @param string $next raw next to be parsed |
||
190 | * @param int $offset offset of the raw option |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | |||
195 | 4 | protected function _parseOption(string $option = null, string $next = null, int $offset = null) : array |
|
196 | { |
||
197 | 4 | $equalPosition = strpos($option, '='); |
|
198 | 4 | if ($equalPosition) |
|
199 | { |
||
200 | return |
||
201 | [ |
||
202 | 4 | 'key' => substr($option, $offset, $equalPosition - $offset), |
|
203 | 4 | 'value' => substr($option, $equalPosition + 1) |
|
204 | ]; |
||
205 | } |
||
206 | return |
||
207 | [ |
||
208 | 4 | 'key' => substr($option, $offset), |
|
209 | 4 | 'value' => !$next || substr($next, 0, 1) === '-' ? true : $next |
|
0 ignored issues
–
show
|
|||
210 | ]; |
||
211 | } |
||
212 | } |
||
213 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: