annotate vendor/psy/psysh/src/Input/ShellInput.php @ 5:12f9dff5fda9 tip

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