annotate vendor/psy/psysh/src/Psy/Input/ShellInput.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
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-2017 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@12 53 $args = $definition->getArguments();
Chris@0 54 $lastArg = array_pop($args);
Chris@0 55 foreach ($args as $arg) {
Chris@0 56 if ($arg instanceof CodeArgument) {
Chris@0 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 = array();
Chris@0 87 $length = strlen($input);
Chris@0 88 $cursor = 0;
Chris@0 89 while ($cursor < $length) {
Chris@0 90 if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
Chris@0 91 } elseif (preg_match('/([^="\'\s]+?)(=?)(' . StringInput::REGEX_QUOTED_STRING . '+)/A', $input, $match, null, $cursor)) {
Chris@0 92 $tokens[] = array(
Chris@0 93 $match[1] . $match[2] . stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2))),
Chris@0 94 substr($input, $cursor),
Chris@0 95 );
Chris@0 96 } elseif (preg_match('/' . StringInput::REGEX_QUOTED_STRING . '/A', $input, $match, null, $cursor)) {
Chris@0 97 $tokens[] = array(
Chris@0 98 stripcslashes(substr($match[0], 1, strlen($match[0]) - 2)),
Chris@0 99 substr($input, $cursor),
Chris@0 100 );
Chris@0 101 } elseif (preg_match('/' . StringInput::REGEX_STRING . '/A', $input, $match, null, $cursor)) {
Chris@0 102 $tokens[] = array(
Chris@0 103 stripcslashes($match[1]),
Chris@0 104 substr($input, $cursor),
Chris@0 105 );
Chris@0 106 } else {
Chris@0 107 // should never happen
Chris@0 108 throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
Chris@0 109 }
Chris@0 110
Chris@0 111 $cursor += strlen($match[0]);
Chris@0 112 }
Chris@0 113
Chris@0 114 return $tokens;
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * Same as parent, but with some bonus handling for code arguments.
Chris@0 119 */
Chris@0 120 protected function parse()
Chris@0 121 {
Chris@0 122 $parseOptions = true;
Chris@0 123 $this->parsed = $this->tokenPairs;
Chris@0 124 while (null !== $tokenPair = array_shift($this->parsed)) {
Chris@0 125 // token is what you'd expect. rest is the remainder of the input
Chris@0 126 // string, including token, and will be used if this is a code arg.
Chris@0 127 list($token, $rest) = $tokenPair;
Chris@0 128
Chris@0 129 if ($parseOptions && '' === $token) {
Chris@0 130 $this->parseShellArgument($token, $rest);
Chris@0 131 } elseif ($parseOptions && '--' === $token) {
Chris@0 132 $parseOptions = false;
Chris@0 133 } elseif ($parseOptions && 0 === strpos($token, '--')) {
Chris@0 134 $this->parseLongOption($token);
Chris@0 135 } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
Chris@0 136 $this->parseShortOption($token);
Chris@0 137 } else {
Chris@0 138 $this->parseShellArgument($token, $rest);
Chris@0 139 }
Chris@0 140 }
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Parses an argument, with bonus handling for code arguments.
Chris@0 145 *
Chris@0 146 * @param string $token The current token
Chris@0 147 * @param string $rest The remaining unparsed input, including the current token
Chris@0 148 *
Chris@0 149 * @throws \RuntimeException When too many arguments are given
Chris@0 150 */
Chris@0 151 private function parseShellArgument($token, $rest)
Chris@0 152 {
Chris@0 153 $c = count($this->arguments);
Chris@0 154
Chris@0 155 // if input is expecting another argument, add it
Chris@0 156 if ($this->definition->hasArgument($c)) {
Chris@0 157 $arg = $this->definition->getArgument($c);
Chris@0 158
Chris@0 159 if ($arg instanceof CodeArgument) {
Chris@0 160 // When we find a code argument, we're done parsing. Add the
Chris@0 161 // remaining input to the current argument and call it a day.
Chris@0 162 $this->parsed = array();
Chris@0 163 $this->arguments[$arg->getName()] = $rest;
Chris@0 164 } else {
Chris@0 165 $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
Chris@0 166 }
Chris@0 167
Chris@0 168 return;
Chris@0 169 }
Chris@0 170
Chris@0 171 // if last argument isArray(), append token to last argument
Chris@0 172 if ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
Chris@0 173 $arg = $this->definition->getArgument($c - 1);
Chris@0 174 $this->arguments[$arg->getName()][] = $token;
Chris@0 175
Chris@0 176 return;
Chris@0 177 }
Chris@0 178
Chris@0 179 // unexpected argument
Chris@0 180 $all = $this->definition->getArguments();
Chris@0 181 if (count($all)) {
Chris@0 182 throw new \RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))));
Chris@0 183 }
Chris@0 184
Chris@0 185 throw new \RuntimeException(sprintf('No arguments expected, got "%s".', $token));
Chris@0 186 }
Chris@0 187
Chris@0 188 // Everything below this is copypasta from ArgvInput private methods
Chris@0 189
Chris@0 190 /**
Chris@0 191 * Parses a short option.
Chris@0 192 *
Chris@0 193 * @param string $token The current token
Chris@0 194 */
Chris@0 195 private function parseShortOption($token)
Chris@0 196 {
Chris@0 197 $name = substr($token, 1);
Chris@0 198
Chris@0 199 if (strlen($name) > 1) {
Chris@0 200 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
Chris@0 201 // an option with a value (with no space)
Chris@0 202 $this->addShortOption($name[0], substr($name, 1));
Chris@0 203 } else {
Chris@0 204 $this->parseShortOptionSet($name);
Chris@0 205 }
Chris@0 206 } else {
Chris@0 207 $this->addShortOption($name, null);
Chris@0 208 }
Chris@0 209 }
Chris@0 210
Chris@0 211 /**
Chris@0 212 * Parses a short option set.
Chris@0 213 *
Chris@0 214 * @param string $name The current token
Chris@0 215 *
Chris@0 216 * @throws \RuntimeException When option given doesn't exist
Chris@0 217 */
Chris@0 218 private function parseShortOptionSet($name)
Chris@0 219 {
Chris@0 220 $len = strlen($name);
Chris@12 221 for ($i = 0; $i < $len; $i++) {
Chris@0 222 if (!$this->definition->hasShortcut($name[$i])) {
Chris@0 223 throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
Chris@0 224 }
Chris@0 225
Chris@0 226 $option = $this->definition->getOptionForShortcut($name[$i]);
Chris@0 227 if ($option->acceptValue()) {
Chris@0 228 $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
Chris@0 229
Chris@0 230 break;
Chris@0 231 } else {
Chris@0 232 $this->addLongOption($option->getName(), null);
Chris@0 233 }
Chris@0 234 }
Chris@0 235 }
Chris@0 236
Chris@0 237 /**
Chris@0 238 * Parses a long option.
Chris@0 239 *
Chris@0 240 * @param string $token The current token
Chris@0 241 */
Chris@0 242 private function parseLongOption($token)
Chris@0 243 {
Chris@0 244 $name = substr($token, 2);
Chris@0 245
Chris@0 246 if (false !== $pos = strpos($name, '=')) {
Chris@0 247 if (0 === strlen($value = substr($name, $pos + 1))) {
Chris@0 248 // if no value after "=" then substr() returns "" since php7 only, false before
Chris@0 249 // see http://php.net/manual/fr/migration70.incompatible.php#119151
Chris@0 250 if (PHP_VERSION_ID < 70000 && false === $value) {
Chris@0 251 $value = '';
Chris@0 252 }
Chris@0 253 array_unshift($this->parsed, array($value, null));
Chris@0 254 }
Chris@0 255 $this->addLongOption(substr($name, 0, $pos), $value);
Chris@0 256 } else {
Chris@0 257 $this->addLongOption($name, null);
Chris@0 258 }
Chris@0 259 }
Chris@0 260
Chris@0 261 /**
Chris@0 262 * Adds a short option value.
Chris@0 263 *
Chris@0 264 * @param string $shortcut The short option key
Chris@0 265 * @param mixed $value The value for the option
Chris@0 266 *
Chris@0 267 * @throws \RuntimeException When option given doesn't exist
Chris@0 268 */
Chris@0 269 private function addShortOption($shortcut, $value)
Chris@0 270 {
Chris@0 271 if (!$this->definition->hasShortcut($shortcut)) {
Chris@0 272 throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
Chris@0 273 }
Chris@0 274
Chris@0 275 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
Chris@0 276 }
Chris@0 277
Chris@0 278 /**
Chris@0 279 * Adds a long option value.
Chris@0 280 *
Chris@0 281 * @param string $name The long option key
Chris@0 282 * @param mixed $value The value for the option
Chris@0 283 *
Chris@0 284 * @throws \RuntimeException When option given doesn't exist
Chris@0 285 */
Chris@0 286 private function addLongOption($name, $value)
Chris@0 287 {
Chris@0 288 if (!$this->definition->hasOption($name)) {
Chris@0 289 throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
Chris@0 290 }
Chris@0 291
Chris@0 292 $option = $this->definition->getOption($name);
Chris@0 293
Chris@0 294 if (null !== $value && !$option->acceptValue()) {
Chris@0 295 throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
Chris@0 296 }
Chris@0 297
Chris@0 298 if (in_array($value, array('', null), true) && $option->acceptValue() && count($this->parsed)) {
Chris@0 299 // if option accepts an optional or mandatory argument
Chris@0 300 // let's see if there is one provided
Chris@0 301 $next = array_shift($this->parsed);
Chris@0 302 $nextToken = $next[0];
Chris@0 303 if ((isset($nextToken[0]) && '-' !== $nextToken[0]) || in_array($nextToken, array('', null), true)) {
Chris@0 304 $value = $nextToken;
Chris@0 305 } else {
Chris@0 306 array_unshift($this->parsed, $next);
Chris@0 307 }
Chris@0 308 }
Chris@0 309
Chris@0 310 if (null === $value) {
Chris@0 311 if ($option->isValueRequired()) {
Chris@0 312 throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
Chris@0 313 }
Chris@0 314
Chris@0 315 if (!$option->isArray() && !$option->isValueOptional()) {
Chris@0 316 $value = true;
Chris@0 317 }
Chris@0 318 }
Chris@0 319
Chris@0 320 if ($option->isArray()) {
Chris@0 321 $this->options[$name][] = $value;
Chris@0 322 } else {
Chris@0 323 $this->options[$name] = $value;
Chris@0 324 }
Chris@0 325 }
Chris@0 326 }