ArrayInput.php 5.4 KB

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