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
InputDefinition.php
371 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\LogicException; |
| 15 | /** |
| 16 | * A InputDefinition represents a set of valid command line arguments and options. |
| 17 | * |
| 18 | * Usage: |
| 19 | * |
| 20 | * $definition = new InputDefinition([ |
| 21 | * new InputArgument('name', InputArgument::REQUIRED), |
| 22 | * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED), |
| 23 | * ]); |
| 24 | * |
| 25 | * @author Fabien Potencier <fabien@symfony.com> |
| 26 | * @internal |
| 27 | */ |
| 28 | class InputDefinition |
| 29 | { |
| 30 | private $arguments; |
| 31 | private $requiredCount; |
| 32 | private $lastArrayArgument; |
| 33 | private $lastOptionalArgument; |
| 34 | private $options; |
| 35 | private $negations; |
| 36 | private $shortcuts; |
| 37 | /** |
| 38 | * @param array $definition An array of InputArgument and InputOption instance |
| 39 | */ |
| 40 | public function __construct(array $definition = []) |
| 41 | { |
| 42 | $this->setDefinition($definition); |
| 43 | } |
| 44 | /** |
| 45 | * Sets the definition of the input. |
| 46 | */ |
| 47 | public function setDefinition(array $definition) |
| 48 | { |
| 49 | $arguments = []; |
| 50 | $options = []; |
| 51 | foreach ($definition as $item) { |
| 52 | if ($item instanceof InputOption) { |
| 53 | $options[] = $item; |
| 54 | } else { |
| 55 | $arguments[] = $item; |
| 56 | } |
| 57 | } |
| 58 | $this->setArguments($arguments); |
| 59 | $this->setOptions($options); |
| 60 | } |
| 61 | /** |
| 62 | * Sets the InputArgument objects. |
| 63 | * |
| 64 | * @param InputArgument[] $arguments An array of InputArgument objects |
| 65 | */ |
| 66 | public function setArguments(array $arguments = []) |
| 67 | { |
| 68 | $this->arguments = []; |
| 69 | $this->requiredCount = 0; |
| 70 | $this->lastOptionalArgument = null; |
| 71 | $this->lastArrayArgument = null; |
| 72 | $this->addArguments($arguments); |
| 73 | } |
| 74 | /** |
| 75 | * Adds an array of InputArgument objects. |
| 76 | * |
| 77 | * @param InputArgument[] $arguments An array of InputArgument objects |
| 78 | */ |
| 79 | public function addArguments(?array $arguments = []) |
| 80 | { |
| 81 | if (null !== $arguments) { |
| 82 | foreach ($arguments as $argument) { |
| 83 | $this->addArgument($argument); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | /** |
| 88 | * @throws LogicException When incorrect argument is given |
| 89 | */ |
| 90 | public function addArgument(InputArgument $argument) |
| 91 | { |
| 92 | if (isset($this->arguments[$argument->getName()])) { |
| 93 | throw new LogicException(\sprintf('An argument with name "%s" already exists.', $argument->getName())); |
| 94 | } |
| 95 | if (null !== $this->lastArrayArgument) { |
| 96 | throw new LogicException(\sprintf('Cannot add a required argument "%s" after an array argument "%s".', $argument->getName(), $this->lastArrayArgument->getName())); |
| 97 | } |
| 98 | if ($argument->isRequired() && null !== $this->lastOptionalArgument) { |
| 99 | throw new LogicException(\sprintf('Cannot add a required argument "%s" after an optional one "%s".', $argument->getName(), $this->lastOptionalArgument->getName())); |
| 100 | } |
| 101 | if ($argument->isArray()) { |
| 102 | $this->lastArrayArgument = $argument; |
| 103 | } |
| 104 | if ($argument->isRequired()) { |
| 105 | ++$this->requiredCount; |
| 106 | } else { |
| 107 | $this->lastOptionalArgument = $argument; |
| 108 | } |
| 109 | $this->arguments[$argument->getName()] = $argument; |
| 110 | } |
| 111 | /** |
| 112 | * Returns an InputArgument by name or by position. |
| 113 | * |
| 114 | * @param string|int $name The InputArgument name or position |
| 115 | * |
| 116 | * @return InputArgument |
| 117 | * |
| 118 | * @throws InvalidArgumentException When argument given doesn't exist |
| 119 | */ |
| 120 | public function getArgument($name) |
| 121 | { |
| 122 | if (!$this->hasArgument($name)) { |
| 123 | throw new InvalidArgumentException(\sprintf('The "%s" argument does not exist.', $name)); |
| 124 | } |
| 125 | $arguments = \is_int($name) ? \array_values($this->arguments) : $this->arguments; |
| 126 | return $arguments[$name]; |
| 127 | } |
| 128 | /** |
| 129 | * Returns true if an InputArgument object exists by name or position. |
| 130 | * |
| 131 | * @param string|int $name The InputArgument name or position |
| 132 | * |
| 133 | * @return bool |
| 134 | */ |
| 135 | public function hasArgument($name) |
| 136 | { |
| 137 | $arguments = \is_int($name) ? \array_values($this->arguments) : $this->arguments; |
| 138 | return isset($arguments[$name]); |
| 139 | } |
| 140 | /** |
| 141 | * Gets the array of InputArgument objects. |
| 142 | * |
| 143 | * @return InputArgument[] |
| 144 | */ |
| 145 | public function getArguments() |
| 146 | { |
| 147 | return $this->arguments; |
| 148 | } |
| 149 | /** |
| 150 | * Returns the number of InputArguments. |
| 151 | * |
| 152 | * @return int |
| 153 | */ |
| 154 | public function getArgumentCount() |
| 155 | { |
| 156 | return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments); |
| 157 | } |
| 158 | /** |
| 159 | * Returns the number of required InputArguments. |
| 160 | * |
| 161 | * @return int |
| 162 | */ |
| 163 | public function getArgumentRequiredCount() |
| 164 | { |
| 165 | return $this->requiredCount; |
| 166 | } |
| 167 | /** |
| 168 | * @return array<string|bool|int|float|array|null> |
| 169 | */ |
| 170 | public function getArgumentDefaults() |
| 171 | { |
| 172 | $values = []; |
| 173 | foreach ($this->arguments as $argument) { |
| 174 | $values[$argument->getName()] = $argument->getDefault(); |
| 175 | } |
| 176 | return $values; |
| 177 | } |
| 178 | /** |
| 179 | * Sets the InputOption objects. |
| 180 | * |
| 181 | * @param InputOption[] $options An array of InputOption objects |
| 182 | */ |
| 183 | public function setOptions(array $options = []) |
| 184 | { |
| 185 | $this->options = []; |
| 186 | $this->shortcuts = []; |
| 187 | $this->negations = []; |
| 188 | $this->addOptions($options); |
| 189 | } |
| 190 | /** |
| 191 | * Adds an array of InputOption objects. |
| 192 | * |
| 193 | * @param InputOption[] $options An array of InputOption objects |
| 194 | */ |
| 195 | public function addOptions(array $options = []) |
| 196 | { |
| 197 | foreach ($options as $option) { |
| 198 | $this->addOption($option); |
| 199 | } |
| 200 | } |
| 201 | /** |
| 202 | * @throws LogicException When option given already exist |
| 203 | */ |
| 204 | public function addOption(InputOption $option) |
| 205 | { |
| 206 | if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) { |
| 207 | throw new LogicException(\sprintf('An option named "%s" already exists.', $option->getName())); |
| 208 | } |
| 209 | if (isset($this->negations[$option->getName()])) { |
| 210 | throw new LogicException(\sprintf('An option named "%s" already exists.', $option->getName())); |
| 211 | } |
| 212 | if ($option->getShortcut()) { |
| 213 | foreach (\explode('|', $option->getShortcut()) as $shortcut) { |
| 214 | if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) { |
| 215 | throw new LogicException(\sprintf('An option with shortcut "%s" already exists.', $shortcut)); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | $this->options[$option->getName()] = $option; |
| 220 | if ($option->getShortcut()) { |
| 221 | foreach (\explode('|', $option->getShortcut()) as $shortcut) { |
| 222 | $this->shortcuts[$shortcut] = $option->getName(); |
| 223 | } |
| 224 | } |
| 225 | if ($option->isNegatable()) { |
| 226 | $negatedName = 'no-' . $option->getName(); |
| 227 | if (isset($this->options[$negatedName])) { |
| 228 | throw new LogicException(\sprintf('An option named "%s" already exists.', $negatedName)); |
| 229 | } |
| 230 | $this->negations[$negatedName] = $option->getName(); |
| 231 | } |
| 232 | } |
| 233 | /** |
| 234 | * Returns an InputOption by name. |
| 235 | * |
| 236 | * @return InputOption |
| 237 | * |
| 238 | * @throws InvalidArgumentException When option given doesn't exist |
| 239 | */ |
| 240 | public function getOption(string $name) |
| 241 | { |
| 242 | if (!$this->hasOption($name)) { |
| 243 | throw new InvalidArgumentException(\sprintf('The "--%s" option does not exist.', $name)); |
| 244 | } |
| 245 | return $this->options[$name]; |
| 246 | } |
| 247 | /** |
| 248 | * Returns true if an InputOption object exists by name. |
| 249 | * |
| 250 | * This method can't be used to check if the user included the option when |
| 251 | * executing the command (use getOption() instead). |
| 252 | * |
| 253 | * @return bool |
| 254 | */ |
| 255 | public function hasOption(string $name) |
| 256 | { |
| 257 | return isset($this->options[$name]); |
| 258 | } |
| 259 | /** |
| 260 | * Gets the array of InputOption objects. |
| 261 | * |
| 262 | * @return InputOption[] |
| 263 | */ |
| 264 | public function getOptions() |
| 265 | { |
| 266 | return $this->options; |
| 267 | } |
| 268 | /** |
| 269 | * Returns true if an InputOption object exists by shortcut. |
| 270 | * |
| 271 | * @return bool |
| 272 | */ |
| 273 | public function hasShortcut(string $name) |
| 274 | { |
| 275 | return isset($this->shortcuts[$name]); |
| 276 | } |
| 277 | /** |
| 278 | * Returns true if an InputOption object exists by negated name. |
| 279 | */ |
| 280 | public function hasNegation(string $name) : bool |
| 281 | { |
| 282 | return isset($this->negations[$name]); |
| 283 | } |
| 284 | /** |
| 285 | * Gets an InputOption by shortcut. |
| 286 | * |
| 287 | * @return InputOption |
| 288 | */ |
| 289 | public function getOptionForShortcut(string $shortcut) |
| 290 | { |
| 291 | return $this->getOption($this->shortcutToName($shortcut)); |
| 292 | } |
| 293 | /** |
| 294 | * @return array<string|bool|int|float|array|null> |
| 295 | */ |
| 296 | public function getOptionDefaults() |
| 297 | { |
| 298 | $values = []; |
| 299 | foreach ($this->options as $option) { |
| 300 | $values[$option->getName()] = $option->getDefault(); |
| 301 | } |
| 302 | return $values; |
| 303 | } |
| 304 | /** |
| 305 | * Returns the InputOption name given a shortcut. |
| 306 | * |
| 307 | * @throws InvalidArgumentException When option given does not exist |
| 308 | * |
| 309 | * @internal |
| 310 | */ |
| 311 | public function shortcutToName(string $shortcut) : string |
| 312 | { |
| 313 | if (!isset($this->shortcuts[$shortcut])) { |
| 314 | throw new InvalidArgumentException(\sprintf('The "-%s" option does not exist.', $shortcut)); |
| 315 | } |
| 316 | return $this->shortcuts[$shortcut]; |
| 317 | } |
| 318 | /** |
| 319 | * Returns the InputOption name given a negation. |
| 320 | * |
| 321 | * @throws InvalidArgumentException When option given does not exist |
| 322 | * |
| 323 | * @internal |
| 324 | */ |
| 325 | public function negationToName(string $negation) : string |
| 326 | { |
| 327 | if (!isset($this->negations[$negation])) { |
| 328 | throw new InvalidArgumentException(\sprintf('The "--%s" option does not exist.', $negation)); |
| 329 | } |
| 330 | return $this->negations[$negation]; |
| 331 | } |
| 332 | /** |
| 333 | * Gets the synopsis. |
| 334 | * |
| 335 | * @return string |
| 336 | */ |
| 337 | public function getSynopsis(bool $short = \false) |
| 338 | { |
| 339 | $elements = []; |
| 340 | if ($short && $this->getOptions()) { |
| 341 | $elements[] = '[options]'; |
| 342 | } elseif (!$short) { |
| 343 | foreach ($this->getOptions() as $option) { |
| 344 | $value = ''; |
| 345 | if ($option->acceptValue()) { |
| 346 | $value = \sprintf(' %s%s%s', $option->isValueOptional() ? '[' : '', \strtoupper($option->getName()), $option->isValueOptional() ? ']' : ''); |
| 347 | } |
| 348 | $shortcut = $option->getShortcut() ? \sprintf('-%s|', $option->getShortcut()) : ''; |
| 349 | $negation = $option->isNegatable() ? \sprintf('|--no-%s', $option->getName()) : ''; |
| 350 | $elements[] = \sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation); |
| 351 | } |
| 352 | } |
| 353 | if (\count($elements) && $this->getArguments()) { |
| 354 | $elements[] = '[--]'; |
| 355 | } |
| 356 | $tail = ''; |
| 357 | foreach ($this->getArguments() as $argument) { |
| 358 | $element = '<' . $argument->getName() . '>'; |
| 359 | if ($argument->isArray()) { |
| 360 | $element .= '...'; |
| 361 | } |
| 362 | if (!$argument->isRequired()) { |
| 363 | $element = '[' . $element; |
| 364 | $tail .= ']'; |
| 365 | } |
| 366 | $elements[] = $element; |
| 367 | } |
| 368 | return \implode(' ', $elements) . $tail; |
| 369 | } |
| 370 | } |
| 371 |