PageRenderTime 276ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel_tintuc/vendor/psy/psysh/src/Input/ShellInput.php

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