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