ArrayInput.php 5.5 KB

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