ArrayInput.php 5.5KB

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