Brak opisu

ArrayInput.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, $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, $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. $glue = ('-' === $param[1]) ? '=' : ' ';
  93. if (\is_array($val)) {
  94. foreach ($val as $v) {
  95. $params[] = $param.('' != $v ? $glue.$this->escapeToken($v) : '');
  96. }
  97. } else {
  98. $params[] = $param.('' != $val ? $glue.$this->escapeToken($val) : '');
  99. }
  100. } else {
  101. $params[] = \is_array($val) ? implode(' ', array_map([$this, 'escapeToken'], $val)) : $this->escapeToken($val);
  102. }
  103. }
  104. return implode(' ', $params);
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. protected function parse()
  110. {
  111. foreach ($this->parameters as $key => $value) {
  112. if ('--' === $key) {
  113. return;
  114. }
  115. if (str_starts_with($key, '--')) {
  116. $this->addLongOption(substr($key, 2), $value);
  117. } elseif (str_starts_with($key, '-')) {
  118. $this->addShortOption(substr($key, 1), $value);
  119. } else {
  120. $this->addArgument($key, $value);
  121. }
  122. }
  123. }
  124. /**
  125. * Adds a short option value.
  126. *
  127. * @throws InvalidOptionException When option given doesn't exist
  128. */
  129. private function addShortOption(string $shortcut, $value)
  130. {
  131. if (!$this->definition->hasShortcut($shortcut)) {
  132. throw new InvalidOptionException(sprintf('The "-%s" option does not exist.', $shortcut));
  133. }
  134. $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
  135. }
  136. /**
  137. * Adds a long option value.
  138. *
  139. * @throws InvalidOptionException When option given doesn't exist
  140. * @throws InvalidOptionException When a required value is missing
  141. */
  142. private function addLongOption(string $name, $value)
  143. {
  144. if (!$this->definition->hasOption($name)) {
  145. throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
  146. }
  147. $option = $this->definition->getOption($name);
  148. if (null === $value) {
  149. if ($option->isValueRequired()) {
  150. throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
  151. }
  152. if (!$option->isValueOptional()) {
  153. $value = true;
  154. }
  155. }
  156. $this->options[$name] = $value;
  157. }
  158. /**
  159. * Adds an argument value.
  160. *
  161. * @param string|int $name The argument name
  162. * @param mixed $value The value for the argument
  163. *
  164. * @throws InvalidArgumentException When argument given doesn't exist
  165. */
  166. private function addArgument($name, $value)
  167. {
  168. if (!$this->definition->hasArgument($name)) {
  169. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  170. }
  171. $this->arguments[$name] = $value;
  172. }
  173. }