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