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