[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/console/Input/ -> InputDefinition.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   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 <[email protected]>
  28   */
  29  class InputDefinition
  30  {
  31      private $arguments;
  32      private $requiredCount;
  33      private $lastArrayArgument;
  34      private $lastOptionalArgument;
  35      private $options;
  36      private $negations;
  37      private $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)
  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 = [])
  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 = [])
  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)
  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       * @param string|int $name The InputArgument name or position
 128       *
 129       * @return InputArgument
 130       *
 131       * @throws InvalidArgumentException When argument given doesn't exist
 132       */
 133      public function getArgument($name)
 134      {
 135          if (!$this->hasArgument($name)) {
 136              throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
 137          }
 138  
 139          $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
 140  
 141          return $arguments[$name];
 142      }
 143  
 144      /**
 145       * Returns true if an InputArgument object exists by name or position.
 146       *
 147       * @param string|int $name The InputArgument name or position
 148       *
 149       * @return bool
 150       */
 151      public function hasArgument($name)
 152      {
 153          $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
 154  
 155          return isset($arguments[$name]);
 156      }
 157  
 158      /**
 159       * Gets the array of InputArgument objects.
 160       *
 161       * @return InputArgument[]
 162       */
 163      public function getArguments()
 164      {
 165          return $this->arguments;
 166      }
 167  
 168      /**
 169       * Returns the number of InputArguments.
 170       *
 171       * @return int
 172       */
 173      public function getArgumentCount()
 174      {
 175          return null !== $this->lastArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
 176      }
 177  
 178      /**
 179       * Returns the number of required InputArguments.
 180       *
 181       * @return int
 182       */
 183      public function getArgumentRequiredCount()
 184      {
 185          return $this->requiredCount;
 186      }
 187  
 188      /**
 189       * @return array<string|bool|int|float|array|null>
 190       */
 191      public function getArgumentDefaults()
 192      {
 193          $values = [];
 194          foreach ($this->arguments as $argument) {
 195              $values[$argument->getName()] = $argument->getDefault();
 196          }
 197  
 198          return $values;
 199      }
 200  
 201      /**
 202       * Sets the InputOption objects.
 203       *
 204       * @param InputOption[] $options An array of InputOption objects
 205       */
 206      public function setOptions(array $options = [])
 207      {
 208          $this->options = [];
 209          $this->shortcuts = [];
 210          $this->negations = [];
 211          $this->addOptions($options);
 212      }
 213  
 214      /**
 215       * Adds an array of InputOption objects.
 216       *
 217       * @param InputOption[] $options An array of InputOption objects
 218       */
 219      public function addOptions(array $options = [])
 220      {
 221          foreach ($options as $option) {
 222              $this->addOption($option);
 223          }
 224      }
 225  
 226      /**
 227       * @throws LogicException When option given already exist
 228       */
 229      public function addOption(InputOption $option)
 230      {
 231          if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
 232              throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
 233          }
 234          if (isset($this->negations[$option->getName()])) {
 235              throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
 236          }
 237  
 238          if ($option->getShortcut()) {
 239              foreach (explode('|', $option->getShortcut()) as $shortcut) {
 240                  if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
 241                      throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
 242                  }
 243              }
 244          }
 245  
 246          $this->options[$option->getName()] = $option;
 247          if ($option->getShortcut()) {
 248              foreach (explode('|', $option->getShortcut()) as $shortcut) {
 249                  $this->shortcuts[$shortcut] = $option->getName();
 250              }
 251          }
 252  
 253          if ($option->isNegatable()) {
 254              $negatedName = 'no-'.$option->getName();
 255              if (isset($this->options[$negatedName])) {
 256                  throw new LogicException(sprintf('An option named "%s" already exists.', $negatedName));
 257              }
 258              $this->negations[$negatedName] = $option->getName();
 259          }
 260      }
 261  
 262      /**
 263       * Returns an InputOption by name.
 264       *
 265       * @return InputOption
 266       *
 267       * @throws InvalidArgumentException When option given doesn't exist
 268       */
 269      public function getOption(string $name)
 270      {
 271          if (!$this->hasOption($name)) {
 272              throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
 273          }
 274  
 275          return $this->options[$name];
 276      }
 277  
 278      /**
 279       * Returns true if an InputOption object exists by name.
 280       *
 281       * This method can't be used to check if the user included the option when
 282       * executing the command (use getOption() instead).
 283       *
 284       * @return bool
 285       */
 286      public function hasOption(string $name)
 287      {
 288          return isset($this->options[$name]);
 289      }
 290  
 291      /**
 292       * Gets the array of InputOption objects.
 293       *
 294       * @return InputOption[]
 295       */
 296      public function getOptions()
 297      {
 298          return $this->options;
 299      }
 300  
 301      /**
 302       * Returns true if an InputOption object exists by shortcut.
 303       *
 304       * @return bool
 305       */
 306      public function hasShortcut(string $name)
 307      {
 308          return isset($this->shortcuts[$name]);
 309      }
 310  
 311      /**
 312       * Returns true if an InputOption object exists by negated name.
 313       */
 314      public function hasNegation(string $name): bool
 315      {
 316          return isset($this->negations[$name]);
 317      }
 318  
 319      /**
 320       * Gets an InputOption by shortcut.
 321       *
 322       * @return InputOption
 323       */
 324      public function getOptionForShortcut(string $shortcut)
 325      {
 326          return $this->getOption($this->shortcutToName($shortcut));
 327      }
 328  
 329      /**
 330       * @return array<string|bool|int|float|array|null>
 331       */
 332      public function getOptionDefaults()
 333      {
 334          $values = [];
 335          foreach ($this->options as $option) {
 336              $values[$option->getName()] = $option->getDefault();
 337          }
 338  
 339          return $values;
 340      }
 341  
 342      /**
 343       * Returns the InputOption name given a shortcut.
 344       *
 345       * @throws InvalidArgumentException When option given does not exist
 346       *
 347       * @internal
 348       */
 349      public function shortcutToName(string $shortcut): string
 350      {
 351          if (!isset($this->shortcuts[$shortcut])) {
 352              throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
 353          }
 354  
 355          return $this->shortcuts[$shortcut];
 356      }
 357  
 358      /**
 359       * Returns the InputOption name given a negation.
 360       *
 361       * @throws InvalidArgumentException When option given does not exist
 362       *
 363       * @internal
 364       */
 365      public function negationToName(string $negation): string
 366      {
 367          if (!isset($this->negations[$negation])) {
 368              throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $negation));
 369          }
 370  
 371          return $this->negations[$negation];
 372      }
 373  
 374      /**
 375       * Gets the synopsis.
 376       *
 377       * @return string
 378       */
 379      public function getSynopsis(bool $short = false)
 380      {
 381          $elements = [];
 382  
 383          if ($short && $this->getOptions()) {
 384              $elements[] = '[options]';
 385          } elseif (!$short) {
 386              foreach ($this->getOptions() as $option) {
 387                  $value = '';
 388                  if ($option->acceptValue()) {
 389                      $value = sprintf(
 390                          ' %s%s%s',
 391                          $option->isValueOptional() ? '[' : '',
 392                          strtoupper($option->getName()),
 393                          $option->isValueOptional() ? ']' : ''
 394                      );
 395                  }
 396  
 397                  $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
 398                  $negation = $option->isNegatable() ? sprintf('|--no-%s', $option->getName()) : '';
 399                  $elements[] = sprintf('[%s--%s%s%s]', $shortcut, $option->getName(), $value, $negation);
 400              }
 401          }
 402  
 403          if (\count($elements) && $this->getArguments()) {
 404              $elements[] = '[--]';
 405          }
 406  
 407          $tail = '';
 408          foreach ($this->getArguments() as $argument) {
 409              $element = '<'.$argument->getName().'>';
 410              if ($argument->isArray()) {
 411                  $element .= '...';
 412              }
 413  
 414              if (!$argument->isRequired()) {
 415                  $element = '['.$element;
 416                  $tail .= ']';
 417              }
 418  
 419              $elements[] = $element;
 420          }
 421  
 422          return implode(' ', $elements).$tail;
 423      }
 424  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer