[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/console/Input/ -> Input.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\RuntimeException;
  16  
  17  /**
  18   * Input is the base class for all concrete Input classes.
  19   *
  20   * Three concrete classes are provided by default:
  21   *
  22   *  * `ArgvInput`: The input comes from the CLI arguments (argv)
  23   *  * `StringInput`: The input is provided as a string
  24   *  * `ArrayInput`: The input is provided as an array
  25   *
  26   * @author Fabien Potencier <[email protected]>
  27   */
  28  abstract class Input implements InputInterface, StreamableInputInterface
  29  {
  30      protected $definition;
  31      protected $stream;
  32      protected $options = [];
  33      protected $arguments = [];
  34      protected $interactive = true;
  35  
  36      public function __construct(InputDefinition $definition = null)
  37      {
  38          if (null === $definition) {
  39              $this->definition = new InputDefinition();
  40          } else {
  41              $this->bind($definition);
  42              $this->validate();
  43          }
  44      }
  45  
  46      /**
  47       * {@inheritdoc}
  48       */
  49      public function bind(InputDefinition $definition)
  50      {
  51          $this->arguments = [];
  52          $this->options = [];
  53          $this->definition = $definition;
  54  
  55          $this->parse();
  56      }
  57  
  58      /**
  59       * Processes command line arguments.
  60       */
  61      abstract protected function parse();
  62  
  63      /**
  64       * {@inheritdoc}
  65       */
  66      public function validate()
  67      {
  68          $definition = $this->definition;
  69          $givenArguments = $this->arguments;
  70  
  71          $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
  72              return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
  73          });
  74  
  75          if (\count($missingArguments) > 0) {
  76              throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
  77          }
  78      }
  79  
  80      /**
  81       * {@inheritdoc}
  82       */
  83      public function isInteractive()
  84      {
  85          return $this->interactive;
  86      }
  87  
  88      /**
  89       * {@inheritdoc}
  90       */
  91      public function setInteractive(bool $interactive)
  92      {
  93          $this->interactive = $interactive;
  94      }
  95  
  96      /**
  97       * {@inheritdoc}
  98       */
  99      public function getArguments()
 100      {
 101          return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
 102      }
 103  
 104      /**
 105       * {@inheritdoc}
 106       */
 107      public function getArgument(string $name)
 108      {
 109          if (!$this->definition->hasArgument($name)) {
 110              throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
 111          }
 112  
 113          return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault();
 114      }
 115  
 116      /**
 117       * {@inheritdoc}
 118       */
 119      public function setArgument(string $name, $value)
 120      {
 121          if (!$this->definition->hasArgument($name)) {
 122              throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
 123          }
 124  
 125          $this->arguments[$name] = $value;
 126      }
 127  
 128      /**
 129       * {@inheritdoc}
 130       */
 131      public function hasArgument(string $name)
 132      {
 133          return $this->definition->hasArgument($name);
 134      }
 135  
 136      /**
 137       * {@inheritdoc}
 138       */
 139      public function getOptions()
 140      {
 141          return array_merge($this->definition->getOptionDefaults(), $this->options);
 142      }
 143  
 144      /**
 145       * {@inheritdoc}
 146       */
 147      public function getOption(string $name)
 148      {
 149          if ($this->definition->hasNegation($name)) {
 150              if (null === $value = $this->getOption($this->definition->negationToName($name))) {
 151                  return $value;
 152              }
 153  
 154              return !$value;
 155          }
 156  
 157          if (!$this->definition->hasOption($name)) {
 158              throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
 159          }
 160  
 161          return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
 162      }
 163  
 164      /**
 165       * {@inheritdoc}
 166       */
 167      public function setOption(string $name, $value)
 168      {
 169          if ($this->definition->hasNegation($name)) {
 170              $this->options[$this->definition->negationToName($name)] = !$value;
 171  
 172              return;
 173          } elseif (!$this->definition->hasOption($name)) {
 174              throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
 175          }
 176  
 177          $this->options[$name] = $value;
 178      }
 179  
 180      /**
 181       * {@inheritdoc}
 182       */
 183      public function hasOption(string $name)
 184      {
 185          return $this->definition->hasOption($name) || $this->definition->hasNegation($name);
 186      }
 187  
 188      /**
 189       * Escapes a token through escapeshellarg if it contains unsafe chars.
 190       *
 191       * @return string
 192       */
 193      public function escapeToken(string $token)
 194      {
 195          return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
 196      }
 197  
 198      /**
 199       * {@inheritdoc}
 200       */
 201      public function setStream($stream)
 202      {
 203          $this->stream = $stream;
 204      }
 205  
 206      /**
 207       * {@inheritdoc}
 208       */
 209      public function getStream()
 210      {
 211          return $this->stream;
 212      }
 213  }


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