Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\Input;
|
Chris@13
|
13
|
Chris@13
|
14 use Symfony\Component\Console\Input\InputDefinition;
|
Chris@13
|
15 use Symfony\Component\Console\Input\StringInput;
|
Chris@13
|
16
|
Chris@13
|
17 /**
|
Chris@13
|
18 * A StringInput subclass specialized for code arguments.
|
Chris@13
|
19 */
|
Chris@13
|
20 class ShellInput extends StringInput
|
Chris@13
|
21 {
|
Chris@13
|
22 private $hasCodeArgument = false;
|
Chris@13
|
23
|
Chris@13
|
24 /**
|
Chris@13
|
25 * Unlike the parent implementation's tokens, this contains an array of
|
Chris@13
|
26 * token/rest pairs, so that code arguments can be handled while parsing.
|
Chris@13
|
27 */
|
Chris@13
|
28 private $tokenPairs;
|
Chris@13
|
29 private $parsed;
|
Chris@13
|
30
|
Chris@13
|
31 /**
|
Chris@13
|
32 * Constructor.
|
Chris@13
|
33 *
|
Chris@13
|
34 * @param string $input An array of parameters from the CLI (in the argv format)
|
Chris@13
|
35 */
|
Chris@13
|
36 public function __construct($input)
|
Chris@13
|
37 {
|
Chris@13
|
38 parent::__construct($input);
|
Chris@13
|
39
|
Chris@13
|
40 $this->tokenPairs = $this->tokenize($input);
|
Chris@13
|
41 }
|
Chris@13
|
42
|
Chris@13
|
43 /**
|
Chris@13
|
44 * {@inheritdoc}
|
Chris@13
|
45 *
|
Chris@13
|
46 * @throws \InvalidArgumentException if $definition has CodeArgument before the final argument position
|
Chris@13
|
47 */
|
Chris@13
|
48 public function bind(InputDefinition $definition)
|
Chris@13
|
49 {
|
Chris@13
|
50 $hasCodeArgument = false;
|
Chris@13
|
51
|
Chris@13
|
52 if ($definition->getArgumentCount() > 0) {
|
Chris@13
|
53 $args = $definition->getArguments();
|
Chris@17
|
54 $lastArg = \array_pop($args);
|
Chris@13
|
55 foreach ($args as $arg) {
|
Chris@13
|
56 if ($arg instanceof CodeArgument) {
|
Chris@17
|
57 $msg = \sprintf('Unexpected CodeArgument before the final position: %s', $arg->getName());
|
Chris@13
|
58 throw new \InvalidArgumentException($msg);
|
Chris@13
|
59 }
|
Chris@13
|
60 }
|
Chris@13
|
61
|
Chris@13
|
62 if ($lastArg instanceof CodeArgument) {
|
Chris@13
|
63 $hasCodeArgument = true;
|
Chris@13
|
64 }
|
Chris@13
|
65 }
|
Chris@13
|
66
|
Chris@13
|
67 $this->hasCodeArgument = $hasCodeArgument;
|
Chris@13
|
68
|
Chris@13
|
69 return parent::bind($definition);
|
Chris@13
|
70 }
|
Chris@13
|
71
|
Chris@13
|
72 /**
|
Chris@13
|
73 * Tokenizes a string.
|
Chris@13
|
74 *
|
Chris@13
|
75 * The version of this on StringInput is good, but doesn't handle code
|
Chris@13
|
76 * arguments if they're at all complicated. This does :)
|
Chris@13
|
77 *
|
Chris@13
|
78 * @param string $input The input to tokenize
|
Chris@13
|
79 *
|
Chris@13
|
80 * @return array An array of token/rest pairs
|
Chris@13
|
81 *
|
Chris@13
|
82 * @throws \InvalidArgumentException When unable to parse input (should never happen)
|
Chris@13
|
83 */
|
Chris@13
|
84 private function tokenize($input)
|
Chris@13
|
85 {
|
Chris@13
|
86 $tokens = [];
|
Chris@17
|
87 $length = \strlen($input);
|
Chris@13
|
88 $cursor = 0;
|
Chris@13
|
89 while ($cursor < $length) {
|
Chris@17
|
90 if (\preg_match('/\s+/A', $input, $match, null, $cursor)) {
|
Chris@17
|
91 } elseif (\preg_match('/([^="\'\s]+?)(=?)(' . StringInput::REGEX_QUOTED_STRING . '+)/A', $input, $match, null, $cursor)) {
|
Chris@13
|
92 $tokens[] = [
|
Chris@17
|
93 $match[1] . $match[2] . \stripcslashes(\str_replace(['"\'', '\'"', '\'\'', '""'], '', \substr($match[3], 1, \strlen($match[3]) - 2))),
|
Chris@17
|
94 \stripcslashes(\substr($input, $cursor)),
|
Chris@13
|
95 ];
|
Chris@17
|
96 } elseif (\preg_match('/' . StringInput::REGEX_QUOTED_STRING . '/A', $input, $match, null, $cursor)) {
|
Chris@13
|
97 $tokens[] = [
|
Chris@17
|
98 \stripcslashes(\substr($match[0], 1, \strlen($match[0]) - 2)),
|
Chris@17
|
99 \stripcslashes(\substr($input, $cursor)),
|
Chris@13
|
100 ];
|
Chris@17
|
101 } elseif (\preg_match('/' . StringInput::REGEX_STRING . '/A', $input, $match, null, $cursor)) {
|
Chris@13
|
102 $tokens[] = [
|
Chris@17
|
103 \stripcslashes($match[1]),
|
Chris@17
|
104 \stripcslashes(\substr($input, $cursor)),
|
Chris@13
|
105 ];
|
Chris@13
|
106 } else {
|
Chris@13
|
107 // should never happen
|
Chris@16
|
108 // @codeCoverageIgnoreStart
|
Chris@17
|
109 throw new \InvalidArgumentException(\sprintf('Unable to parse input near "... %s ..."', \substr($input, $cursor, 10)));
|
Chris@16
|
110 // @codeCoverageIgnoreEnd
|
Chris@13
|
111 }
|
Chris@13
|
112
|
Chris@17
|
113 $cursor += \strlen($match[0]);
|
Chris@13
|
114 }
|
Chris@13
|
115
|
Chris@13
|
116 return $tokens;
|
Chris@13
|
117 }
|
Chris@13
|
118
|
Chris@13
|
119 /**
|
Chris@13
|
120 * Same as parent, but with some bonus handling for code arguments.
|
Chris@13
|
121 */
|
Chris@13
|
122 protected function parse()
|
Chris@13
|
123 {
|
Chris@13
|
124 $parseOptions = true;
|
Chris@13
|
125 $this->parsed = $this->tokenPairs;
|
Chris@17
|
126 while (null !== $tokenPair = \array_shift($this->parsed)) {
|
Chris@13
|
127 // token is what you'd expect. rest is the remainder of the input
|
Chris@13
|
128 // string, including token, and will be used if this is a code arg.
|
Chris@13
|
129 list($token, $rest) = $tokenPair;
|
Chris@13
|
130
|
Chris@13
|
131 if ($parseOptions && '' === $token) {
|
Chris@13
|
132 $this->parseShellArgument($token, $rest);
|
Chris@13
|
133 } elseif ($parseOptions && '--' === $token) {
|
Chris@13
|
134 $parseOptions = false;
|
Chris@17
|
135 } elseif ($parseOptions && 0 === \strpos($token, '--')) {
|
Chris@13
|
136 $this->parseLongOption($token);
|
Chris@13
|
137 } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
|
Chris@13
|
138 $this->parseShortOption($token);
|
Chris@13
|
139 } else {
|
Chris@13
|
140 $this->parseShellArgument($token, $rest);
|
Chris@13
|
141 }
|
Chris@13
|
142 }
|
Chris@13
|
143 }
|
Chris@13
|
144
|
Chris@13
|
145 /**
|
Chris@13
|
146 * Parses an argument, with bonus handling for code arguments.
|
Chris@13
|
147 *
|
Chris@13
|
148 * @param string $token The current token
|
Chris@13
|
149 * @param string $rest The remaining unparsed input, including the current token
|
Chris@13
|
150 *
|
Chris@13
|
151 * @throws \RuntimeException When too many arguments are given
|
Chris@13
|
152 */
|
Chris@13
|
153 private function parseShellArgument($token, $rest)
|
Chris@13
|
154 {
|
Chris@17
|
155 $c = \count($this->arguments);
|
Chris@13
|
156
|
Chris@13
|
157 // if input is expecting another argument, add it
|
Chris@13
|
158 if ($this->definition->hasArgument($c)) {
|
Chris@13
|
159 $arg = $this->definition->getArgument($c);
|
Chris@13
|
160
|
Chris@13
|
161 if ($arg instanceof CodeArgument) {
|
Chris@13
|
162 // When we find a code argument, we're done parsing. Add the
|
Chris@13
|
163 // remaining input to the current argument and call it a day.
|
Chris@13
|
164 $this->parsed = [];
|
Chris@13
|
165 $this->arguments[$arg->getName()] = $rest;
|
Chris@13
|
166 } else {
|
Chris@13
|
167 $this->arguments[$arg->getName()] = $arg->isArray() ? [$token] : $token;
|
Chris@13
|
168 }
|
Chris@13
|
169
|
Chris@13
|
170 return;
|
Chris@13
|
171 }
|
Chris@13
|
172
|
Chris@16
|
173 // (copypasta)
|
Chris@16
|
174 //
|
Chris@16
|
175 // @codeCoverageIgnoreStart
|
Chris@16
|
176
|
Chris@13
|
177 // if last argument isArray(), append token to last argument
|
Chris@13
|
178 if ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
|
Chris@13
|
179 $arg = $this->definition->getArgument($c - 1);
|
Chris@13
|
180 $this->arguments[$arg->getName()][] = $token;
|
Chris@13
|
181
|
Chris@13
|
182 return;
|
Chris@13
|
183 }
|
Chris@13
|
184
|
Chris@13
|
185 // unexpected argument
|
Chris@13
|
186 $all = $this->definition->getArguments();
|
Chris@17
|
187 if (\count($all)) {
|
Chris@17
|
188 throw new \RuntimeException(\sprintf('Too many arguments, expected arguments "%s".', \implode('" "', \array_keys($all))));
|
Chris@13
|
189 }
|
Chris@13
|
190
|
Chris@17
|
191 throw new \RuntimeException(\sprintf('No arguments expected, got "%s".', $token));
|
Chris@16
|
192 // @codeCoverageIgnoreEnd
|
Chris@13
|
193 }
|
Chris@13
|
194
|
Chris@13
|
195 // Everything below this is copypasta from ArgvInput private methods
|
Chris@16
|
196 // @codeCoverageIgnoreStart
|
Chris@13
|
197
|
Chris@13
|
198 /**
|
Chris@13
|
199 * Parses a short option.
|
Chris@13
|
200 *
|
Chris@13
|
201 * @param string $token The current token
|
Chris@13
|
202 */
|
Chris@13
|
203 private function parseShortOption($token)
|
Chris@13
|
204 {
|
Chris@17
|
205 $name = \substr($token, 1);
|
Chris@13
|
206
|
Chris@17
|
207 if (\strlen($name) > 1) {
|
Chris@13
|
208 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
|
Chris@13
|
209 // an option with a value (with no space)
|
Chris@17
|
210 $this->addShortOption($name[0], \substr($name, 1));
|
Chris@13
|
211 } else {
|
Chris@13
|
212 $this->parseShortOptionSet($name);
|
Chris@13
|
213 }
|
Chris@13
|
214 } else {
|
Chris@13
|
215 $this->addShortOption($name, null);
|
Chris@13
|
216 }
|
Chris@13
|
217 }
|
Chris@13
|
218
|
Chris@13
|
219 /**
|
Chris@13
|
220 * Parses a short option set.
|
Chris@13
|
221 *
|
Chris@13
|
222 * @param string $name The current token
|
Chris@13
|
223 *
|
Chris@13
|
224 * @throws \RuntimeException When option given doesn't exist
|
Chris@13
|
225 */
|
Chris@13
|
226 private function parseShortOptionSet($name)
|
Chris@13
|
227 {
|
Chris@17
|
228 $len = \strlen($name);
|
Chris@13
|
229 for ($i = 0; $i < $len; $i++) {
|
Chris@13
|
230 if (!$this->definition->hasShortcut($name[$i])) {
|
Chris@17
|
231 throw new \RuntimeException(\sprintf('The "-%s" option does not exist.', $name[$i]));
|
Chris@13
|
232 }
|
Chris@13
|
233
|
Chris@13
|
234 $option = $this->definition->getOptionForShortcut($name[$i]);
|
Chris@13
|
235 if ($option->acceptValue()) {
|
Chris@17
|
236 $this->addLongOption($option->getName(), $i === $len - 1 ? null : \substr($name, $i + 1));
|
Chris@13
|
237
|
Chris@13
|
238 break;
|
Chris@13
|
239 } else {
|
Chris@13
|
240 $this->addLongOption($option->getName(), null);
|
Chris@13
|
241 }
|
Chris@13
|
242 }
|
Chris@13
|
243 }
|
Chris@13
|
244
|
Chris@13
|
245 /**
|
Chris@13
|
246 * Parses a long option.
|
Chris@13
|
247 *
|
Chris@13
|
248 * @param string $token The current token
|
Chris@13
|
249 */
|
Chris@13
|
250 private function parseLongOption($token)
|
Chris@13
|
251 {
|
Chris@17
|
252 $name = \substr($token, 2);
|
Chris@13
|
253
|
Chris@17
|
254 if (false !== $pos = \strpos($name, '=')) {
|
Chris@17
|
255 if (0 === \strlen($value = \substr($name, $pos + 1))) {
|
Chris@13
|
256 // if no value after "=" then substr() returns "" since php7 only, false before
|
Chris@13
|
257 // see http://php.net/manual/fr/migration70.incompatible.php#119151
|
Chris@13
|
258 if (PHP_VERSION_ID < 70000 && false === $value) {
|
Chris@13
|
259 $value = '';
|
Chris@13
|
260 }
|
Chris@17
|
261 \array_unshift($this->parsed, [$value, null]);
|
Chris@13
|
262 }
|
Chris@17
|
263 $this->addLongOption(\substr($name, 0, $pos), $value);
|
Chris@13
|
264 } else {
|
Chris@13
|
265 $this->addLongOption($name, null);
|
Chris@13
|
266 }
|
Chris@13
|
267 }
|
Chris@13
|
268
|
Chris@13
|
269 /**
|
Chris@13
|
270 * Adds a short option value.
|
Chris@13
|
271 *
|
Chris@13
|
272 * @param string $shortcut The short option key
|
Chris@13
|
273 * @param mixed $value The value for the option
|
Chris@13
|
274 *
|
Chris@13
|
275 * @throws \RuntimeException When option given doesn't exist
|
Chris@13
|
276 */
|
Chris@13
|
277 private function addShortOption($shortcut, $value)
|
Chris@13
|
278 {
|
Chris@13
|
279 if (!$this->definition->hasShortcut($shortcut)) {
|
Chris@17
|
280 throw new \RuntimeException(\sprintf('The "-%s" option does not exist.', $shortcut));
|
Chris@13
|
281 }
|
Chris@13
|
282
|
Chris@13
|
283 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
|
Chris@13
|
284 }
|
Chris@13
|
285
|
Chris@13
|
286 /**
|
Chris@13
|
287 * Adds a long option value.
|
Chris@13
|
288 *
|
Chris@13
|
289 * @param string $name The long option key
|
Chris@13
|
290 * @param mixed $value The value for the option
|
Chris@13
|
291 *
|
Chris@13
|
292 * @throws \RuntimeException When option given doesn't exist
|
Chris@13
|
293 */
|
Chris@13
|
294 private function addLongOption($name, $value)
|
Chris@13
|
295 {
|
Chris@13
|
296 if (!$this->definition->hasOption($name)) {
|
Chris@17
|
297 throw new \RuntimeException(\sprintf('The "--%s" option does not exist.', $name));
|
Chris@13
|
298 }
|
Chris@13
|
299
|
Chris@13
|
300 $option = $this->definition->getOption($name);
|
Chris@13
|
301
|
Chris@13
|
302 if (null !== $value && !$option->acceptValue()) {
|
Chris@17
|
303 throw new \RuntimeException(\sprintf('The "--%s" option does not accept a value.', $name));
|
Chris@13
|
304 }
|
Chris@13
|
305
|
Chris@17
|
306 if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) {
|
Chris@13
|
307 // if option accepts an optional or mandatory argument
|
Chris@13
|
308 // let's see if there is one provided
|
Chris@17
|
309 $next = \array_shift($this->parsed);
|
Chris@13
|
310 $nextToken = $next[0];
|
Chris@17
|
311 if ((isset($nextToken[0]) && '-' !== $nextToken[0]) || \in_array($nextToken, ['', null], true)) {
|
Chris@13
|
312 $value = $nextToken;
|
Chris@13
|
313 } else {
|
Chris@17
|
314 \array_unshift($this->parsed, $next);
|
Chris@13
|
315 }
|
Chris@13
|
316 }
|
Chris@13
|
317
|
Chris@13
|
318 if (null === $value) {
|
Chris@13
|
319 if ($option->isValueRequired()) {
|
Chris@17
|
320 throw new \RuntimeException(\sprintf('The "--%s" option requires a value.', $name));
|
Chris@13
|
321 }
|
Chris@13
|
322
|
Chris@13
|
323 if (!$option->isArray() && !$option->isValueOptional()) {
|
Chris@13
|
324 $value = true;
|
Chris@13
|
325 }
|
Chris@13
|
326 }
|
Chris@13
|
327
|
Chris@13
|
328 if ($option->isArray()) {
|
Chris@13
|
329 $this->options[$name][] = $value;
|
Chris@13
|
330 } else {
|
Chris@13
|
331 $this->options[$name] = $value;
|
Chris@13
|
332 }
|
Chris@13
|
333 }
|
Chris@16
|
334
|
Chris@16
|
335 // @codeCoverageIgnoreEnd
|
Chris@13
|
336 }
|