ArrayInput.php 6.0 KB

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