@@ -40,332 +40,332 @@ |
||
40 | 40 | */ |
41 | 41 | class ArgvInput extends Input |
42 | 42 | { |
43 | - private $tokens; |
|
44 | - private $parsed; |
|
45 | - |
|
46 | - public function __construct(array $argv = null, InputDefinition $definition = null) |
|
47 | - { |
|
48 | - $argv = $argv ?? $_SERVER['argv'] ?? []; |
|
49 | - |
|
50 | - // strip the application name |
|
51 | - array_shift($argv); |
|
52 | - |
|
53 | - $this->tokens = $argv; |
|
54 | - |
|
55 | - parent::__construct($definition); |
|
56 | - } |
|
57 | - |
|
58 | - protected function setTokens(array $tokens) |
|
59 | - { |
|
60 | - $this->tokens = $tokens; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - protected function parse() |
|
67 | - { |
|
68 | - $parseOptions = true; |
|
69 | - $this->parsed = $this->tokens; |
|
70 | - while (null !== $token = array_shift($this->parsed)) { |
|
71 | - if ($parseOptions && '' == $token) { |
|
72 | - $this->parseArgument($token); |
|
73 | - } elseif ($parseOptions && '--' == $token) { |
|
74 | - $parseOptions = false; |
|
75 | - } elseif ($parseOptions && str_starts_with($token, '--')) { |
|
76 | - $this->parseLongOption($token); |
|
77 | - } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { |
|
78 | - $this->parseShortOption($token); |
|
79 | - } else { |
|
80 | - $this->parseArgument($token); |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Parses a short option. |
|
87 | - */ |
|
88 | - private function parseShortOption(string $token) |
|
89 | - { |
|
90 | - $name = substr($token, 1); |
|
91 | - |
|
92 | - if (\strlen($name) > 1) { |
|
93 | - if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { |
|
94 | - // an option with a value (with no space) |
|
95 | - $this->addShortOption($name[0], substr($name, 1)); |
|
96 | - } else { |
|
97 | - $this->parseShortOptionSet($name); |
|
98 | - } |
|
99 | - } else { |
|
100 | - $this->addShortOption($name, null); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Parses a short option set. |
|
106 | - * |
|
107 | - * @throws RuntimeException When option given doesn't exist |
|
108 | - */ |
|
109 | - private function parseShortOptionSet(string $name) |
|
110 | - { |
|
111 | - $len = \strlen($name); |
|
112 | - for ($i = 0; $i < $len; ++$i) { |
|
113 | - if (!$this->definition->hasShortcut($name[$i])) { |
|
114 | - $encoding = mb_detect_encoding($name, null, true); |
|
115 | - throw new RuntimeException(sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding))); |
|
116 | - } |
|
117 | - |
|
118 | - $option = $this->definition->getOptionForShortcut($name[$i]); |
|
119 | - if ($option->acceptValue()) { |
|
120 | - $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); |
|
121 | - |
|
122 | - break; |
|
123 | - } else { |
|
124 | - $this->addLongOption($option->getName(), null); |
|
125 | - } |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Parses a long option. |
|
131 | - */ |
|
132 | - private function parseLongOption(string $token) |
|
133 | - { |
|
134 | - $name = substr($token, 2); |
|
135 | - |
|
136 | - if (false !== $pos = strpos($name, '=')) { |
|
137 | - if (0 === \strlen($value = substr($name, $pos + 1))) { |
|
138 | - array_unshift($this->parsed, $value); |
|
139 | - } |
|
140 | - $this->addLongOption(substr($name, 0, $pos), $value); |
|
141 | - } else { |
|
142 | - $this->addLongOption($name, null); |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Parses an argument. |
|
148 | - * |
|
149 | - * @throws RuntimeException When too many arguments are given |
|
150 | - */ |
|
151 | - private function parseArgument(string $token) |
|
152 | - { |
|
153 | - $c = \count($this->arguments); |
|
154 | - |
|
155 | - // if input is expecting another argument, add it |
|
156 | - if ($this->definition->hasArgument($c)) { |
|
157 | - $arg = $this->definition->getArgument($c); |
|
158 | - $this->arguments[$arg->getName()] = $arg->isArray() ? [$token] : $token; |
|
159 | - |
|
160 | - // if last argument isArray(), append token to last argument |
|
161 | - } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { |
|
162 | - $arg = $this->definition->getArgument($c - 1); |
|
163 | - $this->arguments[$arg->getName()][] = $token; |
|
164 | - |
|
165 | - // unexpected argument |
|
166 | - } else { |
|
167 | - $all = $this->definition->getArguments(); |
|
168 | - $symfonyCommandName = null; |
|
169 | - if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) { |
|
170 | - $symfonyCommandName = $this->arguments['command'] ?? null; |
|
171 | - unset($all[$key]); |
|
172 | - } |
|
173 | - |
|
174 | - if (\count($all)) { |
|
175 | - if ($symfonyCommandName) { |
|
176 | - $message = sprintf('Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode('" "', array_keys($all))); |
|
177 | - } else { |
|
178 | - $message = sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))); |
|
179 | - } |
|
180 | - } elseif ($symfonyCommandName) { |
|
181 | - $message = sprintf('No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token); |
|
182 | - } else { |
|
183 | - $message = sprintf('No arguments expected, got "%s".', $token); |
|
184 | - } |
|
185 | - |
|
186 | - throw new RuntimeException($message); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Adds a short option value. |
|
192 | - * |
|
193 | - * @throws RuntimeException When option given doesn't exist |
|
194 | - */ |
|
195 | - private function addShortOption(string $shortcut, $value) |
|
196 | - { |
|
197 | - if (!$this->definition->hasShortcut($shortcut)) { |
|
198 | - throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
199 | - } |
|
200 | - |
|
201 | - $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Adds a long option value. |
|
206 | - * |
|
207 | - * @throws RuntimeException When option given doesn't exist |
|
208 | - */ |
|
209 | - private function addLongOption(string $name, $value) |
|
210 | - { |
|
211 | - if (!$this->definition->hasOption($name)) { |
|
212 | - if (!$this->definition->hasNegation($name)) { |
|
213 | - throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); |
|
214 | - } |
|
215 | - |
|
216 | - $optionName = $this->definition->negationToName($name); |
|
217 | - if (null !== $value) { |
|
218 | - throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
219 | - } |
|
220 | - $this->options[$optionName] = false; |
|
221 | - |
|
222 | - return; |
|
223 | - } |
|
224 | - |
|
225 | - $option = $this->definition->getOption($name); |
|
226 | - |
|
227 | - if (null !== $value && !$option->acceptValue()) { |
|
228 | - throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
229 | - } |
|
230 | - |
|
231 | - if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) { |
|
232 | - // if option accepts an optional or mandatory argument |
|
233 | - // let's see if there is one provided |
|
234 | - $next = array_shift($this->parsed); |
|
235 | - if ((isset($next[0]) && '-' !== $next[0]) || \in_array($next, ['', null], true)) { |
|
236 | - $value = $next; |
|
237 | - } else { |
|
238 | - array_unshift($this->parsed, $next); |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - if (null === $value) { |
|
243 | - if ($option->isValueRequired()) { |
|
244 | - throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); |
|
245 | - } |
|
246 | - |
|
247 | - if (!$option->isArray() && !$option->isValueOptional()) { |
|
248 | - $value = true; |
|
249 | - } |
|
250 | - } |
|
251 | - |
|
252 | - if ($option->isArray()) { |
|
253 | - $this->options[$name][] = $value; |
|
254 | - } else { |
|
255 | - $this->options[$name] = $value; |
|
256 | - } |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * {@inheritdoc} |
|
261 | - */ |
|
262 | - public function getFirstArgument() |
|
263 | - { |
|
264 | - $isOption = false; |
|
265 | - foreach ($this->tokens as $i => $token) { |
|
266 | - if ($token && '-' === $token[0]) { |
|
267 | - if (str_contains($token, '=') || !isset($this->tokens[$i + 1])) { |
|
268 | - continue; |
|
269 | - } |
|
270 | - |
|
271 | - // If it's a long option, consider that everything after "--" is the option name. |
|
272 | - // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator) |
|
273 | - $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1); |
|
274 | - if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) { |
|
275 | - // noop |
|
276 | - } elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) { |
|
277 | - $isOption = true; |
|
278 | - } |
|
279 | - |
|
280 | - continue; |
|
281 | - } |
|
282 | - |
|
283 | - if ($isOption) { |
|
284 | - $isOption = false; |
|
285 | - continue; |
|
286 | - } |
|
287 | - |
|
288 | - return $token; |
|
289 | - } |
|
290 | - |
|
291 | - return null; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * {@inheritdoc} |
|
296 | - */ |
|
297 | - public function hasParameterOption($values, bool $onlyParams = false) |
|
298 | - { |
|
299 | - $values = (array) $values; |
|
300 | - |
|
301 | - foreach ($this->tokens as $token) { |
|
302 | - if ($onlyParams && '--' === $token) { |
|
303 | - return false; |
|
304 | - } |
|
305 | - foreach ($values as $value) { |
|
306 | - // Options with values: |
|
307 | - // For long options, test for '--option=' at beginning |
|
308 | - // For short options, test for '-o' at beginning |
|
309 | - $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
310 | - if ($token === $value || '' !== $leading && str_starts_with($token, $leading)) { |
|
311 | - return true; |
|
312 | - } |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - return false; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * {@inheritdoc} |
|
321 | - */ |
|
322 | - public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
323 | - { |
|
324 | - $values = (array) $values; |
|
325 | - $tokens = $this->tokens; |
|
326 | - |
|
327 | - while (0 < \count($tokens)) { |
|
328 | - $token = array_shift($tokens); |
|
329 | - if ($onlyParams && '--' === $token) { |
|
330 | - return $default; |
|
331 | - } |
|
332 | - |
|
333 | - foreach ($values as $value) { |
|
334 | - if ($token === $value) { |
|
335 | - return array_shift($tokens); |
|
336 | - } |
|
337 | - // Options with values: |
|
338 | - // For long options, test for '--option=' at beginning |
|
339 | - // For short options, test for '-o' at beginning |
|
340 | - $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
341 | - if ('' !== $leading && str_starts_with($token, $leading)) { |
|
342 | - return substr($token, \strlen($leading)); |
|
343 | - } |
|
344 | - } |
|
345 | - } |
|
346 | - |
|
347 | - return $default; |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Returns a stringified representation of the args passed to the command. |
|
352 | - * |
|
353 | - * @return string |
|
354 | - */ |
|
355 | - public function __toString() |
|
356 | - { |
|
357 | - $tokens = array_map(function ($token) { |
|
358 | - if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
|
359 | - return $match[1].$this->escapeToken($match[2]); |
|
360 | - } |
|
361 | - |
|
362 | - if ($token && '-' !== $token[0]) { |
|
363 | - return $this->escapeToken($token); |
|
364 | - } |
|
365 | - |
|
366 | - return $token; |
|
367 | - }, $this->tokens); |
|
368 | - |
|
369 | - return implode(' ', $tokens); |
|
370 | - } |
|
43 | + private $tokens; |
|
44 | + private $parsed; |
|
45 | + |
|
46 | + public function __construct(array $argv = null, InputDefinition $definition = null) |
|
47 | + { |
|
48 | + $argv = $argv ?? $_SERVER['argv'] ?? []; |
|
49 | + |
|
50 | + // strip the application name |
|
51 | + array_shift($argv); |
|
52 | + |
|
53 | + $this->tokens = $argv; |
|
54 | + |
|
55 | + parent::__construct($definition); |
|
56 | + } |
|
57 | + |
|
58 | + protected function setTokens(array $tokens) |
|
59 | + { |
|
60 | + $this->tokens = $tokens; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + protected function parse() |
|
67 | + { |
|
68 | + $parseOptions = true; |
|
69 | + $this->parsed = $this->tokens; |
|
70 | + while (null !== $token = array_shift($this->parsed)) { |
|
71 | + if ($parseOptions && '' == $token) { |
|
72 | + $this->parseArgument($token); |
|
73 | + } elseif ($parseOptions && '--' == $token) { |
|
74 | + $parseOptions = false; |
|
75 | + } elseif ($parseOptions && str_starts_with($token, '--')) { |
|
76 | + $this->parseLongOption($token); |
|
77 | + } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { |
|
78 | + $this->parseShortOption($token); |
|
79 | + } else { |
|
80 | + $this->parseArgument($token); |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Parses a short option. |
|
87 | + */ |
|
88 | + private function parseShortOption(string $token) |
|
89 | + { |
|
90 | + $name = substr($token, 1); |
|
91 | + |
|
92 | + if (\strlen($name) > 1) { |
|
93 | + if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { |
|
94 | + // an option with a value (with no space) |
|
95 | + $this->addShortOption($name[0], substr($name, 1)); |
|
96 | + } else { |
|
97 | + $this->parseShortOptionSet($name); |
|
98 | + } |
|
99 | + } else { |
|
100 | + $this->addShortOption($name, null); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Parses a short option set. |
|
106 | + * |
|
107 | + * @throws RuntimeException When option given doesn't exist |
|
108 | + */ |
|
109 | + private function parseShortOptionSet(string $name) |
|
110 | + { |
|
111 | + $len = \strlen($name); |
|
112 | + for ($i = 0; $i < $len; ++$i) { |
|
113 | + if (!$this->definition->hasShortcut($name[$i])) { |
|
114 | + $encoding = mb_detect_encoding($name, null, true); |
|
115 | + throw new RuntimeException(sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding))); |
|
116 | + } |
|
117 | + |
|
118 | + $option = $this->definition->getOptionForShortcut($name[$i]); |
|
119 | + if ($option->acceptValue()) { |
|
120 | + $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); |
|
121 | + |
|
122 | + break; |
|
123 | + } else { |
|
124 | + $this->addLongOption($option->getName(), null); |
|
125 | + } |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Parses a long option. |
|
131 | + */ |
|
132 | + private function parseLongOption(string $token) |
|
133 | + { |
|
134 | + $name = substr($token, 2); |
|
135 | + |
|
136 | + if (false !== $pos = strpos($name, '=')) { |
|
137 | + if (0 === \strlen($value = substr($name, $pos + 1))) { |
|
138 | + array_unshift($this->parsed, $value); |
|
139 | + } |
|
140 | + $this->addLongOption(substr($name, 0, $pos), $value); |
|
141 | + } else { |
|
142 | + $this->addLongOption($name, null); |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Parses an argument. |
|
148 | + * |
|
149 | + * @throws RuntimeException When too many arguments are given |
|
150 | + */ |
|
151 | + private function parseArgument(string $token) |
|
152 | + { |
|
153 | + $c = \count($this->arguments); |
|
154 | + |
|
155 | + // if input is expecting another argument, add it |
|
156 | + if ($this->definition->hasArgument($c)) { |
|
157 | + $arg = $this->definition->getArgument($c); |
|
158 | + $this->arguments[$arg->getName()] = $arg->isArray() ? [$token] : $token; |
|
159 | + |
|
160 | + // if last argument isArray(), append token to last argument |
|
161 | + } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { |
|
162 | + $arg = $this->definition->getArgument($c - 1); |
|
163 | + $this->arguments[$arg->getName()][] = $token; |
|
164 | + |
|
165 | + // unexpected argument |
|
166 | + } else { |
|
167 | + $all = $this->definition->getArguments(); |
|
168 | + $symfonyCommandName = null; |
|
169 | + if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) { |
|
170 | + $symfonyCommandName = $this->arguments['command'] ?? null; |
|
171 | + unset($all[$key]); |
|
172 | + } |
|
173 | + |
|
174 | + if (\count($all)) { |
|
175 | + if ($symfonyCommandName) { |
|
176 | + $message = sprintf('Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode('" "', array_keys($all))); |
|
177 | + } else { |
|
178 | + $message = sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))); |
|
179 | + } |
|
180 | + } elseif ($symfonyCommandName) { |
|
181 | + $message = sprintf('No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token); |
|
182 | + } else { |
|
183 | + $message = sprintf('No arguments expected, got "%s".', $token); |
|
184 | + } |
|
185 | + |
|
186 | + throw new RuntimeException($message); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Adds a short option value. |
|
192 | + * |
|
193 | + * @throws RuntimeException When option given doesn't exist |
|
194 | + */ |
|
195 | + private function addShortOption(string $shortcut, $value) |
|
196 | + { |
|
197 | + if (!$this->definition->hasShortcut($shortcut)) { |
|
198 | + throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
199 | + } |
|
200 | + |
|
201 | + $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Adds a long option value. |
|
206 | + * |
|
207 | + * @throws RuntimeException When option given doesn't exist |
|
208 | + */ |
|
209 | + private function addLongOption(string $name, $value) |
|
210 | + { |
|
211 | + if (!$this->definition->hasOption($name)) { |
|
212 | + if (!$this->definition->hasNegation($name)) { |
|
213 | + throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); |
|
214 | + } |
|
215 | + |
|
216 | + $optionName = $this->definition->negationToName($name); |
|
217 | + if (null !== $value) { |
|
218 | + throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
219 | + } |
|
220 | + $this->options[$optionName] = false; |
|
221 | + |
|
222 | + return; |
|
223 | + } |
|
224 | + |
|
225 | + $option = $this->definition->getOption($name); |
|
226 | + |
|
227 | + if (null !== $value && !$option->acceptValue()) { |
|
228 | + throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
|
229 | + } |
|
230 | + |
|
231 | + if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) { |
|
232 | + // if option accepts an optional or mandatory argument |
|
233 | + // let's see if there is one provided |
|
234 | + $next = array_shift($this->parsed); |
|
235 | + if ((isset($next[0]) && '-' !== $next[0]) || \in_array($next, ['', null], true)) { |
|
236 | + $value = $next; |
|
237 | + } else { |
|
238 | + array_unshift($this->parsed, $next); |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + if (null === $value) { |
|
243 | + if ($option->isValueRequired()) { |
|
244 | + throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); |
|
245 | + } |
|
246 | + |
|
247 | + if (!$option->isArray() && !$option->isValueOptional()) { |
|
248 | + $value = true; |
|
249 | + } |
|
250 | + } |
|
251 | + |
|
252 | + if ($option->isArray()) { |
|
253 | + $this->options[$name][] = $value; |
|
254 | + } else { |
|
255 | + $this->options[$name] = $value; |
|
256 | + } |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * {@inheritdoc} |
|
261 | + */ |
|
262 | + public function getFirstArgument() |
|
263 | + { |
|
264 | + $isOption = false; |
|
265 | + foreach ($this->tokens as $i => $token) { |
|
266 | + if ($token && '-' === $token[0]) { |
|
267 | + if (str_contains($token, '=') || !isset($this->tokens[$i + 1])) { |
|
268 | + continue; |
|
269 | + } |
|
270 | + |
|
271 | + // If it's a long option, consider that everything after "--" is the option name. |
|
272 | + // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator) |
|
273 | + $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1); |
|
274 | + if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) { |
|
275 | + // noop |
|
276 | + } elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) { |
|
277 | + $isOption = true; |
|
278 | + } |
|
279 | + |
|
280 | + continue; |
|
281 | + } |
|
282 | + |
|
283 | + if ($isOption) { |
|
284 | + $isOption = false; |
|
285 | + continue; |
|
286 | + } |
|
287 | + |
|
288 | + return $token; |
|
289 | + } |
|
290 | + |
|
291 | + return null; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * {@inheritdoc} |
|
296 | + */ |
|
297 | + public function hasParameterOption($values, bool $onlyParams = false) |
|
298 | + { |
|
299 | + $values = (array) $values; |
|
300 | + |
|
301 | + foreach ($this->tokens as $token) { |
|
302 | + if ($onlyParams && '--' === $token) { |
|
303 | + return false; |
|
304 | + } |
|
305 | + foreach ($values as $value) { |
|
306 | + // Options with values: |
|
307 | + // For long options, test for '--option=' at beginning |
|
308 | + // For short options, test for '-o' at beginning |
|
309 | + $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
310 | + if ($token === $value || '' !== $leading && str_starts_with($token, $leading)) { |
|
311 | + return true; |
|
312 | + } |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + return false; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * {@inheritdoc} |
|
321 | + */ |
|
322 | + public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
323 | + { |
|
324 | + $values = (array) $values; |
|
325 | + $tokens = $this->tokens; |
|
326 | + |
|
327 | + while (0 < \count($tokens)) { |
|
328 | + $token = array_shift($tokens); |
|
329 | + if ($onlyParams && '--' === $token) { |
|
330 | + return $default; |
|
331 | + } |
|
332 | + |
|
333 | + foreach ($values as $value) { |
|
334 | + if ($token === $value) { |
|
335 | + return array_shift($tokens); |
|
336 | + } |
|
337 | + // Options with values: |
|
338 | + // For long options, test for '--option=' at beginning |
|
339 | + // For short options, test for '-o' at beginning |
|
340 | + $leading = str_starts_with($value, '--') ? $value.'=' : $value; |
|
341 | + if ('' !== $leading && str_starts_with($token, $leading)) { |
|
342 | + return substr($token, \strlen($leading)); |
|
343 | + } |
|
344 | + } |
|
345 | + } |
|
346 | + |
|
347 | + return $default; |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Returns a stringified representation of the args passed to the command. |
|
352 | + * |
|
353 | + * @return string |
|
354 | + */ |
|
355 | + public function __toString() |
|
356 | + { |
|
357 | + $tokens = array_map(function ($token) { |
|
358 | + if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
|
359 | + return $match[1].$this->escapeToken($match[2]); |
|
360 | + } |
|
361 | + |
|
362 | + if ($token && '-' !== $token[0]) { |
|
363 | + return $this->escapeToken($token); |
|
364 | + } |
|
365 | + |
|
366 | + return $token; |
|
367 | + }, $this->tokens); |
|
368 | + |
|
369 | + return implode(' ', $tokens); |
|
370 | + } |
|
371 | 371 | } |
@@ -19,19 +19,19 @@ |
||
19 | 19 | */ |
20 | 20 | interface StreamableInputInterface extends InputInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * Sets the input stream to read from when interacting with the user. |
|
24 | - * |
|
25 | - * This is mainly useful for testing purpose. |
|
26 | - * |
|
27 | - * @param resource $stream The input stream |
|
28 | - */ |
|
29 | - public function setStream($stream); |
|
22 | + /** |
|
23 | + * Sets the input stream to read from when interacting with the user. |
|
24 | + * |
|
25 | + * This is mainly useful for testing purpose. |
|
26 | + * |
|
27 | + * @param resource $stream The input stream |
|
28 | + */ |
|
29 | + public function setStream($stream); |
|
30 | 30 | |
31 | - /** |
|
32 | - * Returns the input stream. |
|
33 | - * |
|
34 | - * @return resource|null |
|
35 | - */ |
|
36 | - public function getStream(); |
|
31 | + /** |
|
32 | + * Returns the input stream. |
|
33 | + * |
|
34 | + * @return resource|null |
|
35 | + */ |
|
36 | + public function getStream(); |
|
37 | 37 | } |
@@ -21,131 +21,131 @@ |
||
21 | 21 | */ |
22 | 22 | interface InputInterface |
23 | 23 | { |
24 | - /** |
|
25 | - * Returns the first argument from the raw parameters (not parsed). |
|
26 | - * |
|
27 | - * @return string|null The value of the first argument or null otherwise |
|
28 | - */ |
|
29 | - public function getFirstArgument(); |
|
30 | - |
|
31 | - /** |
|
32 | - * Returns true if the raw parameters (not parsed) contain a value. |
|
33 | - * |
|
34 | - * This method is to be used to introspect the input parameters |
|
35 | - * before they have been validated. It must be used carefully. |
|
36 | - * Does not necessarily return the correct result for short options |
|
37 | - * when multiple flags are combined in the same option. |
|
38 | - * |
|
39 | - * @param string|array $values The values to look for in the raw parameters (can be an array) |
|
40 | - * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
41 | - * |
|
42 | - * @return bool true if the value is contained in the raw parameters |
|
43 | - */ |
|
44 | - public function hasParameterOption($values, bool $onlyParams = false); |
|
45 | - |
|
46 | - /** |
|
47 | - * Returns the value of a raw option (not parsed). |
|
48 | - * |
|
49 | - * This method is to be used to introspect the input parameters |
|
50 | - * before they have been validated. It must be used carefully. |
|
51 | - * Does not necessarily return the correct result for short options |
|
52 | - * when multiple flags are combined in the same option. |
|
53 | - * |
|
54 | - * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
|
55 | - * @param string|bool|int|float|array|null $default The default value to return if no result is found |
|
56 | - * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
57 | - * |
|
58 | - * @return mixed The option value |
|
59 | - */ |
|
60 | - public function getParameterOption($values, $default = false, bool $onlyParams = false); |
|
61 | - |
|
62 | - /** |
|
63 | - * Binds the current Input instance with the given arguments and options. |
|
64 | - * |
|
65 | - * @throws RuntimeException |
|
66 | - */ |
|
67 | - public function bind(InputDefinition $definition); |
|
68 | - |
|
69 | - /** |
|
70 | - * Validates the input. |
|
71 | - * |
|
72 | - * @throws RuntimeException When not enough arguments are given |
|
73 | - */ |
|
74 | - public function validate(); |
|
75 | - |
|
76 | - /** |
|
77 | - * Returns all the given arguments merged with the default values. |
|
78 | - * |
|
79 | - * @return array<string|bool|int|float|array|null> |
|
80 | - */ |
|
81 | - public function getArguments(); |
|
82 | - |
|
83 | - /** |
|
84 | - * Returns the argument value for a given argument name. |
|
85 | - * |
|
86 | - * @return mixed |
|
87 | - * |
|
88 | - * @throws InvalidArgumentException When argument given doesn't exist |
|
89 | - */ |
|
90 | - public function getArgument(string $name); |
|
91 | - |
|
92 | - /** |
|
93 | - * Sets an argument value by name. |
|
94 | - * |
|
95 | - * @param mixed $value The argument value |
|
96 | - * |
|
97 | - * @throws InvalidArgumentException When argument given doesn't exist |
|
98 | - */ |
|
99 | - public function setArgument(string $name, $value); |
|
100 | - |
|
101 | - /** |
|
102 | - * Returns true if an InputArgument object exists by name or position. |
|
103 | - * |
|
104 | - * @return bool true if the InputArgument object exists, false otherwise |
|
105 | - */ |
|
106 | - public function hasArgument(string $name); |
|
107 | - |
|
108 | - /** |
|
109 | - * Returns all the given options merged with the default values. |
|
110 | - * |
|
111 | - * @return array<string|bool|int|float|array|null> |
|
112 | - */ |
|
113 | - public function getOptions(); |
|
114 | - |
|
115 | - /** |
|
116 | - * Returns the option value for a given option name. |
|
117 | - * |
|
118 | - * @return mixed |
|
119 | - * |
|
120 | - * @throws InvalidArgumentException When option given doesn't exist |
|
121 | - */ |
|
122 | - public function getOption(string $name); |
|
123 | - |
|
124 | - /** |
|
125 | - * Sets an option value by name. |
|
126 | - * |
|
127 | - * @param mixed $value The option value |
|
128 | - * |
|
129 | - * @throws InvalidArgumentException When option given doesn't exist |
|
130 | - */ |
|
131 | - public function setOption(string $name, $value); |
|
132 | - |
|
133 | - /** |
|
134 | - * Returns true if an InputOption object exists by name. |
|
135 | - * |
|
136 | - * @return bool true if the InputOption object exists, false otherwise |
|
137 | - */ |
|
138 | - public function hasOption(string $name); |
|
139 | - |
|
140 | - /** |
|
141 | - * Is this input means interactive? |
|
142 | - * |
|
143 | - * @return bool |
|
144 | - */ |
|
145 | - public function isInteractive(); |
|
146 | - |
|
147 | - /** |
|
148 | - * Sets the input interactivity. |
|
149 | - */ |
|
150 | - public function setInteractive(bool $interactive); |
|
24 | + /** |
|
25 | + * Returns the first argument from the raw parameters (not parsed). |
|
26 | + * |
|
27 | + * @return string|null The value of the first argument or null otherwise |
|
28 | + */ |
|
29 | + public function getFirstArgument(); |
|
30 | + |
|
31 | + /** |
|
32 | + * Returns true if the raw parameters (not parsed) contain a value. |
|
33 | + * |
|
34 | + * This method is to be used to introspect the input parameters |
|
35 | + * before they have been validated. It must be used carefully. |
|
36 | + * Does not necessarily return the correct result for short options |
|
37 | + * when multiple flags are combined in the same option. |
|
38 | + * |
|
39 | + * @param string|array $values The values to look for in the raw parameters (can be an array) |
|
40 | + * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
41 | + * |
|
42 | + * @return bool true if the value is contained in the raw parameters |
|
43 | + */ |
|
44 | + public function hasParameterOption($values, bool $onlyParams = false); |
|
45 | + |
|
46 | + /** |
|
47 | + * Returns the value of a raw option (not parsed). |
|
48 | + * |
|
49 | + * This method is to be used to introspect the input parameters |
|
50 | + * before they have been validated. It must be used carefully. |
|
51 | + * Does not necessarily return the correct result for short options |
|
52 | + * when multiple flags are combined in the same option. |
|
53 | + * |
|
54 | + * @param string|array $values The value(s) to look for in the raw parameters (can be an array) |
|
55 | + * @param string|bool|int|float|array|null $default The default value to return if no result is found |
|
56 | + * @param bool $onlyParams Only check real parameters, skip those following an end of options (--) signal |
|
57 | + * |
|
58 | + * @return mixed The option value |
|
59 | + */ |
|
60 | + public function getParameterOption($values, $default = false, bool $onlyParams = false); |
|
61 | + |
|
62 | + /** |
|
63 | + * Binds the current Input instance with the given arguments and options. |
|
64 | + * |
|
65 | + * @throws RuntimeException |
|
66 | + */ |
|
67 | + public function bind(InputDefinition $definition); |
|
68 | + |
|
69 | + /** |
|
70 | + * Validates the input. |
|
71 | + * |
|
72 | + * @throws RuntimeException When not enough arguments are given |
|
73 | + */ |
|
74 | + public function validate(); |
|
75 | + |
|
76 | + /** |
|
77 | + * Returns all the given arguments merged with the default values. |
|
78 | + * |
|
79 | + * @return array<string|bool|int|float|array|null> |
|
80 | + */ |
|
81 | + public function getArguments(); |
|
82 | + |
|
83 | + /** |
|
84 | + * Returns the argument value for a given argument name. |
|
85 | + * |
|
86 | + * @return mixed |
|
87 | + * |
|
88 | + * @throws InvalidArgumentException When argument given doesn't exist |
|
89 | + */ |
|
90 | + public function getArgument(string $name); |
|
91 | + |
|
92 | + /** |
|
93 | + * Sets an argument value by name. |
|
94 | + * |
|
95 | + * @param mixed $value The argument value |
|
96 | + * |
|
97 | + * @throws InvalidArgumentException When argument given doesn't exist |
|
98 | + */ |
|
99 | + public function setArgument(string $name, $value); |
|
100 | + |
|
101 | + /** |
|
102 | + * Returns true if an InputArgument object exists by name or position. |
|
103 | + * |
|
104 | + * @return bool true if the InputArgument object exists, false otherwise |
|
105 | + */ |
|
106 | + public function hasArgument(string $name); |
|
107 | + |
|
108 | + /** |
|
109 | + * Returns all the given options merged with the default values. |
|
110 | + * |
|
111 | + * @return array<string|bool|int|float|array|null> |
|
112 | + */ |
|
113 | + public function getOptions(); |
|
114 | + |
|
115 | + /** |
|
116 | + * Returns the option value for a given option name. |
|
117 | + * |
|
118 | + * @return mixed |
|
119 | + * |
|
120 | + * @throws InvalidArgumentException When option given doesn't exist |
|
121 | + */ |
|
122 | + public function getOption(string $name); |
|
123 | + |
|
124 | + /** |
|
125 | + * Sets an option value by name. |
|
126 | + * |
|
127 | + * @param mixed $value The option value |
|
128 | + * |
|
129 | + * @throws InvalidArgumentException When option given doesn't exist |
|
130 | + */ |
|
131 | + public function setOption(string $name, $value); |
|
132 | + |
|
133 | + /** |
|
134 | + * Returns true if an InputOption object exists by name. |
|
135 | + * |
|
136 | + * @return bool true if the InputOption object exists, false otherwise |
|
137 | + */ |
|
138 | + public function hasOption(string $name); |
|
139 | + |
|
140 | + /** |
|
141 | + * Is this input means interactive? |
|
142 | + * |
|
143 | + * @return bool |
|
144 | + */ |
|
145 | + public function isInteractive(); |
|
146 | + |
|
147 | + /** |
|
148 | + * Sets the input interactivity. |
|
149 | + */ |
|
150 | + public function setInteractive(bool $interactive); |
|
151 | 151 | } |
@@ -28,397 +28,397 @@ |
||
28 | 28 | */ |
29 | 29 | class InputDefinition |
30 | 30 | { |
31 | - private $arguments; |
|
32 | - private $requiredCount; |
|
33 | - private $lastArrayArgument; |
|
34 | - private $lastOptionalArgument; |
|
35 | - private $options; |
|
36 | - private $negations; |
|
37 | - private $shortcuts; |
|
38 | - |
|
39 | - /** |
|
40 | - * @param array $definition An array of InputArgument and InputOption instance |
|
41 | - */ |
|
42 | - public function __construct(array $definition = []) |
|
43 | - { |
|
44 | - $this->setDefinition($definition); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Sets the definition of the input. |
|
49 | - */ |
|
50 | - public function setDefinition(array $definition) |
|
51 | - { |
|
52 | - $arguments = []; |
|
53 | - $options = []; |
|
54 | - foreach ($definition as $item) { |
|
55 | - if ($item instanceof InputOption) { |
|
56 | - $options[] = $item; |
|
57 | - } else { |
|
58 | - $arguments[] = $item; |
|
59 | - } |
|
60 | - } |
|
61 | - |
|
62 | - $this->setArguments($arguments); |
|
63 | - $this->setOptions($options); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Sets the InputArgument objects. |
|
68 | - * |
|
69 | - * @param InputArgument[] $arguments An array of InputArgument objects |
|
70 | - */ |
|
71 | - public function setArguments(array $arguments = []) |
|
72 | - { |
|
73 | - $this->arguments = []; |
|
74 | - $this->requiredCount = 0; |
|
75 | - $this->lastOptionalArgument = null; |
|
76 | - $this->lastArrayArgument = null; |
|
77 | - $this->addArguments($arguments); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Adds an array of InputArgument objects. |
|
82 | - * |
|
83 | - * @param InputArgument[] $arguments An array of InputArgument objects |
|
84 | - */ |
|
85 | - public function addArguments(?array $arguments = []) |
|
86 | - { |
|
87 | - if (null !== $arguments) { |
|
88 | - foreach ($arguments as $argument) { |
|
89 | - $this->addArgument($argument); |
|
90 | - } |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @throws LogicException When incorrect argument is given |
|
96 | - */ |
|
97 | - public function addArgument(InputArgument $argument) |
|
98 | - { |
|
99 | - if (isset($this->arguments[$argument->getName()])) { |
|
100 | - throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName())); |
|
101 | - } |
|
102 | - |
|
103 | - if (null !== $this->lastArrayArgument) { |
|
104 | - throw new LogicException(sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName(), $this->lastArrayArgument->getName())); |
|
105 | - } |
|
106 | - |
|
107 | - if ($argument->isRequired() && null !== $this->lastOptionalArgument) { |
|
108 | - throw new LogicException(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName(), $this->lastOptionalArgument->getName())); |
|
109 | - } |
|
110 | - |
|
111 | - if ($argument->isArray()) { |
|
112 | - $this->lastArrayArgument = $argument; |
|
113 | - } |
|
114 | - |
|
115 | - if ($argument->isRequired()) { |
|
116 | - ++$this->requiredCount; |
|
117 | - } else { |
|
118 | - $this->lastOptionalArgument = $argument; |
|
119 | - } |
|
120 | - |
|
121 | - $this->arguments[$argument->getName()] = $argument; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Returns an InputArgument by name or by position. |
|
126 | - * |
|
127 | - * @param string|int $name The InputArgument name or position |
|
128 | - * |
|
129 | - * @return InputArgument An InputArgument object |
|
130 | - * |
|
131 | - * @throws InvalidArgumentException When argument given doesn't exist |
|
132 | - */ |
|
133 | - public function getArgument($name) |
|
134 | - { |
|
135 | - if (!$this->hasArgument($name)) { |
|
136 | - throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
137 | - } |
|
138 | - |
|
139 | - $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments; |
|
140 | - |
|
141 | - return $arguments[$name]; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Returns true if an InputArgument object exists by name or position. |
|
146 | - * |
|
147 | - * @param string|int $name The InputArgument name or position |
|
148 | - * |
|
149 | - * @return bool true if the InputArgument object exists, false otherwise |
|
150 | - */ |
|
151 | - public function hasArgument($name) |
|
152 | - { |
|
153 | - $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments; |
|
154 | - |
|
155 | - return isset($arguments[$name]); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Gets the array of InputArgument objects. |
|
160 | - * |
|
161 | - * @return InputArgument[] An array of InputArgument objects |
|
162 | - */ |
|
163 | - public function getArguments() |
|
164 | - { |
|
165 | - return $this->arguments; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Returns the number of InputArguments. |
|
170 | - * |
|
171 | - * @return int The number of InputArguments |
|
172 | - */ |
|
173 | - public function getArgumentCount() |
|
174 | - { |
|
175 | - return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Returns the number of required InputArguments. |
|
180 | - * |
|
181 | - * @return int The number of required InputArguments |
|
182 | - */ |
|
183 | - public function getArgumentRequiredCount() |
|
184 | - { |
|
185 | - return $this->requiredCount; |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * @return array<string|bool|int|float|array|null> |
|
190 | - */ |
|
191 | - public function getArgumentDefaults() |
|
192 | - { |
|
193 | - $values = []; |
|
194 | - foreach ($this->arguments as $argument) { |
|
195 | - $values[$argument->getName()] = $argument->getDefault(); |
|
196 | - } |
|
197 | - |
|
198 | - return $values; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Sets the InputOption objects. |
|
203 | - * |
|
204 | - * @param InputOption[] $options An array of InputOption objects |
|
205 | - */ |
|
206 | - public function setOptions(array $options = []) |
|
207 | - { |
|
208 | - $this->options = []; |
|
209 | - $this->shortcuts = []; |
|
210 | - $this->negations = []; |
|
211 | - $this->addOptions($options); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Adds an array of InputOption objects. |
|
216 | - * |
|
217 | - * @param InputOption[] $options An array of InputOption objects |
|
218 | - */ |
|
219 | - public function addOptions(array $options = []) |
|
220 | - { |
|
221 | - foreach ($options as $option) { |
|
222 | - $this->addOption($option); |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @throws LogicException When option given already exist |
|
228 | - */ |
|
229 | - public function addOption(InputOption $option) |
|
230 | - { |
|
231 | - if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) { |
|
232 | - throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName())); |
|
233 | - } |
|
234 | - if (isset($this->negations[$option->getName()])) { |
|
235 | - throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName())); |
|
236 | - } |
|
237 | - |
|
238 | - if ($option->getShortcut()) { |
|
239 | - foreach (explode('|', $option->getShortcut()) as $shortcut) { |
|
240 | - if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) { |
|
241 | - throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut)); |
|
242 | - } |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - $this->options[$option->getName()] = $option; |
|
247 | - if ($option->getShortcut()) { |
|
248 | - foreach (explode('|', $option->getShortcut()) as $shortcut) { |
|
249 | - $this->shortcuts[$shortcut] = $option->getName(); |
|
250 | - } |
|
251 | - } |
|
252 | - |
|
253 | - if ($option->isNegatable()) { |
|
254 | - $negatedName = 'no-'.$option->getName(); |
|
255 | - if (isset($this->options[$negatedName])) { |
|
256 | - throw new LogicException(sprintf('An option named "%s" already exists.', $negatedName)); |
|
257 | - } |
|
258 | - $this->negations[$negatedName] = $option->getName(); |
|
259 | - } |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Returns an InputOption by name. |
|
264 | - * |
|
265 | - * @return InputOption A InputOption object |
|
266 | - * |
|
267 | - * @throws InvalidArgumentException When option given doesn't exist |
|
268 | - */ |
|
269 | - public function getOption(string $name) |
|
270 | - { |
|
271 | - if (!$this->hasOption($name)) { |
|
272 | - throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name)); |
|
273 | - } |
|
274 | - |
|
275 | - return $this->options[$name]; |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Returns true if an InputOption object exists by name. |
|
280 | - * |
|
281 | - * This method can't be used to check if the user included the option when |
|
282 | - * executing the command (use getOption() instead). |
|
283 | - * |
|
284 | - * @return bool true if the InputOption object exists, false otherwise |
|
285 | - */ |
|
286 | - public function hasOption(string $name) |
|
287 | - { |
|
288 | - return isset($this->options[$name]); |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Gets the array of InputOption objects. |
|
293 | - * |
|
294 | - * @return InputOption[] An array of InputOption objects |
|
295 | - */ |
|
296 | - public function getOptions() |
|
297 | - { |
|
298 | - return $this->options; |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Returns true if an InputOption object exists by shortcut. |
|
303 | - * |
|
304 | - * @return bool true if the InputOption object exists, false otherwise |
|
305 | - */ |
|
306 | - public function hasShortcut(string $name) |
|
307 | - { |
|
308 | - return isset($this->shortcuts[$name]); |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Returns true if an InputOption object exists by negated name. |
|
313 | - */ |
|
314 | - public function hasNegation(string $name): bool |
|
315 | - { |
|
316 | - return isset($this->negations[$name]); |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Gets an InputOption by shortcut. |
|
321 | - * |
|
322 | - * @return InputOption An InputOption object |
|
323 | - */ |
|
324 | - public function getOptionForShortcut(string $shortcut) |
|
325 | - { |
|
326 | - return $this->getOption($this->shortcutToName($shortcut)); |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * @return array<string|bool|int|float|array|null> |
|
331 | - */ |
|
332 | - public function getOptionDefaults() |
|
333 | - { |
|
334 | - $values = []; |
|
335 | - foreach ($this->options as $option) { |
|
336 | - $values[$option->getName()] = $option->getDefault(); |
|
337 | - } |
|
338 | - |
|
339 | - return $values; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Returns the InputOption name given a shortcut. |
|
344 | - * |
|
345 | - * @throws InvalidArgumentException When option given does not exist |
|
346 | - * |
|
347 | - * @internal |
|
348 | - */ |
|
349 | - public function shortcutToName(string $shortcut): string |
|
350 | - { |
|
351 | - if (!isset($this->shortcuts[$shortcut])) { |
|
352 | - throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
353 | - } |
|
354 | - |
|
355 | - return $this->shortcuts[$shortcut]; |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Returns the InputOption name given a negation. |
|
360 | - * |
|
361 | - * @throws InvalidArgumentException When option given does not exist |
|
362 | - * |
|
363 | - * @internal |
|
364 | - */ |
|
365 | - public function negationToName(string $negation): string |
|
366 | - { |
|
367 | - if (!isset($this->negations[$negation])) { |
|
368 | - throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $negation)); |
|
369 | - } |
|
370 | - |
|
371 | - return $this->negations[$negation]; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Gets the synopsis. |
|
376 | - * |
|
377 | - * @return string The synopsis |
|
378 | - */ |
|
379 | - public function getSynopsis(bool $short = false) |
|
380 | - { |
|
381 | - $elements = []; |
|
382 | - |
|
383 | - if ($short && $this->getOptions()) { |
|
384 | - $elements[] = '[options]'; |
|
385 | - } elseif (!$short) { |
|
386 | - foreach ($this->getOptions() as $option) { |
|
387 | - $value = ''; |
|
388 | - if ($option->acceptValue()) { |
|
389 | - $value = sprintf( |
|
390 | - ' %s%s%s', |
|
391 | - $option->isValueOptional() ? '[' : '', |
|
392 | - strtoupper($option->getName()), |
|
393 | - $option->isValueOptional() ? ']' : '' |
|
394 | - ); |
|
395 | - } |
|
396 | - |
|
397 | - $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : ''; |
|
398 | - $negation = $option->isNegatable() ? sprintf('|--no-%s', $option->getName()) : ''; |
|
399 | - $elements[] = sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation); |
|
400 | - } |
|
401 | - } |
|
402 | - |
|
403 | - if (\count($elements) && $this->getArguments()) { |
|
404 | - $elements[] = '[--]'; |
|
405 | - } |
|
406 | - |
|
407 | - $tail = ''; |
|
408 | - foreach ($this->getArguments() as $argument) { |
|
409 | - $element = '<'.$argument->getName().'>'; |
|
410 | - if ($argument->isArray()) { |
|
411 | - $element .= '...'; |
|
412 | - } |
|
413 | - |
|
414 | - if (!$argument->isRequired()) { |
|
415 | - $element = '['.$element; |
|
416 | - $tail .= ']'; |
|
417 | - } |
|
418 | - |
|
419 | - $elements[] = $element; |
|
420 | - } |
|
421 | - |
|
422 | - return implode(' ', $elements).$tail; |
|
423 | - } |
|
31 | + private $arguments; |
|
32 | + private $requiredCount; |
|
33 | + private $lastArrayArgument; |
|
34 | + private $lastOptionalArgument; |
|
35 | + private $options; |
|
36 | + private $negations; |
|
37 | + private $shortcuts; |
|
38 | + |
|
39 | + /** |
|
40 | + * @param array $definition An array of InputArgument and InputOption instance |
|
41 | + */ |
|
42 | + public function __construct(array $definition = []) |
|
43 | + { |
|
44 | + $this->setDefinition($definition); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Sets the definition of the input. |
|
49 | + */ |
|
50 | + public function setDefinition(array $definition) |
|
51 | + { |
|
52 | + $arguments = []; |
|
53 | + $options = []; |
|
54 | + foreach ($definition as $item) { |
|
55 | + if ($item instanceof InputOption) { |
|
56 | + $options[] = $item; |
|
57 | + } else { |
|
58 | + $arguments[] = $item; |
|
59 | + } |
|
60 | + } |
|
61 | + |
|
62 | + $this->setArguments($arguments); |
|
63 | + $this->setOptions($options); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Sets the InputArgument objects. |
|
68 | + * |
|
69 | + * @param InputArgument[] $arguments An array of InputArgument objects |
|
70 | + */ |
|
71 | + public function setArguments(array $arguments = []) |
|
72 | + { |
|
73 | + $this->arguments = []; |
|
74 | + $this->requiredCount = 0; |
|
75 | + $this->lastOptionalArgument = null; |
|
76 | + $this->lastArrayArgument = null; |
|
77 | + $this->addArguments($arguments); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Adds an array of InputArgument objects. |
|
82 | + * |
|
83 | + * @param InputArgument[] $arguments An array of InputArgument objects |
|
84 | + */ |
|
85 | + public function addArguments(?array $arguments = []) |
|
86 | + { |
|
87 | + if (null !== $arguments) { |
|
88 | + foreach ($arguments as $argument) { |
|
89 | + $this->addArgument($argument); |
|
90 | + } |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @throws LogicException When incorrect argument is given |
|
96 | + */ |
|
97 | + public function addArgument(InputArgument $argument) |
|
98 | + { |
|
99 | + if (isset($this->arguments[$argument->getName()])) { |
|
100 | + throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName())); |
|
101 | + } |
|
102 | + |
|
103 | + if (null !== $this->lastArrayArgument) { |
|
104 | + throw new LogicException(sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName(), $this->lastArrayArgument->getName())); |
|
105 | + } |
|
106 | + |
|
107 | + if ($argument->isRequired() && null !== $this->lastOptionalArgument) { |
|
108 | + throw new LogicException(sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName(), $this->lastOptionalArgument->getName())); |
|
109 | + } |
|
110 | + |
|
111 | + if ($argument->isArray()) { |
|
112 | + $this->lastArrayArgument = $argument; |
|
113 | + } |
|
114 | + |
|
115 | + if ($argument->isRequired()) { |
|
116 | + ++$this->requiredCount; |
|
117 | + } else { |
|
118 | + $this->lastOptionalArgument = $argument; |
|
119 | + } |
|
120 | + |
|
121 | + $this->arguments[$argument->getName()] = $argument; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Returns an InputArgument by name or by position. |
|
126 | + * |
|
127 | + * @param string|int $name The InputArgument name or position |
|
128 | + * |
|
129 | + * @return InputArgument An InputArgument object |
|
130 | + * |
|
131 | + * @throws InvalidArgumentException When argument given doesn't exist |
|
132 | + */ |
|
133 | + public function getArgument($name) |
|
134 | + { |
|
135 | + if (!$this->hasArgument($name)) { |
|
136 | + throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
137 | + } |
|
138 | + |
|
139 | + $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments; |
|
140 | + |
|
141 | + return $arguments[$name]; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Returns true if an InputArgument object exists by name or position. |
|
146 | + * |
|
147 | + * @param string|int $name The InputArgument name or position |
|
148 | + * |
|
149 | + * @return bool true if the InputArgument object exists, false otherwise |
|
150 | + */ |
|
151 | + public function hasArgument($name) |
|
152 | + { |
|
153 | + $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments; |
|
154 | + |
|
155 | + return isset($arguments[$name]); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Gets the array of InputArgument objects. |
|
160 | + * |
|
161 | + * @return InputArgument[] An array of InputArgument objects |
|
162 | + */ |
|
163 | + public function getArguments() |
|
164 | + { |
|
165 | + return $this->arguments; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Returns the number of InputArguments. |
|
170 | + * |
|
171 | + * @return int The number of InputArguments |
|
172 | + */ |
|
173 | + public function getArgumentCount() |
|
174 | + { |
|
175 | + return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Returns the number of required InputArguments. |
|
180 | + * |
|
181 | + * @return int The number of required InputArguments |
|
182 | + */ |
|
183 | + public function getArgumentRequiredCount() |
|
184 | + { |
|
185 | + return $this->requiredCount; |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * @return array<string|bool|int|float|array|null> |
|
190 | + */ |
|
191 | + public function getArgumentDefaults() |
|
192 | + { |
|
193 | + $values = []; |
|
194 | + foreach ($this->arguments as $argument) { |
|
195 | + $values[$argument->getName()] = $argument->getDefault(); |
|
196 | + } |
|
197 | + |
|
198 | + return $values; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Sets the InputOption objects. |
|
203 | + * |
|
204 | + * @param InputOption[] $options An array of InputOption objects |
|
205 | + */ |
|
206 | + public function setOptions(array $options = []) |
|
207 | + { |
|
208 | + $this->options = []; |
|
209 | + $this->shortcuts = []; |
|
210 | + $this->negations = []; |
|
211 | + $this->addOptions($options); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Adds an array of InputOption objects. |
|
216 | + * |
|
217 | + * @param InputOption[] $options An array of InputOption objects |
|
218 | + */ |
|
219 | + public function addOptions(array $options = []) |
|
220 | + { |
|
221 | + foreach ($options as $option) { |
|
222 | + $this->addOption($option); |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @throws LogicException When option given already exist |
|
228 | + */ |
|
229 | + public function addOption(InputOption $option) |
|
230 | + { |
|
231 | + if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) { |
|
232 | + throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName())); |
|
233 | + } |
|
234 | + if (isset($this->negations[$option->getName()])) { |
|
235 | + throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName())); |
|
236 | + } |
|
237 | + |
|
238 | + if ($option->getShortcut()) { |
|
239 | + foreach (explode('|', $option->getShortcut()) as $shortcut) { |
|
240 | + if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) { |
|
241 | + throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut)); |
|
242 | + } |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + $this->options[$option->getName()] = $option; |
|
247 | + if ($option->getShortcut()) { |
|
248 | + foreach (explode('|', $option->getShortcut()) as $shortcut) { |
|
249 | + $this->shortcuts[$shortcut] = $option->getName(); |
|
250 | + } |
|
251 | + } |
|
252 | + |
|
253 | + if ($option->isNegatable()) { |
|
254 | + $negatedName = 'no-'.$option->getName(); |
|
255 | + if (isset($this->options[$negatedName])) { |
|
256 | + throw new LogicException(sprintf('An option named "%s" already exists.', $negatedName)); |
|
257 | + } |
|
258 | + $this->negations[$negatedName] = $option->getName(); |
|
259 | + } |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Returns an InputOption by name. |
|
264 | + * |
|
265 | + * @return InputOption A InputOption object |
|
266 | + * |
|
267 | + * @throws InvalidArgumentException When option given doesn't exist |
|
268 | + */ |
|
269 | + public function getOption(string $name) |
|
270 | + { |
|
271 | + if (!$this->hasOption($name)) { |
|
272 | + throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name)); |
|
273 | + } |
|
274 | + |
|
275 | + return $this->options[$name]; |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Returns true if an InputOption object exists by name. |
|
280 | + * |
|
281 | + * This method can't be used to check if the user included the option when |
|
282 | + * executing the command (use getOption() instead). |
|
283 | + * |
|
284 | + * @return bool true if the InputOption object exists, false otherwise |
|
285 | + */ |
|
286 | + public function hasOption(string $name) |
|
287 | + { |
|
288 | + return isset($this->options[$name]); |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Gets the array of InputOption objects. |
|
293 | + * |
|
294 | + * @return InputOption[] An array of InputOption objects |
|
295 | + */ |
|
296 | + public function getOptions() |
|
297 | + { |
|
298 | + return $this->options; |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Returns true if an InputOption object exists by shortcut. |
|
303 | + * |
|
304 | + * @return bool true if the InputOption object exists, false otherwise |
|
305 | + */ |
|
306 | + public function hasShortcut(string $name) |
|
307 | + { |
|
308 | + return isset($this->shortcuts[$name]); |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Returns true if an InputOption object exists by negated name. |
|
313 | + */ |
|
314 | + public function hasNegation(string $name): bool |
|
315 | + { |
|
316 | + return isset($this->negations[$name]); |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Gets an InputOption by shortcut. |
|
321 | + * |
|
322 | + * @return InputOption An InputOption object |
|
323 | + */ |
|
324 | + public function getOptionForShortcut(string $shortcut) |
|
325 | + { |
|
326 | + return $this->getOption($this->shortcutToName($shortcut)); |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * @return array<string|bool|int|float|array|null> |
|
331 | + */ |
|
332 | + public function getOptionDefaults() |
|
333 | + { |
|
334 | + $values = []; |
|
335 | + foreach ($this->options as $option) { |
|
336 | + $values[$option->getName()] = $option->getDefault(); |
|
337 | + } |
|
338 | + |
|
339 | + return $values; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Returns the InputOption name given a shortcut. |
|
344 | + * |
|
345 | + * @throws InvalidArgumentException When option given does not exist |
|
346 | + * |
|
347 | + * @internal |
|
348 | + */ |
|
349 | + public function shortcutToName(string $shortcut): string |
|
350 | + { |
|
351 | + if (!isset($this->shortcuts[$shortcut])) { |
|
352 | + throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
353 | + } |
|
354 | + |
|
355 | + return $this->shortcuts[$shortcut]; |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Returns the InputOption name given a negation. |
|
360 | + * |
|
361 | + * @throws InvalidArgumentException When option given does not exist |
|
362 | + * |
|
363 | + * @internal |
|
364 | + */ |
|
365 | + public function negationToName(string $negation): string |
|
366 | + { |
|
367 | + if (!isset($this->negations[$negation])) { |
|
368 | + throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $negation)); |
|
369 | + } |
|
370 | + |
|
371 | + return $this->negations[$negation]; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Gets the synopsis. |
|
376 | + * |
|
377 | + * @return string The synopsis |
|
378 | + */ |
|
379 | + public function getSynopsis(bool $short = false) |
|
380 | + { |
|
381 | + $elements = []; |
|
382 | + |
|
383 | + if ($short && $this->getOptions()) { |
|
384 | + $elements[] = '[options]'; |
|
385 | + } elseif (!$short) { |
|
386 | + foreach ($this->getOptions() as $option) { |
|
387 | + $value = ''; |
|
388 | + if ($option->acceptValue()) { |
|
389 | + $value = sprintf( |
|
390 | + ' %s%s%s', |
|
391 | + $option->isValueOptional() ? '[' : '', |
|
392 | + strtoupper($option->getName()), |
|
393 | + $option->isValueOptional() ? ']' : '' |
|
394 | + ); |
|
395 | + } |
|
396 | + |
|
397 | + $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : ''; |
|
398 | + $negation = $option->isNegatable() ? sprintf('|--no-%s', $option->getName()) : ''; |
|
399 | + $elements[] = sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation); |
|
400 | + } |
|
401 | + } |
|
402 | + |
|
403 | + if (\count($elements) && $this->getArguments()) { |
|
404 | + $elements[] = '[--]'; |
|
405 | + } |
|
406 | + |
|
407 | + $tail = ''; |
|
408 | + foreach ($this->getArguments() as $argument) { |
|
409 | + $element = '<'.$argument->getName().'>'; |
|
410 | + if ($argument->isArray()) { |
|
411 | + $element .= '...'; |
|
412 | + } |
|
413 | + |
|
414 | + if (!$argument->isRequired()) { |
|
415 | + $element = '['.$element; |
|
416 | + $tail .= ']'; |
|
417 | + } |
|
418 | + |
|
419 | + $elements[] = $element; |
|
420 | + } |
|
421 | + |
|
422 | + return implode(' ', $elements).$tail; |
|
423 | + } |
|
424 | 424 | } |
@@ -19,8 +19,8 @@ |
||
19 | 19 | */ |
20 | 20 | interface InputAwareInterface |
21 | 21 | { |
22 | - /** |
|
23 | - * Sets the Console Input. |
|
24 | - */ |
|
25 | - public function setInput(InputInterface $input); |
|
22 | + /** |
|
23 | + * Sets the Console Input. |
|
24 | + */ |
|
25 | + public function setInput(InputInterface $input); |
|
26 | 26 | } |
@@ -27,187 +27,187 @@ |
||
27 | 27 | */ |
28 | 28 | abstract class Input implements InputInterface, StreamableInputInterface |
29 | 29 | { |
30 | - protected $definition; |
|
31 | - protected $stream; |
|
32 | - protected $options = []; |
|
33 | - protected $arguments = []; |
|
34 | - protected $interactive = true; |
|
35 | - |
|
36 | - public function __construct(InputDefinition $definition = null) |
|
37 | - { |
|
38 | - if (null === $definition) { |
|
39 | - $this->definition = new InputDefinition(); |
|
40 | - } else { |
|
41 | - $this->bind($definition); |
|
42 | - $this->validate(); |
|
43 | - } |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * {@inheritdoc} |
|
48 | - */ |
|
49 | - public function bind(InputDefinition $definition) |
|
50 | - { |
|
51 | - $this->arguments = []; |
|
52 | - $this->options = []; |
|
53 | - $this->definition = $definition; |
|
54 | - |
|
55 | - $this->parse(); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Processes command line arguments. |
|
60 | - */ |
|
61 | - abstract protected function parse(); |
|
62 | - |
|
63 | - /** |
|
64 | - * {@inheritdoc} |
|
65 | - */ |
|
66 | - public function validate() |
|
67 | - { |
|
68 | - $definition = $this->definition; |
|
69 | - $givenArguments = $this->arguments; |
|
70 | - |
|
71 | - $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) { |
|
72 | - return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired(); |
|
73 | - }); |
|
74 | - |
|
75 | - if (\count($missingArguments) > 0) { |
|
76 | - throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments))); |
|
77 | - } |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * {@inheritdoc} |
|
82 | - */ |
|
83 | - public function isInteractive() |
|
84 | - { |
|
85 | - return $this->interactive; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * {@inheritdoc} |
|
90 | - */ |
|
91 | - public function setInteractive(bool $interactive) |
|
92 | - { |
|
93 | - $this->interactive = $interactive; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * {@inheritdoc} |
|
98 | - */ |
|
99 | - public function getArguments() |
|
100 | - { |
|
101 | - return array_merge($this->definition->getArgumentDefaults(), $this->arguments); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * {@inheritdoc} |
|
106 | - */ |
|
107 | - public function getArgument(string $name) |
|
108 | - { |
|
109 | - if (!$this->definition->hasArgument($name)) { |
|
110 | - throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
111 | - } |
|
112 | - |
|
113 | - return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault(); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * {@inheritdoc} |
|
118 | - */ |
|
119 | - public function setArgument(string $name, $value) |
|
120 | - { |
|
121 | - if (!$this->definition->hasArgument($name)) { |
|
122 | - throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
123 | - } |
|
124 | - |
|
125 | - $this->arguments[$name] = $value; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * {@inheritdoc} |
|
130 | - */ |
|
131 | - public function hasArgument(string $name) |
|
132 | - { |
|
133 | - return $this->definition->hasArgument($name); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * {@inheritdoc} |
|
138 | - */ |
|
139 | - public function getOptions() |
|
140 | - { |
|
141 | - return array_merge($this->definition->getOptionDefaults(), $this->options); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * {@inheritdoc} |
|
146 | - */ |
|
147 | - public function getOption(string $name) |
|
148 | - { |
|
149 | - if ($this->definition->hasNegation($name)) { |
|
150 | - if (null === $value = $this->getOption($this->definition->negationToName($name))) { |
|
151 | - return $value; |
|
152 | - } |
|
153 | - |
|
154 | - return !$value; |
|
155 | - } |
|
156 | - |
|
157 | - if (!$this->definition->hasOption($name)) { |
|
158 | - throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
|
159 | - } |
|
160 | - |
|
161 | - return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * {@inheritdoc} |
|
166 | - */ |
|
167 | - public function setOption(string $name, $value) |
|
168 | - { |
|
169 | - if ($this->definition->hasNegation($name)) { |
|
170 | - $this->options[$this->definition->negationToName($name)] = !$value; |
|
171 | - |
|
172 | - return; |
|
173 | - } elseif (!$this->definition->hasOption($name)) { |
|
174 | - throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
|
175 | - } |
|
176 | - |
|
177 | - $this->options[$name] = $value; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * {@inheritdoc} |
|
182 | - */ |
|
183 | - public function hasOption(string $name) |
|
184 | - { |
|
185 | - return $this->definition->hasOption($name) || $this->definition->hasNegation($name); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Escapes a token through escapeshellarg if it contains unsafe chars. |
|
190 | - * |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - public function escapeToken(string $token) |
|
194 | - { |
|
195 | - return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * {@inheritdoc} |
|
200 | - */ |
|
201 | - public function setStream($stream) |
|
202 | - { |
|
203 | - $this->stream = $stream; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * {@inheritdoc} |
|
208 | - */ |
|
209 | - public function getStream() |
|
210 | - { |
|
211 | - return $this->stream; |
|
212 | - } |
|
30 | + protected $definition; |
|
31 | + protected $stream; |
|
32 | + protected $options = []; |
|
33 | + protected $arguments = []; |
|
34 | + protected $interactive = true; |
|
35 | + |
|
36 | + public function __construct(InputDefinition $definition = null) |
|
37 | + { |
|
38 | + if (null === $definition) { |
|
39 | + $this->definition = new InputDefinition(); |
|
40 | + } else { |
|
41 | + $this->bind($definition); |
|
42 | + $this->validate(); |
|
43 | + } |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * {@inheritdoc} |
|
48 | + */ |
|
49 | + public function bind(InputDefinition $definition) |
|
50 | + { |
|
51 | + $this->arguments = []; |
|
52 | + $this->options = []; |
|
53 | + $this->definition = $definition; |
|
54 | + |
|
55 | + $this->parse(); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Processes command line arguments. |
|
60 | + */ |
|
61 | + abstract protected function parse(); |
|
62 | + |
|
63 | + /** |
|
64 | + * {@inheritdoc} |
|
65 | + */ |
|
66 | + public function validate() |
|
67 | + { |
|
68 | + $definition = $this->definition; |
|
69 | + $givenArguments = $this->arguments; |
|
70 | + |
|
71 | + $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) { |
|
72 | + return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired(); |
|
73 | + }); |
|
74 | + |
|
75 | + if (\count($missingArguments) > 0) { |
|
76 | + throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments))); |
|
77 | + } |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * {@inheritdoc} |
|
82 | + */ |
|
83 | + public function isInteractive() |
|
84 | + { |
|
85 | + return $this->interactive; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * {@inheritdoc} |
|
90 | + */ |
|
91 | + public function setInteractive(bool $interactive) |
|
92 | + { |
|
93 | + $this->interactive = $interactive; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * {@inheritdoc} |
|
98 | + */ |
|
99 | + public function getArguments() |
|
100 | + { |
|
101 | + return array_merge($this->definition->getArgumentDefaults(), $this->arguments); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * {@inheritdoc} |
|
106 | + */ |
|
107 | + public function getArgument(string $name) |
|
108 | + { |
|
109 | + if (!$this->definition->hasArgument($name)) { |
|
110 | + throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
111 | + } |
|
112 | + |
|
113 | + return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault(); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * {@inheritdoc} |
|
118 | + */ |
|
119 | + public function setArgument(string $name, $value) |
|
120 | + { |
|
121 | + if (!$this->definition->hasArgument($name)) { |
|
122 | + throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
123 | + } |
|
124 | + |
|
125 | + $this->arguments[$name] = $value; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * {@inheritdoc} |
|
130 | + */ |
|
131 | + public function hasArgument(string $name) |
|
132 | + { |
|
133 | + return $this->definition->hasArgument($name); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * {@inheritdoc} |
|
138 | + */ |
|
139 | + public function getOptions() |
|
140 | + { |
|
141 | + return array_merge($this->definition->getOptionDefaults(), $this->options); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * {@inheritdoc} |
|
146 | + */ |
|
147 | + public function getOption(string $name) |
|
148 | + { |
|
149 | + if ($this->definition->hasNegation($name)) { |
|
150 | + if (null === $value = $this->getOption($this->definition->negationToName($name))) { |
|
151 | + return $value; |
|
152 | + } |
|
153 | + |
|
154 | + return !$value; |
|
155 | + } |
|
156 | + |
|
157 | + if (!$this->definition->hasOption($name)) { |
|
158 | + throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
|
159 | + } |
|
160 | + |
|
161 | + return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * {@inheritdoc} |
|
166 | + */ |
|
167 | + public function setOption(string $name, $value) |
|
168 | + { |
|
169 | + if ($this->definition->hasNegation($name)) { |
|
170 | + $this->options[$this->definition->negationToName($name)] = !$value; |
|
171 | + |
|
172 | + return; |
|
173 | + } elseif (!$this->definition->hasOption($name)) { |
|
174 | + throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); |
|
175 | + } |
|
176 | + |
|
177 | + $this->options[$name] = $value; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * {@inheritdoc} |
|
182 | + */ |
|
183 | + public function hasOption(string $name) |
|
184 | + { |
|
185 | + return $this->definition->hasOption($name) || $this->definition->hasNegation($name); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Escapes a token through escapeshellarg if it contains unsafe chars. |
|
190 | + * |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + public function escapeToken(string $token) |
|
194 | + { |
|
195 | + return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * {@inheritdoc} |
|
200 | + */ |
|
201 | + public function setStream($stream) |
|
202 | + { |
|
203 | + $this->stream = $stream; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * {@inheritdoc} |
|
208 | + */ |
|
209 | + public function getStream() |
|
210 | + { |
|
211 | + return $this->stream; |
|
212 | + } |
|
213 | 213 | } |
@@ -25,186 +25,186 @@ |
||
25 | 25 | */ |
26 | 26 | class ArrayInput extends Input |
27 | 27 | { |
28 | - private $parameters; |
|
29 | - |
|
30 | - public function __construct(array $parameters, InputDefinition $definition = null) |
|
31 | - { |
|
32 | - $this->parameters = $parameters; |
|
33 | - |
|
34 | - parent::__construct($definition); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - public function getFirstArgument() |
|
41 | - { |
|
42 | - foreach ($this->parameters as $param => $value) { |
|
43 | - if ($param && \is_string($param) && '-' === $param[0]) { |
|
44 | - continue; |
|
45 | - } |
|
46 | - |
|
47 | - return $value; |
|
48 | - } |
|
49 | - |
|
50 | - return null; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * {@inheritdoc} |
|
55 | - */ |
|
56 | - public function hasParameterOption($values, bool $onlyParams = false) |
|
57 | - { |
|
58 | - $values = (array) $values; |
|
59 | - |
|
60 | - foreach ($this->parameters as $k => $v) { |
|
61 | - if (!\is_int($k)) { |
|
62 | - $v = $k; |
|
63 | - } |
|
64 | - |
|
65 | - if ($onlyParams && '--' === $v) { |
|
66 | - return false; |
|
67 | - } |
|
68 | - |
|
69 | - if (\in_array($v, $values)) { |
|
70 | - return true; |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - return false; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * {@inheritdoc} |
|
79 | - */ |
|
80 | - public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
81 | - { |
|
82 | - $values = (array) $values; |
|
83 | - |
|
84 | - foreach ($this->parameters as $k => $v) { |
|
85 | - if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) { |
|
86 | - return $default; |
|
87 | - } |
|
88 | - |
|
89 | - if (\is_int($k)) { |
|
90 | - if (\in_array($v, $values)) { |
|
91 | - return true; |
|
92 | - } |
|
93 | - } elseif (\in_array($k, $values)) { |
|
94 | - return $v; |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - return $default; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Returns a stringified representation of the args passed to the command. |
|
103 | - * |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function __toString() |
|
107 | - { |
|
108 | - $params = []; |
|
109 | - foreach ($this->parameters as $param => $val) { |
|
110 | - if ($param && \is_string($param) && '-' === $param[0]) { |
|
111 | - $glue = ('-' === $param[1]) ? '=' : ' '; |
|
112 | - if (\is_array($val)) { |
|
113 | - foreach ($val as $v) { |
|
114 | - $params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : ''); |
|
115 | - } |
|
116 | - } else { |
|
117 | - $params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : ''); |
|
118 | - } |
|
119 | - } else { |
|
120 | - $params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - return implode(' ', $params); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * {@inheritdoc} |
|
129 | - */ |
|
130 | - protected function parse() |
|
131 | - { |
|
132 | - foreach ($this->parameters as $key => $value) { |
|
133 | - if ('--' === $key) { |
|
134 | - return; |
|
135 | - } |
|
136 | - if (str_starts_with($key, '--')) { |
|
137 | - $this->addLongOption(substr($key, 2), $value); |
|
138 | - } elseif (str_starts_with($key, '-')) { |
|
139 | - $this->addShortOption(substr($key, 1), $value); |
|
140 | - } else { |
|
141 | - $this->addArgument($key, $value); |
|
142 | - } |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Adds a short option value. |
|
148 | - * |
|
149 | - * @throws InvalidOptionException When option given doesn't exist |
|
150 | - */ |
|
151 | - private function addShortOption(string $shortcut, $value) |
|
152 | - { |
|
153 | - if (!$this->definition->hasShortcut($shortcut)) { |
|
154 | - throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
155 | - } |
|
156 | - |
|
157 | - $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Adds a long option value. |
|
162 | - * |
|
163 | - * @throws InvalidOptionException When option given doesn't exist |
|
164 | - * @throws InvalidOptionException When a required value is missing |
|
165 | - */ |
|
166 | - private function addLongOption(string $name, $value) |
|
167 | - { |
|
168 | - if (!$this->definition->hasOption($name)) { |
|
169 | - if (!$this->definition->hasNegation($name)) { |
|
170 | - throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name)); |
|
171 | - } |
|
172 | - |
|
173 | - $optionName = $this->definition->negationToName($name); |
|
174 | - $this->options[$optionName] = false; |
|
175 | - |
|
176 | - return; |
|
177 | - } |
|
178 | - |
|
179 | - $option = $this->definition->getOption($name); |
|
180 | - |
|
181 | - if (null === $value) { |
|
182 | - if ($option->isValueRequired()) { |
|
183 | - throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); |
|
184 | - } |
|
185 | - |
|
186 | - if (!$option->isValueOptional()) { |
|
187 | - $value = true; |
|
188 | - } |
|
189 | - } |
|
190 | - |
|
191 | - $this->options[$name] = $value; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Adds an argument value. |
|
196 | - * |
|
197 | - * @param string|int $name The argument name |
|
198 | - * @param mixed $value The value for the argument |
|
199 | - * |
|
200 | - * @throws InvalidArgumentException When argument given doesn't exist |
|
201 | - */ |
|
202 | - private function addArgument($name, $value) |
|
203 | - { |
|
204 | - if (!$this->definition->hasArgument($name)) { |
|
205 | - throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
206 | - } |
|
207 | - |
|
208 | - $this->arguments[$name] = $value; |
|
209 | - } |
|
28 | + private $parameters; |
|
29 | + |
|
30 | + public function __construct(array $parameters, InputDefinition $definition = null) |
|
31 | + { |
|
32 | + $this->parameters = $parameters; |
|
33 | + |
|
34 | + parent::__construct($definition); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + public function getFirstArgument() |
|
41 | + { |
|
42 | + foreach ($this->parameters as $param => $value) { |
|
43 | + if ($param && \is_string($param) && '-' === $param[0]) { |
|
44 | + continue; |
|
45 | + } |
|
46 | + |
|
47 | + return $value; |
|
48 | + } |
|
49 | + |
|
50 | + return null; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * {@inheritdoc} |
|
55 | + */ |
|
56 | + public function hasParameterOption($values, bool $onlyParams = false) |
|
57 | + { |
|
58 | + $values = (array) $values; |
|
59 | + |
|
60 | + foreach ($this->parameters as $k => $v) { |
|
61 | + if (!\is_int($k)) { |
|
62 | + $v = $k; |
|
63 | + } |
|
64 | + |
|
65 | + if ($onlyParams && '--' === $v) { |
|
66 | + return false; |
|
67 | + } |
|
68 | + |
|
69 | + if (\in_array($v, $values)) { |
|
70 | + return true; |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + return false; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * {@inheritdoc} |
|
79 | + */ |
|
80 | + public function getParameterOption($values, $default = false, bool $onlyParams = false) |
|
81 | + { |
|
82 | + $values = (array) $values; |
|
83 | + |
|
84 | + foreach ($this->parameters as $k => $v) { |
|
85 | + if ($onlyParams && ('--' === $k || (\is_int($k) && '--' === $v))) { |
|
86 | + return $default; |
|
87 | + } |
|
88 | + |
|
89 | + if (\is_int($k)) { |
|
90 | + if (\in_array($v, $values)) { |
|
91 | + return true; |
|
92 | + } |
|
93 | + } elseif (\in_array($k, $values)) { |
|
94 | + return $v; |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + return $default; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Returns a stringified representation of the args passed to the command. |
|
103 | + * |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function __toString() |
|
107 | + { |
|
108 | + $params = []; |
|
109 | + foreach ($this->parameters as $param => $val) { |
|
110 | + if ($param && \is_string($param) && '-' === $param[0]) { |
|
111 | + $glue = ('-' === $param[1]) ? '=' : ' '; |
|
112 | + if (\is_array($val)) { |
|
113 | + foreach ($val as $v) { |
|
114 | + $params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : ''); |
|
115 | + } |
|
116 | + } else { |
|
117 | + $params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : ''); |
|
118 | + } |
|
119 | + } else { |
|
120 | + $params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + return implode(' ', $params); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * {@inheritdoc} |
|
129 | + */ |
|
130 | + protected function parse() |
|
131 | + { |
|
132 | + foreach ($this->parameters as $key => $value) { |
|
133 | + if ('--' === $key) { |
|
134 | + return; |
|
135 | + } |
|
136 | + if (str_starts_with($key, '--')) { |
|
137 | + $this->addLongOption(substr($key, 2), $value); |
|
138 | + } elseif (str_starts_with($key, '-')) { |
|
139 | + $this->addShortOption(substr($key, 1), $value); |
|
140 | + } else { |
|
141 | + $this->addArgument($key, $value); |
|
142 | + } |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Adds a short option value. |
|
148 | + * |
|
149 | + * @throws InvalidOptionException When option given doesn't exist |
|
150 | + */ |
|
151 | + private function addShortOption(string $shortcut, $value) |
|
152 | + { |
|
153 | + if (!$this->definition->hasShortcut($shortcut)) { |
|
154 | + throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut)); |
|
155 | + } |
|
156 | + |
|
157 | + $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Adds a long option value. |
|
162 | + * |
|
163 | + * @throws InvalidOptionException When option given doesn't exist |
|
164 | + * @throws InvalidOptionException When a required value is missing |
|
165 | + */ |
|
166 | + private function addLongOption(string $name, $value) |
|
167 | + { |
|
168 | + if (!$this->definition->hasOption($name)) { |
|
169 | + if (!$this->definition->hasNegation($name)) { |
|
170 | + throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name)); |
|
171 | + } |
|
172 | + |
|
173 | + $optionName = $this->definition->negationToName($name); |
|
174 | + $this->options[$optionName] = false; |
|
175 | + |
|
176 | + return; |
|
177 | + } |
|
178 | + |
|
179 | + $option = $this->definition->getOption($name); |
|
180 | + |
|
181 | + if (null === $value) { |
|
182 | + if ($option->isValueRequired()) { |
|
183 | + throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name)); |
|
184 | + } |
|
185 | + |
|
186 | + if (!$option->isValueOptional()) { |
|
187 | + $value = true; |
|
188 | + } |
|
189 | + } |
|
190 | + |
|
191 | + $this->options[$name] = $value; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Adds an argument value. |
|
196 | + * |
|
197 | + * @param string|int $name The argument name |
|
198 | + * @param mixed $value The value for the argument |
|
199 | + * |
|
200 | + * @throws InvalidArgumentException When argument given doesn't exist |
|
201 | + */ |
|
202 | + private function addArgument($name, $value) |
|
203 | + { |
|
204 | + if (!$this->definition->hasArgument($name)) { |
|
205 | + throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); |
|
206 | + } |
|
207 | + |
|
208 | + $this->arguments[$name] = $value; |
|
209 | + } |
|
210 | 210 | } |
@@ -13,53 +13,53 @@ |
||
13 | 13 | |
14 | 14 | final class SignalRegistry |
15 | 15 | { |
16 | - private $signalHandlers = []; |
|
16 | + private $signalHandlers = []; |
|
17 | 17 | |
18 | - public function __construct() |
|
19 | - { |
|
20 | - if (\function_exists('pcntl_async_signals')) { |
|
21 | - pcntl_async_signals(true); |
|
22 | - } |
|
23 | - } |
|
18 | + public function __construct() |
|
19 | + { |
|
20 | + if (\function_exists('pcntl_async_signals')) { |
|
21 | + pcntl_async_signals(true); |
|
22 | + } |
|
23 | + } |
|
24 | 24 | |
25 | - public function register(int $signal, callable $signalHandler): void |
|
26 | - { |
|
27 | - if (!isset($this->signalHandlers[$signal])) { |
|
28 | - $previousCallback = pcntl_signal_get_handler($signal); |
|
25 | + public function register(int $signal, callable $signalHandler): void |
|
26 | + { |
|
27 | + if (!isset($this->signalHandlers[$signal])) { |
|
28 | + $previousCallback = pcntl_signal_get_handler($signal); |
|
29 | 29 | |
30 | - if (\is_callable($previousCallback)) { |
|
31 | - $this->signalHandlers[$signal][] = $previousCallback; |
|
32 | - } |
|
33 | - } |
|
30 | + if (\is_callable($previousCallback)) { |
|
31 | + $this->signalHandlers[$signal][] = $previousCallback; |
|
32 | + } |
|
33 | + } |
|
34 | 34 | |
35 | - $this->signalHandlers[$signal][] = $signalHandler; |
|
35 | + $this->signalHandlers[$signal][] = $signalHandler; |
|
36 | 36 | |
37 | - pcntl_signal($signal, [$this, 'handle']); |
|
38 | - } |
|
37 | + pcntl_signal($signal, [$this, 'handle']); |
|
38 | + } |
|
39 | 39 | |
40 | - public static function isSupported(): bool |
|
41 | - { |
|
42 | - if (!\function_exists('pcntl_signal')) { |
|
43 | - return false; |
|
44 | - } |
|
40 | + public static function isSupported(): bool |
|
41 | + { |
|
42 | + if (!\function_exists('pcntl_signal')) { |
|
43 | + return false; |
|
44 | + } |
|
45 | 45 | |
46 | - if (\in_array('pcntl_signal', explode(',', ini_get('disable_functions')))) { |
|
47 | - return false; |
|
48 | - } |
|
46 | + if (\in_array('pcntl_signal', explode(',', ini_get('disable_functions')))) { |
|
47 | + return false; |
|
48 | + } |
|
49 | 49 | |
50 | - return true; |
|
51 | - } |
|
50 | + return true; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @internal |
|
55 | - */ |
|
56 | - public function handle(int $signal): void |
|
57 | - { |
|
58 | - $count = \count($this->signalHandlers[$signal]); |
|
53 | + /** |
|
54 | + * @internal |
|
55 | + */ |
|
56 | + public function handle(int $signal): void |
|
57 | + { |
|
58 | + $count = \count($this->signalHandlers[$signal]); |
|
59 | 59 | |
60 | - foreach ($this->signalHandlers[$signal] as $i => $signalHandler) { |
|
61 | - $hasNext = $i !== $count - 1; |
|
62 | - $signalHandler($signal, $hasNext); |
|
63 | - } |
|
64 | - } |
|
60 | + foreach ($this->signalHandlers[$signal] as $i => $signalHandler) { |
|
61 | + $hasNext = $i !== $count - 1; |
|
62 | + $signalHandler($signal, $hasNext); |
|
63 | + } |
|
64 | + } |
|
65 | 65 | } |
@@ -20,43 +20,43 @@ |
||
20 | 20 | */ |
21 | 21 | class FactoryCommandLoader implements CommandLoaderInterface |
22 | 22 | { |
23 | - private $factories; |
|
24 | - |
|
25 | - /** |
|
26 | - * @param callable[] $factories Indexed by command names |
|
27 | - */ |
|
28 | - public function __construct(array $factories) |
|
29 | - { |
|
30 | - $this->factories = $factories; |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * {@inheritdoc} |
|
35 | - */ |
|
36 | - public function has(string $name) |
|
37 | - { |
|
38 | - return isset($this->factories[$name]); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * {@inheritdoc} |
|
43 | - */ |
|
44 | - public function get(string $name) |
|
45 | - { |
|
46 | - if (!isset($this->factories[$name])) { |
|
47 | - throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); |
|
48 | - } |
|
49 | - |
|
50 | - $factory = $this->factories[$name]; |
|
51 | - |
|
52 | - return $factory(); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * {@inheritdoc} |
|
57 | - */ |
|
58 | - public function getNames() |
|
59 | - { |
|
60 | - return array_keys($this->factories); |
|
61 | - } |
|
23 | + private $factories; |
|
24 | + |
|
25 | + /** |
|
26 | + * @param callable[] $factories Indexed by command names |
|
27 | + */ |
|
28 | + public function __construct(array $factories) |
|
29 | + { |
|
30 | + $this->factories = $factories; |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * {@inheritdoc} |
|
35 | + */ |
|
36 | + public function has(string $name) |
|
37 | + { |
|
38 | + return isset($this->factories[$name]); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * {@inheritdoc} |
|
43 | + */ |
|
44 | + public function get(string $name) |
|
45 | + { |
|
46 | + if (!isset($this->factories[$name])) { |
|
47 | + throw new CommandNotFoundException(sprintf('Command "%s" does not exist.', $name)); |
|
48 | + } |
|
49 | + |
|
50 | + $factory = $this->factories[$name]; |
|
51 | + |
|
52 | + return $factory(); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * {@inheritdoc} |
|
57 | + */ |
|
58 | + public function getNames() |
|
59 | + { |
|
60 | + return array_keys($this->factories); |
|
61 | + } |
|
62 | 62 | } |