/application/libraries/Doctrine/Symfony/Component/Console/Input/ArgvInput.php

https://github.com/nicnocquee/PPION-Website · PHP · 255 lines · 124 code · 30 blank · 101 comment · 28 complexity · 3e757216f6b19f83db48e3a86dfef3bc MD5 · raw file

  1. <?php
  2. namespace Symfony\Component\Console\Input;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * ArgvInput represents an input coming from the CLI arguments.
  13. *
  14. * Usage:
  15. *
  16. * $input = new ArgvInput();
  17. *
  18. * By default, the `$_SERVER['argv']` array is used for the input values.
  19. *
  20. * This can be overridden by explicitly passing the input values in the constructor:
  21. *
  22. * $input = new ArgvInput($_SERVER['argv']);
  23. *
  24. * If you pass it yourself, don't forget that the first element of the array
  25. * is the name of the running program.
  26. *
  27. * When passing an argument to the constructor, be sure that it respects
  28. * the same rules as the argv one. It's almost always better to use the
  29. * `StringInput` when you want to provide your own input.
  30. *
  31. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  32. *
  33. * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
  34. * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
  35. */
  36. class ArgvInput extends Input
  37. {
  38. protected $tokens;
  39. protected $parsed;
  40. /**
  41. * Constructor.
  42. *
  43. * @param array $argv An array of parameters from the CLI (in the argv format)
  44. * @param InputDefinition $definition A InputDefinition instance
  45. */
  46. public function __construct(array $argv = null, InputDefinition $definition = null)
  47. {
  48. if (null === $argv) {
  49. $argv = $_SERVER['argv'];
  50. }
  51. // strip the program name
  52. array_shift($argv);
  53. $this->tokens = $argv;
  54. parent::__construct($definition);
  55. }
  56. /**
  57. * Processes command line arguments.
  58. */
  59. protected function parse()
  60. {
  61. $this->parsed = $this->tokens;
  62. while (null !== $token = array_shift($this->parsed)) {
  63. if ('--' === substr($token, 0, 2)) {
  64. $this->parseLongOption($token);
  65. } elseif ('-' === $token[0]) {
  66. $this->parseShortOption($token);
  67. } else {
  68. $this->parseArgument($token);
  69. }
  70. }
  71. }
  72. /**
  73. * Parses a short option.
  74. *
  75. * @param string $token The current token.
  76. */
  77. protected function parseShortOption($token)
  78. {
  79. $name = substr($token, 1);
  80. if (strlen($name) > 1) {
  81. if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
  82. // an option with a value (with no space)
  83. $this->addShortOption($name[0], substr($name, 1));
  84. } else {
  85. $this->parseShortOptionSet($name);
  86. }
  87. } else {
  88. $this->addShortOption($name, null);
  89. }
  90. }
  91. /**
  92. * Parses a short option set.
  93. *
  94. * @param string $token The current token
  95. *
  96. * @throws \RuntimeException When option given doesn't exist
  97. */
  98. protected function parseShortOptionSet($name)
  99. {
  100. $len = strlen($name);
  101. for ($i = 0; $i < $len; $i++) {
  102. if (!$this->definition->hasShortcut($name[$i])) {
  103. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
  104. }
  105. $option = $this->definition->getOptionForShortcut($name[$i]);
  106. if ($option->acceptValue()) {
  107. $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
  108. break;
  109. } else {
  110. $this->addLongOption($option->getName(), true);
  111. }
  112. }
  113. }
  114. /**
  115. * Parses a long option.
  116. *
  117. * @param string $token The current token
  118. */
  119. protected function parseLongOption($token)
  120. {
  121. $name = substr($token, 2);
  122. if (false !== $pos = strpos($name, '=')) {
  123. $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
  124. } else {
  125. $this->addLongOption($name, null);
  126. }
  127. }
  128. /**
  129. * Parses an argument.
  130. *
  131. * @param string $token The current token
  132. *
  133. * @throws \RuntimeException When too many arguments are given
  134. */
  135. protected function parseArgument($token)
  136. {
  137. if (!$this->definition->hasArgument(count($this->arguments))) {
  138. throw new \RuntimeException('Too many arguments.');
  139. }
  140. $this->arguments[$this->definition->getArgument(count($this->arguments))->getName()] = $token;
  141. }
  142. /**
  143. * Adds a short option value.
  144. *
  145. * @param string $shortcut The short option key
  146. * @param mixed $value The value for the option
  147. *
  148. * @throws \RuntimeException When option given doesn't exist
  149. */
  150. protected function addShortOption($shortcut, $value)
  151. {
  152. if (!$this->definition->hasShortcut($shortcut)) {
  153. throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
  154. }
  155. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  156. }
  157. /**
  158. * Adds a long option value.
  159. *
  160. * @param string $name The long option key
  161. * @param mixed $value The value for the option
  162. *
  163. * @throws \RuntimeException When option given doesn't exist
  164. */
  165. protected function addLongOption($name, $value)
  166. {
  167. if (!$this->definition->hasOption($name)) {
  168. throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
  169. }
  170. $option = $this->definition->getOption($name);
  171. if (null === $value && $option->acceptValue()) {
  172. // if option accepts an optional or mandatory argument
  173. // let's see if there is one provided
  174. $next = array_shift($this->parsed);
  175. if ('-' !== $next[0]) {
  176. $value = $next;
  177. } else {
  178. array_unshift($this->parsed, $next);
  179. }
  180. }
  181. if (null === $value) {
  182. if ($option->isValueRequired()) {
  183. throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
  184. }
  185. $value = $option->isValueOptional() ? $option->getDefault() : true;
  186. }
  187. $this->options[$name] = $value;
  188. }
  189. /**
  190. * Returns the first argument from the raw parameters (not parsed).
  191. *
  192. * @return string The value of the first argument or null otherwise
  193. */
  194. public function getFirstArgument()
  195. {
  196. foreach ($this->tokens as $token) {
  197. if ($token && '-' === $token[0]) {
  198. continue;
  199. }
  200. return $token;
  201. }
  202. }
  203. /**
  204. * Returns true if the raw parameters (not parsed) contains a value.
  205. *
  206. * This method is to be used to introspect the input parameters
  207. * before it has been validated. It must be used carefully.
  208. *
  209. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  210. *
  211. * @return Boolean true if the value is contained in the raw parameters
  212. */
  213. public function hasParameterOption($values)
  214. {
  215. if (!is_array($values)) {
  216. $values = array($values);
  217. }
  218. foreach ($this->tokens as $v) {
  219. if (in_array($v, $values)) {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. }