You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
5.6 KiB

3 years ago
  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. $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 (0 === strpos($key, '--')) {
  116. $this->addLongOption(substr($key, 2), $value);
  117. } elseif (0 === strpos($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. if (!$this->definition->hasNegation($name)) {
  146. throw new InvalidOptionException(sprintf('The "--%s" option does not exist.', $name));
  147. }
  148. $optionName = $this->definition->negationToName($name);
  149. $this->options[$optionName] = false;
  150. return;
  151. }
  152. $option = $this->definition->getOption($name);
  153. if (null === $value) {
  154. if ($option->isValueRequired()) {
  155. throw new InvalidOptionException(sprintf('The "--%s" option requires a value.', $name));
  156. }
  157. if (!$option->isValueOptional()) {
  158. $value = true;
  159. }
  160. }
  161. $this->options[$name] = $value;
  162. }
  163. /**
  164. * Adds an argument value.
  165. *
  166. * @param string|int $name The argument name
  167. * @param mixed $value The value for the argument
  168. *
  169. * @throws InvalidArgumentException When argument given doesn't exist
  170. */
  171. private function addArgument($name, $value)
  172. {
  173. if (!$this->definition->hasArgument($name)) {
  174. throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
  175. }
  176. $this->arguments[$name] = $value;
  177. }
  178. }