c9dd7f86feae79410b75873959e7ba10a172a54b
[yaffs-website] / vendor / drush / drush / src / Symfony / DrushArgvInput.php
1 <?php
2
3 /*
4  *
5  * A copy of ArgvInput (v3.4.2) while commenting out a section of hasParameterOption() per https://github.com/symfony/symfony/issues/25825.
6  *
7  * This file is part of the Symfony package.
8  *
9  * (c) Fabien Potencier <fabien@symfony.com>
10  *
11  * For the full copyright and license information, please view the LICENSE
12  * file that was distributed with this source code.
13  */
14
15 namespace Drush\Symfony;
16
17 use Symfony\Component\Console\Exception\RuntimeException;
18 use Symfony\Component\Console\Input\Input;
19 use Symfony\Component\Console\Input\ArgvInput;
20 use Symfony\Component\Console\Input\InputDefinition;
21
22 /**
23  * ArgvInput represents an input coming from the CLI arguments.
24  *
25  * Usage:
26  *
27  *     $input = new ArgvInput();
28  *
29  * By default, the `$_SERVER['argv']` array is used for the input values.
30  *
31  * This can be overridden by explicitly passing the input values in the constructor:
32  *
33  *     $input = new ArgvInput($_SERVER['argv']);
34  *
35  * If you pass it yourself, don't forget that the first element of the array
36  * is the name of the running application.
37  *
38  * When passing an argument to the constructor, be sure that it respects
39  * the same rules as the argv one. It's almost always better to use the
40  * `StringInput` when you want to provide your own input.
41  *
42  * @author Fabien Potencier <fabien@symfony.com>
43  *
44  * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
45  * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
46  */
47 class DrushArgvInput extends ArgvInput
48 {
49     private $tokens;
50     private $parsed;
51
52     /**
53      * @param array|null           $argv       An array of parameters from the CLI (in the argv format)
54      * @param InputDefinition|null $definition A InputDefinition instance
55      */
56     public function __construct(array $argv = null, InputDefinition $definition = null)
57     {
58         if (null === $argv) {
59             $argv = $_SERVER['argv'];
60         }
61
62         // strip the application name
63         array_shift($argv);
64
65         $this->tokens = $argv;
66
67         parent::__construct($definition);
68     }
69
70     protected function setTokens(array $tokens)
71     {
72         $this->tokens = $tokens;
73     }
74
75     /**
76      * {@inheritdoc}
77      */
78     protected function parse()
79     {
80         $parseOptions = true;
81         $this->parsed = $this->tokens;
82         while (null !== $token = array_shift($this->parsed)) {
83             if ($parseOptions && '' == $token) {
84                 $this->parseArgument($token);
85             } elseif ($parseOptions && '--' == $token) {
86                 $parseOptions = false;
87             } elseif ($parseOptions && 0 === strpos($token, '--')) {
88                 $this->parseLongOption($token);
89             } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
90                 $this->parseShortOption($token);
91             } else {
92                 $this->parseArgument($token);
93             }
94         }
95     }
96
97     /**
98      * Parses a short option.
99      *
100      * @param string $token The current token
101      */
102     private function parseShortOption($token)
103     {
104         $name = substr($token, 1);
105
106         if (strlen($name) > 1) {
107             if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
108                 // an option with a value (with no space)
109                 $this->addShortOption($name[0], substr($name, 1));
110             } else {
111                 $this->parseShortOptionSet($name);
112             }
113         } else {
114             $this->addShortOption($name, null);
115         }
116     }
117
118     /**
119      * Parses a short option set.
120      *
121      * @param string $name The current token
122      *
123      * @throws RuntimeException When option given doesn't exist
124      */
125     private function parseShortOptionSet($name)
126     {
127         $len = strlen($name);
128         for ($i = 0; $i < $len; ++$i) {
129             if (!$this->definition->hasShortcut($name[$i])) {
130                 throw new RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
131             }
132
133             $option = $this->definition->getOptionForShortcut($name[$i]);
134             if ($option->acceptValue()) {
135                 $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
136
137                 break;
138             } else {
139                 $this->addLongOption($option->getName(), null);
140             }
141         }
142     }
143
144     /**
145      * Parses a long option.
146      *
147      * @param string $token The current token
148      */
149     private function parseLongOption($token)
150     {
151         $name = substr($token, 2);
152
153         if (false !== $pos = strpos($name, '=')) {
154             if (0 === strlen($value = substr($name, $pos + 1))) {
155                 // if no value after "=" then substr() returns "" since php7 only, false before
156                 // see http://php.net/manual/fr/migration70.incompatible.php#119151
157                 if (\PHP_VERSION_ID < 70000 && false === $value) {
158                     $value = '';
159                 }
160                 array_unshift($this->parsed, $value);
161             }
162             $this->addLongOption(substr($name, 0, $pos), $value);
163         } else {
164             $this->addLongOption($name, null);
165         }
166     }
167
168     /**
169      * Parses an argument.
170      *
171      * @param string $token The current token
172      *
173      * @throws RuntimeException When too many arguments are given
174      */
175     private function parseArgument($token)
176     {
177         $c = count($this->arguments);
178
179         // if input is expecting another argument, add it
180         if ($this->definition->hasArgument($c)) {
181             $arg = $this->definition->getArgument($c);
182             $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
183
184             // if last argument isArray(), append token to last argument
185         } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
186             $arg = $this->definition->getArgument($c - 1);
187             $this->arguments[$arg->getName()][] = $token;
188
189             // unexpected argument
190         } else {
191             $all = $this->definition->getArguments();
192             if (count($all)) {
193                 throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
194             }
195
196             throw new RuntimeException(sprintf('No arguments expected, got "%s".', $token));
197         }
198     }
199
200     /**
201      * Adds a short option value.
202      *
203      * @param string $shortcut The short option key
204      * @param mixed  $value    The value for the option
205      *
206      * @throws RuntimeException When option given doesn't exist
207      */
208     private function addShortOption($shortcut, $value)
209     {
210         if (!$this->definition->hasShortcut($shortcut)) {
211             throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
212         }
213
214         $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
215     }
216
217     /**
218      * Adds a long option value.
219      *
220      * @param string $name  The long option key
221      * @param mixed  $value The value for the option
222      *
223      * @throws RuntimeException When option given doesn't exist
224      */
225     private function addLongOption($name, $value)
226     {
227         if (!$this->definition->hasOption($name)) {
228             throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
229         }
230
231         $option = $this->definition->getOption($name);
232
233         if (null !== $value && !$option->acceptValue()) {
234             throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
235         }
236
237         if (in_array($value, array('', null), true) && $option->acceptValue() && count($this->parsed)) {
238             // if option accepts an optional or mandatory argument
239             // let's see if there is one provided
240             $next = array_shift($this->parsed);
241             if ((isset($next[0]) && '-' !== $next[0]) || in_array($next, array('', null), true)) {
242                 $value = $next;
243             } else {
244                 array_unshift($this->parsed, $next);
245             }
246         }
247
248         if (null === $value) {
249             if ($option->isValueRequired()) {
250                 throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
251             }
252
253             if (!$option->isArray() && !$option->isValueOptional()) {
254                 $value = true;
255             }
256         }
257
258         if ($option->isArray()) {
259             $this->options[$name][] = $value;
260         } else {
261             $this->options[$name] = $value;
262         }
263     }
264
265     /**
266      * {@inheritdoc}
267      */
268     public function getFirstArgument()
269     {
270         foreach ($this->tokens as $token) {
271             if ($token && '-' === $token[0]) {
272                 continue;
273             }
274
275             return $token;
276         }
277     }
278
279     /**
280      * {@inheritdoc}
281      */
282     public function hasParameterOption($values, $onlyParams = false)
283     {
284         $values = (array) $values;
285
286         foreach ($this->tokens as $token) {
287             if ($onlyParams && '--' === $token) {
288                 return false;
289             }
290             foreach ($values as $value) {
291                 if ($token === $value || 0 === strpos($token, $value.'=')) {
292                     return true;
293                 }
294
295 /**
296  * Commented out until https://github.com/symfony/symfony/issues/25825 is resolved and released.
297  */
298 //                if (0 === strpos($token, '-') && 0 !== strpos($token, '--')) {
299 //                    $noValue = explode('=', $token);
300 //                    $token = $noValue[0];
301 //                    $searchableToken = str_replace('-', '', $token);
302 //                    $searchableValue = str_replace('-', '', $value);
303 //                    if ('' !== $searchableToken && '' !== $searchableValue && false !== strpos($searchableToken, $searchableValue)) {
304 //                        return true;
305 //                    }
306 //                }
307             }
308         }
309
310         return false;
311     }
312
313     /**
314      * {@inheritdoc}
315      */
316     public function getParameterOption($values, $default = false, $onlyParams = false)
317     {
318         $values = (array) $values;
319         $tokens = $this->tokens;
320
321         while (0 < count($tokens)) {
322             $token = array_shift($tokens);
323             if ($onlyParams && '--' === $token) {
324                 return false;
325             }
326
327             foreach ($values as $value) {
328                 if ($token === $value || 0 === strpos($token, $value.'=')) {
329                     if (false !== $pos = strpos($token, '=')) {
330                         return substr($token, $pos + 1);
331                     }
332
333                     return array_shift($tokens);
334                 }
335             }
336         }
337
338         return $default;
339     }
340
341     /**
342      * Returns a stringified representation of the args passed to the command.
343      *
344      * @return string
345      */
346     public function __toString()
347     {
348         $tokens = array_map(function ($token) {
349             if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
350                 return $match[1].$this->escapeToken($match[2]);
351             }
352
353             if ($token && '-' !== $token[0]) {
354                 return $this->escapeToken($token);
355             }
356
357             return $token;
358         }, $this->tokens);
359
360         return implode(' ', $tokens);
361     }
362 }