Mercurial > hg > cmmr2012-drupal-site
comparison vendor/psy/psysh/src/Input/ShellInput.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
3:307d7a7fd348 | 4:a9cd425dd02b |
---|---|
49 { | 49 { |
50 $hasCodeArgument = false; | 50 $hasCodeArgument = false; |
51 | 51 |
52 if ($definition->getArgumentCount() > 0) { | 52 if ($definition->getArgumentCount() > 0) { |
53 $args = $definition->getArguments(); | 53 $args = $definition->getArguments(); |
54 $lastArg = array_pop($args); | 54 $lastArg = \array_pop($args); |
55 foreach ($args as $arg) { | 55 foreach ($args as $arg) { |
56 if ($arg instanceof CodeArgument) { | 56 if ($arg instanceof CodeArgument) { |
57 $msg = sprintf('Unexpected CodeArgument before the final position: %s', $arg->getName()); | 57 $msg = \sprintf('Unexpected CodeArgument before the final position: %s', $arg->getName()); |
58 throw new \InvalidArgumentException($msg); | 58 throw new \InvalidArgumentException($msg); |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 if ($lastArg instanceof CodeArgument) { | 62 if ($lastArg instanceof CodeArgument) { |
82 * @throws \InvalidArgumentException When unable to parse input (should never happen) | 82 * @throws \InvalidArgumentException When unable to parse input (should never happen) |
83 */ | 83 */ |
84 private function tokenize($input) | 84 private function tokenize($input) |
85 { | 85 { |
86 $tokens = []; | 86 $tokens = []; |
87 $length = strlen($input); | 87 $length = \strlen($input); |
88 $cursor = 0; | 88 $cursor = 0; |
89 while ($cursor < $length) { | 89 while ($cursor < $length) { |
90 if (preg_match('/\s+/A', $input, $match, null, $cursor)) { | 90 if (\preg_match('/\s+/A', $input, $match, null, $cursor)) { |
91 } elseif (preg_match('/([^="\'\s]+?)(=?)(' . StringInput::REGEX_QUOTED_STRING . '+)/A', $input, $match, null, $cursor)) { | 91 } elseif (\preg_match('/([^="\'\s]+?)(=?)(' . StringInput::REGEX_QUOTED_STRING . '+)/A', $input, $match, null, $cursor)) { |
92 $tokens[] = [ | 92 $tokens[] = [ |
93 $match[1] . $match[2] . stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, strlen($match[3]) - 2))), | 93 $match[1] . $match[2] . \stripcslashes(\str_replace(['"\'', '\'"', '\'\'', '""'], '', \substr($match[3], 1, \strlen($match[3]) - 2))), |
94 stripcslashes(substr($input, $cursor)), | 94 \stripcslashes(\substr($input, $cursor)), |
95 ]; | 95 ]; |
96 } elseif (preg_match('/' . StringInput::REGEX_QUOTED_STRING . '/A', $input, $match, null, $cursor)) { | 96 } elseif (\preg_match('/' . StringInput::REGEX_QUOTED_STRING . '/A', $input, $match, null, $cursor)) { |
97 $tokens[] = [ | 97 $tokens[] = [ |
98 stripcslashes(substr($match[0], 1, strlen($match[0]) - 2)), | 98 \stripcslashes(\substr($match[0], 1, \strlen($match[0]) - 2)), |
99 stripcslashes(substr($input, $cursor)), | 99 \stripcslashes(\substr($input, $cursor)), |
100 ]; | 100 ]; |
101 } elseif (preg_match('/' . StringInput::REGEX_STRING . '/A', $input, $match, null, $cursor)) { | 101 } elseif (\preg_match('/' . StringInput::REGEX_STRING . '/A', $input, $match, null, $cursor)) { |
102 $tokens[] = [ | 102 $tokens[] = [ |
103 stripcslashes($match[1]), | 103 \stripcslashes($match[1]), |
104 stripcslashes(substr($input, $cursor)), | 104 \stripcslashes(\substr($input, $cursor)), |
105 ]; | 105 ]; |
106 } else { | 106 } else { |
107 // should never happen | 107 // should never happen |
108 // @codeCoverageIgnoreStart | 108 // @codeCoverageIgnoreStart |
109 throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10))); | 109 throw new \InvalidArgumentException(\sprintf('Unable to parse input near "... %s ..."', \substr($input, $cursor, 10))); |
110 // @codeCoverageIgnoreEnd | 110 // @codeCoverageIgnoreEnd |
111 } | 111 } |
112 | 112 |
113 $cursor += strlen($match[0]); | 113 $cursor += \strlen($match[0]); |
114 } | 114 } |
115 | 115 |
116 return $tokens; | 116 return $tokens; |
117 } | 117 } |
118 | 118 |
121 */ | 121 */ |
122 protected function parse() | 122 protected function parse() |
123 { | 123 { |
124 $parseOptions = true; | 124 $parseOptions = true; |
125 $this->parsed = $this->tokenPairs; | 125 $this->parsed = $this->tokenPairs; |
126 while (null !== $tokenPair = array_shift($this->parsed)) { | 126 while (null !== $tokenPair = \array_shift($this->parsed)) { |
127 // token is what you'd expect. rest is the remainder of the input | 127 // token is what you'd expect. rest is the remainder of the input |
128 // string, including token, and will be used if this is a code arg. | 128 // string, including token, and will be used if this is a code arg. |
129 list($token, $rest) = $tokenPair; | 129 list($token, $rest) = $tokenPair; |
130 | 130 |
131 if ($parseOptions && '' === $token) { | 131 if ($parseOptions && '' === $token) { |
132 $this->parseShellArgument($token, $rest); | 132 $this->parseShellArgument($token, $rest); |
133 } elseif ($parseOptions && '--' === $token) { | 133 } elseif ($parseOptions && '--' === $token) { |
134 $parseOptions = false; | 134 $parseOptions = false; |
135 } elseif ($parseOptions && 0 === strpos($token, '--')) { | 135 } elseif ($parseOptions && 0 === \strpos($token, '--')) { |
136 $this->parseLongOption($token); | 136 $this->parseLongOption($token); |
137 } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { | 137 } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { |
138 $this->parseShortOption($token); | 138 $this->parseShortOption($token); |
139 } else { | 139 } else { |
140 $this->parseShellArgument($token, $rest); | 140 $this->parseShellArgument($token, $rest); |
150 * | 150 * |
151 * @throws \RuntimeException When too many arguments are given | 151 * @throws \RuntimeException When too many arguments are given |
152 */ | 152 */ |
153 private function parseShellArgument($token, $rest) | 153 private function parseShellArgument($token, $rest) |
154 { | 154 { |
155 $c = count($this->arguments); | 155 $c = \count($this->arguments); |
156 | 156 |
157 // if input is expecting another argument, add it | 157 // if input is expecting another argument, add it |
158 if ($this->definition->hasArgument($c)) { | 158 if ($this->definition->hasArgument($c)) { |
159 $arg = $this->definition->getArgument($c); | 159 $arg = $this->definition->getArgument($c); |
160 | 160 |
182 return; | 182 return; |
183 } | 183 } |
184 | 184 |
185 // unexpected argument | 185 // unexpected argument |
186 $all = $this->definition->getArguments(); | 186 $all = $this->definition->getArguments(); |
187 if (count($all)) { | 187 if (\count($all)) { |
188 throw new \RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all)))); | 188 throw new \RuntimeException(\sprintf('Too many arguments, expected arguments "%s".', \implode('" "', \array_keys($all)))); |
189 } | 189 } |
190 | 190 |
191 throw new \RuntimeException(sprintf('No arguments expected, got "%s".', $token)); | 191 throw new \RuntimeException(\sprintf('No arguments expected, got "%s".', $token)); |
192 // @codeCoverageIgnoreEnd | 192 // @codeCoverageIgnoreEnd |
193 } | 193 } |
194 | 194 |
195 // Everything below this is copypasta from ArgvInput private methods | 195 // Everything below this is copypasta from ArgvInput private methods |
196 // @codeCoverageIgnoreStart | 196 // @codeCoverageIgnoreStart |
200 * | 200 * |
201 * @param string $token The current token | 201 * @param string $token The current token |
202 */ | 202 */ |
203 private function parseShortOption($token) | 203 private function parseShortOption($token) |
204 { | 204 { |
205 $name = substr($token, 1); | 205 $name = \substr($token, 1); |
206 | 206 |
207 if (strlen($name) > 1) { | 207 if (\strlen($name) > 1) { |
208 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { | 208 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { |
209 // an option with a value (with no space) | 209 // an option with a value (with no space) |
210 $this->addShortOption($name[0], substr($name, 1)); | 210 $this->addShortOption($name[0], \substr($name, 1)); |
211 } else { | 211 } else { |
212 $this->parseShortOptionSet($name); | 212 $this->parseShortOptionSet($name); |
213 } | 213 } |
214 } else { | 214 } else { |
215 $this->addShortOption($name, null); | 215 $this->addShortOption($name, null); |
223 * | 223 * |
224 * @throws \RuntimeException When option given doesn't exist | 224 * @throws \RuntimeException When option given doesn't exist |
225 */ | 225 */ |
226 private function parseShortOptionSet($name) | 226 private function parseShortOptionSet($name) |
227 { | 227 { |
228 $len = strlen($name); | 228 $len = \strlen($name); |
229 for ($i = 0; $i < $len; $i++) { | 229 for ($i = 0; $i < $len; $i++) { |
230 if (!$this->definition->hasShortcut($name[$i])) { | 230 if (!$this->definition->hasShortcut($name[$i])) { |
231 throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i])); | 231 throw new \RuntimeException(\sprintf('The "-%s" option does not exist.', $name[$i])); |
232 } | 232 } |
233 | 233 |
234 $option = $this->definition->getOptionForShortcut($name[$i]); | 234 $option = $this->definition->getOptionForShortcut($name[$i]); |
235 if ($option->acceptValue()) { | 235 if ($option->acceptValue()) { |
236 $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); | 236 $this->addLongOption($option->getName(), $i === $len - 1 ? null : \substr($name, $i + 1)); |
237 | 237 |
238 break; | 238 break; |
239 } else { | 239 } else { |
240 $this->addLongOption($option->getName(), null); | 240 $this->addLongOption($option->getName(), null); |
241 } | 241 } |
247 * | 247 * |
248 * @param string $token The current token | 248 * @param string $token The current token |
249 */ | 249 */ |
250 private function parseLongOption($token) | 250 private function parseLongOption($token) |
251 { | 251 { |
252 $name = substr($token, 2); | 252 $name = \substr($token, 2); |
253 | 253 |
254 if (false !== $pos = strpos($name, '=')) { | 254 if (false !== $pos = \strpos($name, '=')) { |
255 if (0 === strlen($value = substr($name, $pos + 1))) { | 255 if (0 === \strlen($value = \substr($name, $pos + 1))) { |
256 // if no value after "=" then substr() returns "" since php7 only, false before | 256 // if no value after "=" then substr() returns "" since php7 only, false before |
257 // see http://php.net/manual/fr/migration70.incompatible.php#119151 | 257 // see http://php.net/manual/fr/migration70.incompatible.php#119151 |
258 if (PHP_VERSION_ID < 70000 && false === $value) { | 258 if (PHP_VERSION_ID < 70000 && false === $value) { |
259 $value = ''; | 259 $value = ''; |
260 } | 260 } |
261 array_unshift($this->parsed, [$value, null]); | 261 \array_unshift($this->parsed, [$value, null]); |
262 } | 262 } |
263 $this->addLongOption(substr($name, 0, $pos), $value); | 263 $this->addLongOption(\substr($name, 0, $pos), $value); |
264 } else { | 264 } else { |
265 $this->addLongOption($name, null); | 265 $this->addLongOption($name, null); |
266 } | 266 } |
267 } | 267 } |
268 | 268 |
275 * @throws \RuntimeException When option given doesn't exist | 275 * @throws \RuntimeException When option given doesn't exist |
276 */ | 276 */ |
277 private function addShortOption($shortcut, $value) | 277 private function addShortOption($shortcut, $value) |
278 { | 278 { |
279 if (!$this->definition->hasShortcut($shortcut)) { | 279 if (!$this->definition->hasShortcut($shortcut)) { |
280 throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); | 280 throw new \RuntimeException(\sprintf('The "-%s" option does not exist.', $shortcut)); |
281 } | 281 } |
282 | 282 |
283 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); | 283 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
284 } | 284 } |
285 | 285 |
292 * @throws \RuntimeException When option given doesn't exist | 292 * @throws \RuntimeException When option given doesn't exist |
293 */ | 293 */ |
294 private function addLongOption($name, $value) | 294 private function addLongOption($name, $value) |
295 { | 295 { |
296 if (!$this->definition->hasOption($name)) { | 296 if (!$this->definition->hasOption($name)) { |
297 throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name)); | 297 throw new \RuntimeException(\sprintf('The "--%s" option does not exist.', $name)); |
298 } | 298 } |
299 | 299 |
300 $option = $this->definition->getOption($name); | 300 $option = $this->definition->getOption($name); |
301 | 301 |
302 if (null !== $value && !$option->acceptValue()) { | 302 if (null !== $value && !$option->acceptValue()) { |
303 throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); | 303 throw new \RuntimeException(\sprintf('The "--%s" option does not accept a value.', $name)); |
304 } | 304 } |
305 | 305 |
306 if (in_array($value, ['', null], true) && $option->acceptValue() && count($this->parsed)) { | 306 if (\in_array($value, ['', null], true) && $option->acceptValue() && \count($this->parsed)) { |
307 // if option accepts an optional or mandatory argument | 307 // if option accepts an optional or mandatory argument |
308 // let's see if there is one provided | 308 // let's see if there is one provided |
309 $next = array_shift($this->parsed); | 309 $next = \array_shift($this->parsed); |
310 $nextToken = $next[0]; | 310 $nextToken = $next[0]; |
311 if ((isset($nextToken[0]) && '-' !== $nextToken[0]) || in_array($nextToken, ['', null], true)) { | 311 if ((isset($nextToken[0]) && '-' !== $nextToken[0]) || \in_array($nextToken, ['', null], true)) { |
312 $value = $nextToken; | 312 $value = $nextToken; |
313 } else { | 313 } else { |
314 array_unshift($this->parsed, $next); | 314 \array_unshift($this->parsed, $next); |
315 } | 315 } |
316 } | 316 } |
317 | 317 |
318 if (null === $value) { | 318 if (null === $value) { |
319 if ($option->isValueRequired()) { | 319 if ($option->isValueRequired()) { |
320 throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name)); | 320 throw new \RuntimeException(\sprintf('The "--%s" option requires a value.', $name)); |
321 } | 321 } |
322 | 322 |
323 if (!$option->isArray() && !$option->isValueOptional()) { | 323 if (!$option->isArray() && !$option->isValueOptional()) { |
324 $value = true; | 324 $value = true; |
325 } | 325 } |