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
ArgvInput.php
328 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\RuntimeException; |
| 14 | /** |
| 15 | * ArgvInput represents an input coming from the CLI arguments. |
| 16 | * |
| 17 | * Usage: |
| 18 | * |
| 19 | * $input = new ArgvInput(); |
| 20 | * |
| 21 | * By default, the `$_SERVER['argv']` array is used for the input values. |
| 22 | * |
| 23 | * This can be overridden by explicitly passing the input values in the constructor: |
| 24 | * |
| 25 | * $input = new ArgvInput($_SERVER['argv']); |
| 26 | * |
| 27 | * If you pass it yourself, don't forget that the first element of the array |
| 28 | * is the name of the running application. |
| 29 | * |
| 30 | * When passing an argument to the constructor, be sure that it respects |
| 31 | * the same rules as the argv one. It's almost always better to use the |
| 32 | * `StringInput` when you want to provide your own input. |
| 33 | * |
| 34 | * @author Fabien Potencier <fabien@symfony.com> |
| 35 | * |
| 36 | * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html |
| 37 | * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02 |
| 38 | */ |
| 39 | class ArgvInput extends Input |
| 40 | { |
| 41 | private $tokens; |
| 42 | private $parsed; |
| 43 | public function __construct(?array $argv = null, ?InputDefinition $definition = null) |
| 44 | { |
| 45 | $argv = $argv ?? $_SERVER['argv'] ?? []; |
| 46 | // strip the application name |
| 47 | array_shift($argv); |
| 48 | $this->tokens = $argv; |
| 49 | parent::__construct($definition); |
| 50 | } |
| 51 | protected function setTokens(array $tokens) |
| 52 | { |
| 53 | $this->tokens = $tokens; |
| 54 | } |
| 55 | /** |
| 56 | * {@inheritdoc} |
| 57 | */ |
| 58 | protected function parse() |
| 59 | { |
| 60 | $parseOptions = \true; |
| 61 | $this->parsed = $this->tokens; |
| 62 | while (null !== ($token = array_shift($this->parsed))) { |
| 63 | $parseOptions = $this->parseToken($token, $parseOptions); |
| 64 | } |
| 65 | } |
| 66 | protected function parseToken(string $token, bool $parseOptions) : bool |
| 67 | { |
| 68 | if ($parseOptions && '' == $token) { |
| 69 | $this->parseArgument($token); |
| 70 | } elseif ($parseOptions && '--' == $token) { |
| 71 | return \false; |
| 72 | } elseif ($parseOptions && str_starts_with($token, '--')) { |
| 73 | $this->parseLongOption($token); |
| 74 | } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { |
| 75 | $this->parseShortOption($token); |
| 76 | } else { |
| 77 | $this->parseArgument($token); |
| 78 | } |
| 79 | return $parseOptions; |
| 80 | } |
| 81 | /** |
| 82 | * Parses a short option. |
| 83 | */ |
| 84 | private function parseShortOption(string $token) |
| 85 | { |
| 86 | $name = substr($token, 1); |
| 87 | if (\strlen($name) > 1) { |
| 88 | if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { |
| 89 | // an option with a value (with no space) |
| 90 | $this->addShortOption($name[0], substr($name, 1)); |
| 91 | } else { |
| 92 | $this->parseShortOptionSet($name); |
| 93 | } |
| 94 | } else { |
| 95 | $this->addShortOption($name, null); |
| 96 | } |
| 97 | } |
| 98 | /** |
| 99 | * Parses a short option set. |
| 100 | * |
| 101 | * @throws RuntimeException When option given doesn't exist |
| 102 | */ |
| 103 | private function parseShortOptionSet(string $name) |
| 104 | { |
| 105 | $len = \strlen($name); |
| 106 | for ($i = 0; $i < $len; ++$i) { |
| 107 | if (!$this->definition->hasShortcut($name[$i])) { |
| 108 | $encoding = mb_detect_encoding($name, null, \true); |
| 109 | throw new RuntimeException(sprintf('The "-%s" option does not exist.', \false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding))); |
| 110 | } |
| 111 | $option = $this->definition->getOptionForShortcut($name[$i]); |
| 112 | if ($option->acceptValue()) { |
| 113 | $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); |
| 114 | break; |
| 115 | } else { |
| 116 | $this->addLongOption($option->getName(), null); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | /** |
| 121 | * Parses a long option. |
| 122 | */ |
| 123 | private function parseLongOption(string $token) |
| 124 | { |
| 125 | $name = substr($token, 2); |
| 126 | if (\false !== ($pos = strpos($name, '='))) { |
| 127 | if ('' === ($value = substr($name, $pos + 1))) { |
| 128 | array_unshift($this->parsed, $value); |
| 129 | } |
| 130 | $this->addLongOption(substr($name, 0, $pos), $value); |
| 131 | } else { |
| 132 | $this->addLongOption($name, null); |
| 133 | } |
| 134 | } |
| 135 | /** |
| 136 | * Parses an argument. |
| 137 | * |
| 138 | * @throws RuntimeException When too many arguments are given |
| 139 | */ |
| 140 | private function parseArgument(string $token) |
| 141 | { |
| 142 | $c = \count($this->arguments); |
| 143 | // if input is expecting another argument, add it |
| 144 | if ($this->definition->hasArgument($c)) { |
| 145 | $arg = $this->definition->getArgument($c); |
| 146 | $this->arguments[$arg->getName()] = $arg->isArray() ? [$token] : $token; |
| 147 | // if last argument isArray(), append token to last argument |
| 148 | } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { |
| 149 | $arg = $this->definition->getArgument($c - 1); |
| 150 | $this->arguments[$arg->getName()][] = $token; |
| 151 | // unexpected argument |
| 152 | } else { |
| 153 | $all = $this->definition->getArguments(); |
| 154 | $symfonyCommandName = null; |
| 155 | if (($inputArgument = $all[$key = array_key_first($all)] ?? null) && 'command' === $inputArgument->getName()) { |
| 156 | $symfonyCommandName = $this->arguments['command'] ?? null; |
| 157 | unset($all[$key]); |
| 158 | } |
| 159 | if (\count($all)) { |
| 160 | if ($symfonyCommandName) { |
| 161 | $message = sprintf('Too many arguments to "%s" command, expected arguments "%s".', $symfonyCommandName, implode('" "', array_keys($all))); |
| 162 | } else { |
| 163 | $message = sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all))); |
| 164 | } |
| 165 | } elseif ($symfonyCommandName) { |
| 166 | $message = sprintf('No arguments expected for "%s" command, got "%s".', $symfonyCommandName, $token); |
| 167 | } else { |
| 168 | $message = sprintf('No arguments expected, got "%s".', $token); |
| 169 | } |
| 170 | throw new RuntimeException($message); |
| 171 | } |
| 172 | } |
| 173 | /** |
| 174 | * Adds a short option value. |
| 175 | * |
| 176 | * @throws RuntimeException When option given doesn't exist |
| 177 | */ |
| 178 | private function addShortOption(string $shortcut, $value) |
| 179 | { |
| 180 | if (!$this->definition->hasShortcut($shortcut)) { |
| 181 | throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); |
| 182 | } |
| 183 | $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); |
| 184 | } |
| 185 | /** |
| 186 | * Adds a long option value. |
| 187 | * |
| 188 | * @throws RuntimeException When option given doesn't exist |
| 189 | */ |
| 190 | private function addLongOption(string $name, $value) |
| 191 | { |
| 192 | if (!$this->definition->hasOption($name)) { |
| 193 | if (!$this->definition->hasNegation($name)) { |
| 194 | throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); |
| 195 | } |
| 196 | $optionName = $this->definition->negationToName($name); |
| 197 | if (null !== $value) { |
| 198 | throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
| 199 | } |
| 200 | $this->options[$optionName] = \false; |
| 201 | return; |
| 202 | } |
| 203 | $option = $this->definition->getOption($name); |
| 204 | if (null !== $value && !$option->acceptValue()) { |
| 205 | throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); |
| 206 | } |
| 207 | if (\in_array($value, ['', null], \true) && $option->acceptValue() && \count($this->parsed)) { |
| 208 | // if option accepts an optional or mandatory argument |
| 209 | // let's see if there is one provided |
| 210 | $next = array_shift($this->parsed); |
| 211 | if (isset($next[0]) && '-' !== $next[0] || \in_array($next, ['', null], \true)) { |
| 212 | $value = $next; |
| 213 | } else { |
| 214 | array_unshift($this->parsed, $next); |
| 215 | } |
| 216 | } |
| 217 | if (null === $value) { |
| 218 | if ($option->isValueRequired()) { |
| 219 | throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); |
| 220 | } |
| 221 | if (!$option->isArray() && !$option->isValueOptional()) { |
| 222 | $value = \true; |
| 223 | } |
| 224 | } |
| 225 | if ($option->isArray()) { |
| 226 | $this->options[$name][] = $value; |
| 227 | } else { |
| 228 | $this->options[$name] = $value; |
| 229 | } |
| 230 | } |
| 231 | /** |
| 232 | * {@inheritdoc} |
| 233 | */ |
| 234 | public function getFirstArgument() |
| 235 | { |
| 236 | $isOption = \false; |
| 237 | foreach ($this->tokens as $i => $token) { |
| 238 | if ($token && '-' === $token[0]) { |
| 239 | if (str_contains($token, '=') || !isset($this->tokens[$i + 1])) { |
| 240 | continue; |
| 241 | } |
| 242 | // If it's a long option, consider that everything after "--" is the option name. |
| 243 | // Otherwise, use the last char (if it's a short option set, only the last one can take a value with space separator) |
| 244 | $name = '-' === $token[1] ? substr($token, 2) : substr($token, -1); |
| 245 | if (!isset($this->options[$name]) && !$this->definition->hasShortcut($name)) { |
| 246 | // noop |
| 247 | } elseif ((isset($this->options[$name]) || isset($this->options[$name = $this->definition->shortcutToName($name)])) && $this->tokens[$i + 1] === $this->options[$name]) { |
| 248 | $isOption = \true; |
| 249 | } |
| 250 | continue; |
| 251 | } |
| 252 | if ($isOption) { |
| 253 | $isOption = \false; |
| 254 | continue; |
| 255 | } |
| 256 | return $token; |
| 257 | } |
| 258 | return null; |
| 259 | } |
| 260 | /** |
| 261 | * {@inheritdoc} |
| 262 | */ |
| 263 | public function hasParameterOption($values, bool $onlyParams = \false) |
| 264 | { |
| 265 | $values = (array) $values; |
| 266 | foreach ($this->tokens as $token) { |
| 267 | if ($onlyParams && '--' === $token) { |
| 268 | return \false; |
| 269 | } |
| 270 | foreach ($values as $value) { |
| 271 | // Options with values: |
| 272 | // For long options, test for '--option=' at beginning |
| 273 | // For short options, test for '-o' at beginning |
| 274 | $leading = str_starts_with($value, '--') ? $value . '=' : $value; |
| 275 | if ($token === $value || '' !== $leading && str_starts_with($token, $leading)) { |
| 276 | return \true; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | return \false; |
| 281 | } |
| 282 | /** |
| 283 | * {@inheritdoc} |
| 284 | */ |
| 285 | public function getParameterOption($values, $default = \false, bool $onlyParams = \false) |
| 286 | { |
| 287 | $values = (array) $values; |
| 288 | $tokens = $this->tokens; |
| 289 | while (0 < \count($tokens)) { |
| 290 | $token = array_shift($tokens); |
| 291 | if ($onlyParams && '--' === $token) { |
| 292 | return $default; |
| 293 | } |
| 294 | foreach ($values as $value) { |
| 295 | if ($token === $value) { |
| 296 | return array_shift($tokens); |
| 297 | } |
| 298 | // Options with values: |
| 299 | // For long options, test for '--option=' at beginning |
| 300 | // For short options, test for '-o' at beginning |
| 301 | $leading = str_starts_with($value, '--') ? $value . '=' : $value; |
| 302 | if ('' !== $leading && str_starts_with($token, $leading)) { |
| 303 | return substr($token, \strlen($leading)); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | return $default; |
| 308 | } |
| 309 | /** |
| 310 | * Returns a stringified representation of the args passed to the command. |
| 311 | * |
| 312 | * @return string |
| 313 | */ |
| 314 | public function __toString() |
| 315 | { |
| 316 | $tokens = array_map(function ($token) { |
| 317 | if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { |
| 318 | return $match[1] . $this->escapeToken($match[2]); |
| 319 | } |
| 320 | if ($token && '-' !== $token[0]) { |
| 321 | return $this->escapeToken($token); |
| 322 | } |
| 323 | return $token; |
| 324 | }, $this->tokens); |
| 325 | return implode(' ', $tokens); |
| 326 | } |
| 327 | } |
| 328 |