ArrayInput.php 5.6 KB

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