annotate vendor/psy/psysh/src/Input/ShellInput.php @ 13:5fb285c0d0e3

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