PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/vendor/symfony/console/Input/ArgvInput.php

https://gitlab.com/I-NOZex/quiz
PHP | 351 lines | 185 code | 45 blank | 121 comment | 52 complexity | 55bf98d66625e02c45ee7356af552262 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Console\Input;
  11. use Symfony\Component\Console\Exception\RuntimeException;
  12. /**
  13. * ArgvInput represents an input coming from the CLI arguments.
  14. *
  15. * Usage:
  16. *
  17. * $input = new ArgvInput();
  18. *
  19. * By default, the `$_SERVER['argv']` array is used for the input values.
  20. *
  21. * This can be overridden by explicitly passing the input values in the constructor:
  22. *
  23. * $input = new ArgvInput($_SERVER['argv']);
  24. *
  25. * If you pass it yourself, don't forget that the first element of the array
  26. * is the name of the running application.
  27. *
  28. * When passing an argument to the constructor, be sure that it respects
  29. * the same rules as the argv one. It's almost always better to use the
  30. * `StringInput` when you want to provide your own input.
  31. *
  32. * @author Fabien Potencier <fabien@symfony.com>
  33. *
  34. * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
  35. * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
  36. */
  37. class ArgvInput extends Input
  38. {
  39. private $tokens;
  40. private $parsed;
  41. /**
  42. * Constructor.
  43. *
  44. * @param array $argv An array of parameters from the CLI (in the argv format)
  45. * @param InputDefinition $definition A InputDefinition instance
  46. */
  47. public function __construct(array $argv = null, InputDefinition $definition = null)
  48. {
  49. if (null === $argv) {
  50. $argv = $_SERVER['argv'];
  51. }
  52. // strip the application name
  53. array_shift($argv);
  54. $this->tokens = $argv;
  55. parent::__construct($definition);
  56. }
  57. protected function setTokens(array $tokens)
  58. {
  59. $this->tokens = $tokens;
  60. }
  61. /**
  62. * Processes command line arguments.
  63. */
  64. protected function parse()
  65. {
  66. $parseOptions = true;
  67. $this->parsed = $this->tokens;
  68. while (null !== $token = array_shift($this->parsed)) {
  69. if ($parseOptions && '' == $token) {
  70. $this->parseArgument($token);
  71. } elseif ($parseOptions && '--' == $token) {
  72. $parseOptions = false;
  73. } elseif ($parseOptions && 0 === strpos($token, '--')) {
  74. $this->parseLongOption($token);
  75. } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
  76. $this->parseShortOption($token);
  77. } else {
  78. $this->parseArgument($token);
  79. }
  80. }
  81. }
  82. /**
  83. * Parses a short option.
  84. *
  85. * @param string $token The current token.
  86. */
  87. private function parseShortOption($token)
  88. {
  89. $name = substr($token, 1);
  90. if (strlen($name) > 1) {
  91. if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
  92. // an option with a value (with no space)
  93. $this->addShortOption($name[0], substr($name, 1));
  94. } else {
  95. $this->parseShortOptionSet($name);
  96. }
  97. } else {
  98. $this->addShortOption($name, null);
  99. }
  100. }
  101. /**
  102. * Parses a short option set.
  103. *
  104. * @param string $name The current token
  105. *
  106. * @throws RuntimeException When option given doesn't exist
  107. */
  108. private function parseShortOptionSet($name)
  109. {
  110. $len = strlen($name);
  111. for ($i = 0; $i < $len; ++$i) {
  112. if (!$this->definition->hasShortcut($name[$i])) {
  113. throw new RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
  114. }
  115. $option = $this->definition->getOptionForShortcut($name[$i]);
  116. if ($option->acceptValue()) {
  117. $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
  118. break;
  119. } else {
  120. $this->addLongOption($option->getName(), null);
  121. }
  122. }
  123. }
  124. /**
  125. * Parses a long option.
  126. *
  127. * @param string $token The current token
  128. */
  129. private function parseLongOption($token)
  130. {
  131. $name = substr($token, 2);
  132. if (false !== $pos = strpos($name, '=')) {
  133. $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
  134. } else {
  135. $this->addLongOption($name, null);
  136. }
  137. }
  138. /**
  139. * Parses an argument.
  140. *
  141. * @param string $token The current token
  142. *
  143. * @throws RuntimeException When too many arguments are given
  144. */
  145. private function parseArgument($token)
  146. {
  147. $c = count($this->arguments);
  148. // if input is expecting another argument, add it
  149. if ($this->definition->hasArgument($c)) {
  150. $arg = $this->definition->getArgument($c);
  151. $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
  152. // if last argument isArray(), append token to last argument
  153. } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
  154. $arg = $this->definition->getArgument($c - 1);
  155. $this->arguments[$arg->getName()][] = $token;
  156. // unexpected argument
  157. } else {
  158. throw new RuntimeException('Too many arguments.');
  159. }
  160. }
  161. /**
  162. * Adds a short option value.
  163. *
  164. * @param string $shortcut The short option key
  165. * @param mixed $value The value for the option
  166. *
  167. * @throws RuntimeException When option given doesn't exist
  168. */
  169. private function addShortOption($shortcut, $value)
  170. {
  171. if (!$this->definition->hasShortcut($shortcut)) {
  172. throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
  173. }
  174. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  175. }
  176. /**
  177. * Adds a long option value.
  178. *
  179. * @param string $name The long option key
  180. * @param mixed $value The value for the option
  181. *
  182. * @throws RuntimeException When option given doesn't exist
  183. */
  184. private function addLongOption($name, $value)
  185. {
  186. if (!$this->definition->hasOption($name)) {
  187. throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name));
  188. }
  189. $option = $this->definition->getOption($name);
  190. // Convert empty values to null
  191. if (!isset($value[0])) {
  192. $value = null;
  193. }
  194. if (null !== $value && !$option->acceptValue()) {
  195. throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name));
  196. }
  197. if (null === $value && $option->acceptValue() && count($this->parsed)) {
  198. // if option accepts an optional or mandatory argument
  199. // let's see if there is one provided
  200. $next = array_shift($this->parsed);
  201. if (isset($next[0]) && '-' !== $next[0]) {
  202. $value = $next;
  203. } elseif (empty($next)) {
  204. $value = '';
  205. } else {
  206. array_unshift($this->parsed, $next);
  207. }
  208. }
  209. if (null === $value) {
  210. if ($option->isValueRequired()) {
  211. throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name));
  212. }
  213. if (!$option->isArray()) {
  214. $value = $option->isValueOptional() ? $option->getDefault() : true;
  215. }
  216. }
  217. if ($option->isArray()) {
  218. $this->options[$name][] = $value;
  219. } else {
  220. $this->options[$name] = $value;
  221. }
  222. }
  223. /**
  224. * Returns the first argument from the raw parameters (not parsed).
  225. *
  226. * @return string The value of the first argument or null otherwise
  227. */
  228. public function getFirstArgument()
  229. {
  230. foreach ($this->tokens as $token) {
  231. if ($token && '-' === $token[0]) {
  232. continue;
  233. }
  234. return $token;
  235. }
  236. }
  237. /**
  238. * Returns true if the raw parameters (not parsed) contain a value.
  239. *
  240. * This method is to be used to introspect the input parameters
  241. * before they have been validated. It must be used carefully.
  242. *
  243. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  244. *
  245. * @return bool true if the value is contained in the raw parameters
  246. */
  247. public function hasParameterOption($values)
  248. {
  249. $values = (array) $values;
  250. foreach ($this->tokens as $token) {
  251. foreach ($values as $value) {
  252. if ($token === $value || 0 === strpos($token, $value.'=')) {
  253. return true;
  254. }
  255. }
  256. }
  257. return false;
  258. }
  259. /**
  260. * Returns the value of a raw option (not parsed).
  261. *
  262. * This method is to be used to introspect the input parameters
  263. * before they have been validated. It must be used carefully.
  264. *
  265. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  266. * @param mixed $default The default value to return if no result is found
  267. *
  268. * @return mixed The option value
  269. */
  270. public function getParameterOption($values, $default = false)
  271. {
  272. $values = (array) $values;
  273. $tokens = $this->tokens;
  274. while (0 < count($tokens)) {
  275. $token = array_shift($tokens);
  276. foreach ($values as $value) {
  277. if ($token === $value || 0 === strpos($token, $value.'=')) {
  278. if (false !== $pos = strpos($token, '=')) {
  279. return substr($token, $pos + 1);
  280. }
  281. return array_shift($tokens);
  282. }
  283. }
  284. }
  285. return $default;
  286. }
  287. /**
  288. * Returns a stringified representation of the args passed to the command.
  289. *
  290. * @return string
  291. */
  292. public function __toString()
  293. {
  294. $self = $this;
  295. $tokens = array_map(function ($token) use ($self) {
  296. if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
  297. return $match[1].$self->escapeToken($match[2]);
  298. }
  299. if ($token && $token[0] !== '-') {
  300. return $self->escapeToken($token);
  301. }
  302. return $token;
  303. }, $this->tokens);
  304. return implode(' ', $tokens);
  305. }
  306. }