[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/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 <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\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 <fabien@symfony.com>
  27   */
  28  abstract class Input implements InputInterface
  29  {
  30      protected $definition;
  31      protected $options = array();
  32      protected $arguments = array();
  33      protected $interactive = true;
  34  
  35      public function __construct(InputDefinition $definition = null)
  36      {
  37          if (null === $definition) {
  38              $this->definition = new InputDefinition();
  39          } else {
  40              $this->bind($definition);
  41              $this->validate();
  42          }
  43      }
  44  
  45      /**
  46       * {@inheritdoc}
  47       */
  48      public function bind(InputDefinition $definition)
  49      {
  50          $this->arguments = array();
  51          $this->options = array();
  52          $this->definition = $definition;
  53  
  54          $this->parse();
  55      }
  56  
  57      /**
  58       * Processes command line arguments.
  59       */
  60      abstract protected function parse();
  61  
  62      /**
  63       * {@inheritdoc}
  64       */
  65      public function validate()
  66      {
  67          $definition = $this->definition;
  68          $givenArguments = $this->arguments;
  69  
  70          $missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {
  71              return !array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();
  72          });
  73  
  74          if (\count($missingArguments) > 0) {
  75              throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
  76          }
  77      }
  78  
  79      /**
  80       * {@inheritdoc}
  81       */
  82      public function isInteractive()
  83      {
  84          return $this->interactive;
  85      }
  86  
  87      /**
  88       * {@inheritdoc}
  89       */
  90      public function setInteractive($interactive)
  91      {
  92          $this->interactive = (bool) $interactive;
  93      }
  94  
  95      /**
  96       * {@inheritdoc}
  97       */
  98      public function getArguments()
  99      {
 100          return array_merge($this->definition->getArgumentDefaults(), $this->arguments);
 101      }
 102  
 103      /**
 104       * {@inheritdoc}
 105       */
 106      public function getArgument($name)
 107      {
 108          if (!$this->definition->hasArgument($name)) {
 109              throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
 110          }
 111  
 112          return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault();
 113      }
 114  
 115      /**
 116       * {@inheritdoc}
 117       */
 118      public function setArgument($name, $value)
 119      {
 120          if (!$this->definition->hasArgument($name)) {
 121              throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
 122          }
 123  
 124          $this->arguments[$name] = $value;
 125      }
 126  
 127      /**
 128       * {@inheritdoc}
 129       */
 130      public function hasArgument($name)
 131      {
 132          return $this->definition->hasArgument($name);
 133      }
 134  
 135      /**
 136       * {@inheritdoc}
 137       */
 138      public function getOptions()
 139      {
 140          return array_merge($this->definition->getOptionDefaults(), $this->options);
 141      }
 142  
 143      /**
 144       * {@inheritdoc}
 145       */
 146      public function getOption($name)
 147      {
 148          if (!$this->definition->hasOption($name)) {
 149              throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
 150          }
 151  
 152          return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();
 153      }
 154  
 155      /**
 156       * {@inheritdoc}
 157       */
 158      public function setOption($name, $value)
 159      {
 160          if (!$this->definition->hasOption($name)) {
 161              throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));
 162          }
 163  
 164          $this->options[$name] = $value;
 165      }
 166  
 167      /**
 168       * {@inheritdoc}
 169       */
 170      public function hasOption($name)
 171      {
 172          return $this->definition->hasOption($name);
 173      }
 174  
 175      /**
 176       * Escapes a token through escapeshellarg if it contains unsafe chars.
 177       *
 178       * @param string $token
 179       *
 180       * @return string
 181       */
 182      public function escapeToken($token)
 183      {
 184          return preg_match('{^[\w-]+$}', $token) ? $token : escapeshellarg($token);
 185      }
 186  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1