[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1