Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

ArgvInput.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 10.43 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\Console\Input;
013   
014  /**
015   * ArgvInput represents an input coming from the CLI arguments.
016   *
017   * Usage:
018   *
019   *     $input = new ArgvInput();
020   *
021   * By default, the `$_SERVER['argv']` array is used for the input values.
022   *
023   * This can be overridden by explicitly passing the input values in the constructor:
024   *
025   *     $input = new ArgvInput($_SERVER['argv']);
026   *
027   * If you pass it yourself, don't forget that the first element of the array
028   * is the name of the running application.
029   *
030   * When passing an argument to the constructor, be sure that it respects
031   * the same rules as the argv one. It's almost always better to use the
032   * `StringInput` when you want to provide your own input.
033   *
034   * @author Fabien Potencier <fabien@symfony.com>
035   *
036   * @see    http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
037   * @see    http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
038   *
039   * @api
040   */
041  class ArgvInput extends Input
042  {
043      private $tokens;
044      private $parsed;
045   
046      /**
047       * Constructor.
048       *
049       * @param array           $argv       An array of parameters from the CLI (in the argv format)
050       * @param InputDefinition $definition A InputDefinition instance
051       *
052       * @api
053       */
054      public function __construct(array $argv = null, InputDefinition $definition = null)
055      {
056          if (null === $argv) {
057              $argv = $_SERVER['argv'];
058          }
059   
060          // strip the application name
061          array_shift($argv);
062   
063          $this->tokens = $argv;
064   
065          parent::__construct($definition);
066      }
067   
068      protected function setTokens(array $tokens)
069      {
070          $this->tokens = $tokens;
071      }
072   
073      /**
074       * Processes command line arguments.
075       */
076      protected function parse()
077      {
078          $parseOptions = true;
079          $this->parsed = $this->tokens;
080          while (null !== $token = array_shift($this->parsed)) {
081              if ($parseOptions && '' == $token) {
082                  $this->parseArgument($token);
083              } elseif ($parseOptions && '--' == $token) {
084                  $parseOptions = false;
085              } elseif ($parseOptions && 0 === strpos($token, '--')) {
086                  $this->parseLongOption($token);
087              } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {
088                  $this->parseShortOption($token);
089              } else {
090                  $this->parseArgument($token);
091              }
092          }
093      }
094   
095      /**
096       * Parses a short option.
097       *
098       * @param string $token The current token.
099       */
100      private function parseShortOption($token)
101      {
102          $name = substr($token, 1);
103   
104          if (strlen($name) > 1) {
105              if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {
106                  // an option with a value (with no space)
107                  $this->addShortOption($name[0], substr($name, 1));
108              } else {
109                  $this->parseShortOptionSet($name);
110              }
111          } else {
112              $this->addShortOption($name, null);
113          }
114      }
115   
116      /**
117       * Parses a short option set.
118       *
119       * @param string $name The current token
120       *
121       * @throws \RuntimeException When option given doesn't exist
122       */
123      private function parseShortOptionSet($name)
124      {
125          $len = strlen($name);
126          for ($i = 0; $i < $len; $i++) {
127              if (!$this->definition->hasShortcut($name[$i])) {
128                  throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i]));
129              }
130   
131              $option = $this->definition->getOptionForShortcut($name[$i]);
132              if ($option->acceptValue()) {
133                  $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1));
134   
135                  break;
136              } else {
137                  $this->addLongOption($option->getName(), null);
138              }
139          }
140      }
141   
142      /**
143       * Parses a long option.
144       *
145       * @param string $token The current token
146       */
147      private function parseLongOption($token)
148      {
149          $name = substr($token, 2);
150   
151          if (false !== $pos = strpos($name, '=')) {
152              $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1));
153          } else {
154              $this->addLongOption($name, null);
155          }
156      }
157   
158      /**
159       * Parses an argument.
160       *
161       * @param string $token The current token
162       *
163       * @throws \RuntimeException When too many arguments are given
164       */
165      private function parseArgument($token)
166      {
167          $c = count($this->arguments);
168   
169          // if input is expecting another argument, add it
170          if ($this->definition->hasArgument($c)) {
171              $arg = $this->definition->getArgument($c);
172              $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token;
173   
174          // if last argument isArray(), append token to last argument
175          } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) {
176              $arg = $this->definition->getArgument($c - 1);
177              $this->arguments[$arg->getName()][] = $token;
178   
179          // unexpected argument
180          } else {
181              throw new \RuntimeException('Too many arguments.');
182          }
183      }
184   
185      /**
186       * Adds a short option value.
187       *
188       * @param string $shortcut The short option key
189       * @param mixed  $value    The value for the option
190       *
191       * @throws \RuntimeException When option given doesn't exist
192       */
193      private function addShortOption($shortcut, $value)
194      {
195          if (!$this->definition->hasShortcut($shortcut)) {
196              throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));
197          }
198   
199          $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
200      }
201   
202      /**
203       * Adds a long option value.
204       *
205       * @param string $name  The long option key
206       * @param mixed  $value The value for the option
207       *
208       * @throws \RuntimeException When option given doesn't exist
209       */
210      private function addLongOption($name, $value)
211      {
212          if (!$this->definition->hasOption($name)) {
213              throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name));
214          }
215   
216          $option = $this->definition->getOption($name);
217   
218          // Convert false values (from a previous call to substr()) to null
219          if (false === $value) {
220              $value = null;
221          }
222   
223          if (null !== $value && !$option->acceptValue()) {
224              throw new \RuntimeException(sprintf('The "--%s" option does not accept a value.', $name, $value));
225          }
226   
227          if (null === $value && $option->acceptValue() && count($this->parsed)) {
228              // if option accepts an optional or mandatory argument
229              // let's see if there is one provided
230              $next = array_shift($this->parsed);
231              if (isset($next[0]) && '-' !== $next[0]) {
232                  $value = $next;
233              } elseif (empty($next)) {
234                  $value = '';
235              } else {
236                  array_unshift($this->parsed, $next);
237              }
238          }
239   
240          if (null === $value) {
241              if ($option->isValueRequired()) {
242                  throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name));
243              }
244   
245              if (!$option->isArray()) {
246                  $value = $option->isValueOptional() ? $option->getDefault() : true;
247              }
248          }
249   
250          if ($option->isArray()) {
251              $this->options[$name][] = $value;
252          } else {
253              $this->options[$name] = $value;
254          }
255      }
256   
257      /**
258       * Returns the first argument from the raw parameters (not parsed).
259       *
260       * @return string The value of the first argument or null otherwise
261       */
262      public function getFirstArgument()
263      {
264          foreach ($this->tokens as $token) {
265              if ($token && '-' === $token[0]) {
266                  continue;
267              }
268   
269              return $token;
270          }
271      }
272   
273      /**
274       * Returns true if the raw parameters (not parsed) contain a value.
275       *
276       * This method is to be used to introspect the input parameters
277       * before they have been validated. It must be used carefully.
278       *
279       * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
280       *
281       * @return bool    true if the value is contained in the raw parameters
282       */
283      public function hasParameterOption($values)
284      {
285          $values = (array) $values;
286   
287          foreach ($this->tokens as $token) {
288              foreach ($values as $value) {
289                  if ($token === $value || 0 === strpos($token, $value.'=')) {
290                      return true;
291                  }
292              }
293          }
294   
295          return false;
296      }
297   
298      /**
299       * Returns the value of a raw option (not parsed).
300       *
301       * This method is to be used to introspect the input parameters
302       * before they have been validated. It must be used carefully.
303       *
304       * @param string|array $values  The value(s) to look for in the raw parameters (can be an array)
305       * @param mixed        $default The default value to return if no result is found
306       *
307       * @return mixed The option value
308       */
309      public function getParameterOption($values, $default = false)
310      {
311          $values = (array) $values;
312   
313          $tokens = $this->tokens;
314          while ($token = array_shift($tokens)) {
315              foreach ($values as $value) {
316                  if ($token === $value || 0 === strpos($token, $value.'=')) {
317                      if (false !== $pos = strpos($token, '=')) {
318                          return substr($token, $pos + 1);
319                      }
320   
321                      return array_shift($tokens);
322                  }
323              }
324          }
325   
326          return $default;
327      }
328   
329      /**
330       * Returns a stringified representation of the args passed to the command
331       *
332       * @return string
333       */
334      public function __toString()
335      {
336          $self = $this;
337          $tokens = array_map(function ($token) use ($self) {
338              if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
339                  return $match[1].$self->escapeToken($match[2]);
340              }
341   
342              if ($token && $token[0] !== '-') {
343                  return $self->escapeToken($token);
344              }
345   
346              return $token;
347          }, $this->tokens);
348   
349          return implode(' ', $tokens);
350      }
351  }
352