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