PluginProbe
Depicter — Popup & Slider Builder / 1.3.2
Depicter — Popup & Slider Builder v1.3.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Input / ArgvInput.php

ArgvInput.php in Depicter — Popup & Slider Builder 1.3.2, at vendor/symfony/console/Input/ArgvInput.php

348 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Console\Input;
13
14 use Symfony\Component\Console\Exception\RuntimeException;
15
16 /**
17 * ArgvInput represents an input coming from the CLI arguments.
18 *
19 * Usage:
20 *
21 * $input = new ArgvInput();
22 *
23 * By default, the `$_SERVER['argv']` array is used for the input values.
24 *
25 * This can be overridden by explicitly passing the input values in the constructor:
26 *
27 * $input = new ArgvInput($_SERVER['argv']);
28 *
29 * If you pass it yourself, don't forget that the first element of the array
30 * is the name of the running application.
31 *
32 * When passing an argument to the constructor, be sure that it respects
33 * the same rules as the argv one. It's almost always better to use the
34 * `StringInput` when you want to provide your own input.
35 *
36 * @author Fabien Potencier <fabien@symfony.com>
37 *
38 * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
39 * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
40 */
41 class ArgvInput extends Input
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 && 0 === strpos($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 if (\count($all)) {
169 throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
170 }
171
172 throw new RuntimeException(sprintf('No arguments expected, got "%s".', $token));
173 }
174 }
175
176 /**
177 * Adds a short option value.
178 *
179 * @throws RuntimeException When option given doesn't exist
180 */
181 private function addShortOption(string $shortcut, $value)
182 {
183 if (!$this->definition->hasShortcut($shortcut)) {
184 throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
185 }
186
187 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
188 }
189
190 /**
191 * Adds a long option value.
192 *
193 * @throws RuntimeException When option given doesn't exist
194 */
195 private function addLongOption(string $name, $value)
196 {
197 if (!$this->definition->hasOption($name)) {
198 throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
199 }
200
201 $option = $this->definition->getOption($name);
202
203 if (null !== $value && !$option->acceptValue()) {
204 throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
205 }
206
207 if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) {
208 // if option accepts an optional or mandatory argument
209 // let's see if there is one provided
210 $next = array_shift($this->parsed);
211 if ((isset($next[0]) && '-' !== $next[0]) || \in_array($next, ['', null], true)) {
212 $value = $next;
213 } else {
214 array_unshift($this->parsed, $next);
215 }
216 }
217
218 if (null === $value) {
219 if ($option->isValueRequired()) {
220 throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
221 }
222
223 if (!$option->isArray() && !$option->isValueOptional()) {
224 $value = true;
225 }
226 }
227
228 if ($option->isArray()) {
229 $this->options[$name][] = $value;
230 } else {
231 $this->options[$name] = $value;
232 }
233 }
234
235 /**
236 * {@inheritdoc}
237 */
238 public function getFirstArgument()
239 {
240 $isOption = false;
241 foreach ($this->tokens as $i => $token) {
242 if ($token && '-' === $token[0]) {
243 if (false !== strpos($token, '=') || !isset($this->tokens[$i + 1])) {
244 continue;
245 }
246
247 // If it's a long option, consider that everything after "--" is the option name.
248 // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator)
249 $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1);
250 if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) {
251 // noop
252 } elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) {
253 $isOption = true;
254 }
255
256 continue;
257 }
258
259 if ($isOption) {
260 $isOption = false;
261 continue;
262 }
263
264 return $token;
265 }
266
267 return null;
268 }
269
270 /**
271 * {@inheritdoc}
272 */
273 public function hasParameterOption($values, bool $onlyParams = false)
274 {
275 $values = (array) $values;
276
277 foreach ($this->tokens as $token) {
278 if ($onlyParams && '--' === $token) {
279 return false;
280 }
281 foreach ($values as $value) {
282 // Options with values:
283 // For long options, test for '--option=' at beginning
284 // For short options, test for '-o' at beginning
285 $leading = 0 === strpos($value, '--') ? $value.'=' : $value;
286 if ($token === $value || '' !== $leading && 0 === strpos($token, $leading)) {
287 return true;
288 }
289 }
290 }
291
292 return false;
293 }
294
295 /**
296 * {@inheritdoc}
297 */
298 public function getParameterOption($values, $default = false, bool $onlyParams = false)
299 {
300 $values = (array) $values;
301 $tokens = $this->tokens;
302
303 while (0 < \count($tokens)) {
304 $token = array_shift($tokens);
305 if ($onlyParams && '--' === $token) {
306 return $default;
307 }
308
309 foreach ($values as $value) {
310 if ($token === $value) {
311 return array_shift($tokens);
312 }
313 // Options with values:
314 // For long options, test for '--option=' at beginning
315 // For short options, test for '-o' at beginning
316 $leading = 0 === strpos($value, '--') ? $value.'=' : $value;
317 if ('' !== $leading && 0 === strpos($token, $leading)) {
318 return substr($token, \strlen($leading));
319 }
320 }
321 }
322
323 return $default;
324 }
325
326 /**
327 * Returns a stringified representation of the args passed to the command.
328 *
329 * @return string
330 */
331 public function __toString()
332 {
333 $tokens = array_map(function ($token) {
334 if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
335 return $match[1].$this->escapeToken($match[2]);
336 }
337
338 if ($token && '-' !== $token[0]) {
339 return $this->escapeToken($token);
340 }
341
342 return $token;
343 }, $this->tokens);
344
345 return implode(' ', $tokens);
346 }
347 }
348