ArrayInput.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /**
  12. * ArrayInput represents an input provided as an array.
  13. *
  14. * Usage:
  15. *
  16. * $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @api
  21. */
  22. class ArrayInput extends Input
  23. {
  24. private $parameters;
  25. /**
  26. * Constructor.
  27. *
  28. * @param array $parameters An array of parameters
  29. * @param InputDefinition $definition A InputDefinition instance
  30. *
  31. * @api
  32. */
  33. public function __construct(array $parameters, InputDefinition $definition = null)
  34. {
  35. $this->parameters = $parameters;
  36. parent::__construct($definition);
  37. }
  38. /**
  39. * Returns the first argument from the raw parameters (not parsed).
  40. *
  41. * @return string The value of the first argument or null otherwise
  42. */
  43. public function getFirstArgument()
  44. {
  45. foreach ($this->parameters as $key => $value) {
  46. if ($key && '-' === $key[0]) {
  47. continue;
  48. }
  49. return $value;
  50. }
  51. }
  52. /**
  53. * Returns true if the raw parameters (not parsed) contain a value.
  54. *
  55. * This method is to be used to introspect the input parameters
  56. * before they have been validated. It must be used carefully.
  57. *
  58. * @param string|array $values The values to look for in the raw parameters (can be an array)
  59. *
  60. * @return bool true if the value is contained in the raw parameters
  61. */
  62. public function hasParameterOption($values)
  63. {
  64. $values = (array) $values;
  65. foreach ($this->parameters as $k => $v) {
  66. if (!is_int($k)) {
  67. $v = $k;
  68. }
  69. if (in_array($v, $values)) {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. /**
  76. * Returns the value of a raw option (not parsed).
  77. *
  78. * This method is to be used to introspect the input parameters
  79. * before they have been validated. It must be used carefully.
  80. *
  81. * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
  82. * @param mixed $default The default value to return if no result is found
  83. *
  84. * @return mixed The option value
  85. */
  86. public function getParameterOption($values, $default = false)
  87. {
  88. $values = (array) $values;
  89. foreach ($this->parameters as $k => $v) {
  90. if (is_int($k)) {
  91. if (in_array($v, $values)) {
  92. return true;
  93. }
  94. } elseif (in_array($k, $values)) {
  95. return $v;
  96. }
  97. }
  98. return $default;
  99. }
  100. /**
  101. * Returns a stringified representation of the args passed to the command.
  102. *
  103. * @return string
  104. */
  105. public function __toString()
  106. {
  107. $params = array();
  108. foreach ($this->parameters as $param => $val) {
  109. if ($param && '-' === $param[0]) {
  110. $params[] = $param.('' != $val ? '='.$this->escapeToken($val) : '');
  111. } else {
  112. $params[] = $this->escapeToken($val);
  113. }
  114. }
  115. return implode(' ', $params);
  116. }
  117. /**
  118. * Processes command line arguments.
  119. */
  120. protected function parse()
  121. {
  122. foreach ($this->parameters as $key => $value) {
  123. if (0 === strpos($key, '--')) {
  124. $this->addLongOption(substr($key, 2), $value);
  125. } elseif ('-' === $key[0]) {
  126. $this->addShortOption(substr($key, 1), $value);
  127. } else {
  128. $this->addArgument($key, $value);
  129. }
  130. }
  131. }
  132. /**
  133. * Adds a short option value.
  134. *
  135. * @param string $shortcut The short option key
  136. * @param mixed $value The value for the option
  137. *
  138. * @throws \InvalidArgumentException When option given doesn't exist
  139. */
  140. private function addShortOption($shortcut, $value)
  141. {
  142. if (!$this->definition->hasShortcut($shortcut)) {
  143. throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
  144. }
  145. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  146. }
  147. /**
  148. * Adds a long option value.
  149. *
  150. * @param string $name The long option key
  151. * @param mixed $value The value for the option
  152. *
  153. * @throws \InvalidArgumentException When option given doesn't exist
  154. * @throws \InvalidArgumentException When a required value is missing
  155. */
  156. private function addLongOption($name, $value)
  157. {
  158. if (!$this->definition->hasOption($name)) {
  159. throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
  160. }
  161. $option = $this->definition->getOption($name);
  162. if (null === $value) {
  163. if ($option->isValueRequired()) {
  164. throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
  165. }
  166. $value = $option->isValueOptional() ? $option->getDefault() : true;
  167. }
  168. $this->options[$name] = $value;
  169. }
  170. /**
  171. * Adds an argument value.
  172. *
  173. * @param string $name The argument name
  174. * @param mixed $value The value for the argument
  175. *
  176. * @throws \InvalidArgumentException When argument given doesn't exist
  177. */
  178. private function addArgument($name, $value)
  179. {
  180. if (!$this->definition->hasArgument($name)) {
  181. throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  182. }
  183. $this->arguments[$name] = $value;
  184. }
  185. }