Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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

InputDefinition.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 12.81 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  use Symfony\Component\Console\Descriptor\TextDescriptor;
015  use Symfony\Component\Console\Descriptor\XmlDescriptor;
016  use Symfony\Component\Console\Output\BufferedOutput;
017  use Symfony\Component\Console\Exception\InvalidArgumentException;
018  use Symfony\Component\Console\Exception\LogicException;
019   
020  /**
021   * A InputDefinition represents a set of valid command line arguments and options.
022   *
023   * Usage:
024   *
025   *     $definition = new InputDefinition(array(
026   *       new InputArgument('name', InputArgument::REQUIRED),
027   *       new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
028   *     ));
029   *
030   * @author Fabien Potencier <fabien@symfony.com>
031   */
032  class InputDefinition
033  {
034      private $arguments;
035      private $requiredCount;
036      private $hasAnArrayArgument = false;
037      private $hasOptional;
038      private $options;
039      private $shortcuts;
040   
041      /**
042       * Constructor.
043       *
044       * @param array $definition An array of InputArgument and InputOption instance
045       */
046      public function __construct(array $definition = array())
047      {
048          $this->setDefinition($definition);
049      }
050   
051      /**
052       * Sets the definition of the input.
053       *
054       * @param array $definition The definition array
055       */
056      public function setDefinition(array $definition)
057      {
058          $arguments = array();
059          $options = array();
060          foreach ($definition as $item) {
061              if ($item instanceof InputOption) {
062                  $options[] = $item;
063              } else {
064                  $arguments[] = $item;
065              }
066          }
067   
068          $this->setArguments($arguments);
069          $this->setOptions($options);
070      }
071   
072      /**
073       * Sets the InputArgument objects.
074       *
075       * @param InputArgument[] $arguments An array of InputArgument objects
076       */
077      public function setArguments($arguments = array())
078      {
079          $this->arguments = array();
080          $this->requiredCount = 0;
081          $this->hasOptional = false;
082          $this->hasAnArrayArgument = false;
083          $this->addArguments($arguments);
084      }
085   
086      /**
087       * Adds an array of InputArgument objects.
088       *
089       * @param InputArgument[] $arguments An array of InputArgument objects
090       */
091      public function addArguments($arguments = array())
092      {
093          if (null !== $arguments) {
094              foreach ($arguments as $argument) {
095                  $this->addArgument($argument);
096              }
097          }
098      }
099   
100      /**
101       * Adds an InputArgument object.
102       *
103       * @param InputArgument $argument An InputArgument object
104       *
105       * @throws LogicException When incorrect argument is given
106       */
107      public function addArgument(InputArgument $argument)
108      {
109          if (isset($this->arguments[$argument->getName()])) {
110              throw new LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
111          }
112   
113          if ($this->hasAnArrayArgument) {
114              throw new LogicException('Cannot add an argument after an array argument.');
115          }
116   
117          if ($argument->isRequired() && $this->hasOptional) {
118              throw new LogicException('Cannot add a required argument after an optional one.');
119          }
120   
121          if ($argument->isArray()) {
122              $this->hasAnArrayArgument = true;
123          }
124   
125          if ($argument->isRequired()) {
126              ++$this->requiredCount;
127          } else {
128              $this->hasOptional = true;
129          }
130   
131          $this->arguments[$argument->getName()] = $argument;
132      }
133   
134      /**
135       * Returns an InputArgument by name or by position.
136       *
137       * @param string|int $name The InputArgument name or position
138       *
139       * @return InputArgument An InputArgument object
140       *
141       * @throws InvalidArgumentException When argument given doesn't exist
142       */
143      public function getArgument($name)
144      {
145          if (!$this->hasArgument($name)) {
146              throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
147          }
148   
149          $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
150   
151          return $arguments[$name];
152      }
153   
154      /**
155       * Returns true if an InputArgument object exists by name or position.
156       *
157       * @param string|int $name The InputArgument name or position
158       *
159       * @return bool true if the InputArgument object exists, false otherwise
160       */
161      public function hasArgument($name)
162      {
163          $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
164   
165          return isset($arguments[$name]);
166      }
167   
168      /**
169       * Gets the array of InputArgument objects.
170       *
171       * @return InputArgument[] An array of InputArgument objects
172       */
173      public function getArguments()
174      {
175          return $this->arguments;
176      }
177   
178      /**
179       * Returns the number of InputArguments.
180       *
181       * @return int The number of InputArguments
182       */
183      public function getArgumentCount()
184      {
185          return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
186      }
187   
188      /**
189       * Returns the number of required InputArguments.
190       *
191       * @return int The number of required InputArguments
192       */
193      public function getArgumentRequiredCount()
194      {
195          return $this->requiredCount;
196      }
197   
198      /**
199       * Gets the default values.
200       *
201       * @return array An array of default values
202       */
203      public function getArgumentDefaults()
204      {
205          $values = array();
206          foreach ($this->arguments as $argument) {
207              $values[$argument->getName()] = $argument->getDefault();
208          }
209   
210          return $values;
211      }
212   
213      /**
214       * Sets the InputOption objects.
215       *
216       * @param InputOption[] $options An array of InputOption objects
217       */
218      public function setOptions($options = array())
219      {
220          $this->options = array();
221          $this->shortcuts = array();
222          $this->addOptions($options);
223      }
224   
225      /**
226       * Adds an array of InputOption objects.
227       *
228       * @param InputOption[] $options An array of InputOption objects
229       */
230      public function addOptions($options = array())
231      {
232          foreach ($options as $option) {
233              $this->addOption($option);
234          }
235      }
236   
237      /**
238       * Adds an InputOption object.
239       *
240       * @param InputOption $option An InputOption object
241       *
242       * @throws LogicException When option given already exist
243       */
244      public function addOption(InputOption $option)
245      {
246          if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
247              throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
248          }
249   
250          if ($option->getShortcut()) {
251              foreach (explode('|', $option->getShortcut()) as $shortcut) {
252                  if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
253                      throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
254                  }
255              }
256          }
257   
258          $this->options[$option->getName()] = $option;
259          if ($option->getShortcut()) {
260              foreach (explode('|', $option->getShortcut()) as $shortcut) {
261                  $this->shortcuts[$shortcut] = $option->getName();
262              }
263          }
264      }
265   
266      /**
267       * Returns an InputOption by name.
268       *
269       * @param string $name The InputOption name
270       *
271       * @return InputOption A InputOption object
272       *
273       * @throws InvalidArgumentException When option given doesn't exist
274       */
275      public function getOption($name)
276      {
277          if (!$this->hasOption($name)) {
278              throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
279          }
280   
281          return $this->options[$name];
282      }
283   
284      /**
285       * Returns true if an InputOption object exists by name.
286       *
287       * This method can't be used to check if the user included the option when
288       * executing the command (use getOption() instead).
289       *
290       * @param string $name The InputOption name
291       *
292       * @return bool true if the InputOption object exists, false otherwise
293       */
294      public function hasOption($name)
295      {
296          return isset($this->options[$name]);
297      }
298   
299      /**
300       * Gets the array of InputOption objects.
301       *
302       * @return InputOption[] An array of InputOption objects
303       */
304      public function getOptions()
305      {
306          return $this->options;
307      }
308   
309      /**
310       * Returns true if an InputOption object exists by shortcut.
311       *
312       * @param string $name The InputOption shortcut
313       *
314       * @return bool true if the InputOption object exists, false otherwise
315       */
316      public function hasShortcut($name)
317      {
318          return isset($this->shortcuts[$name]);
319      }
320   
321      /**
322       * Gets an InputOption by shortcut.
323       *
324       * @param string $shortcut the Shortcut name
325       *
326       * @return InputOption An InputOption object
327       */
328      public function getOptionForShortcut($shortcut)
329      {
330          return $this->getOption($this->shortcutToName($shortcut));
331      }
332   
333      /**
334       * Gets an array of default values.
335       *
336       * @return array An array of all default values
337       */
338      public function getOptionDefaults()
339      {
340          $values = array();
341          foreach ($this->options as $option) {
342              $values[$option->getName()] = $option->getDefault();
343          }
344   
345          return $values;
346      }
347   
348      /**
349       * Returns the InputOption name given a shortcut.
350       *
351       * @param string $shortcut The shortcut
352       *
353       * @return string The InputOption name
354       *
355       * @throws InvalidArgumentException When option given does not exist
356       */
357      private function shortcutToName($shortcut)
358      {
359          if (!isset($this->shortcuts[$shortcut])) {
360              throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
361          }
362   
363          return $this->shortcuts[$shortcut];
364      }
365   
366      /**
367       * Gets the synopsis.
368       *
369       * @param bool $short Whether to return the short version (with options folded) or not
370       *
371       * @return string The synopsis
372       */
373      public function getSynopsis($short = false)
374      {
375          $elements = array();
376   
377          if ($short && $this->getOptions()) {
378              $elements[] = '[options]';
379          } elseif (!$short) {
380              foreach ($this->getOptions() as $option) {
381                  $value = '';
382                  if ($option->acceptValue()) {
383                      $value = sprintf(
384                          ' %s%s%s',
385                          $option->isValueOptional() ? '[' : '',
386                          strtoupper($option->getName()),
387                          $option->isValueOptional() ? ']' : ''
388                      );
389                  }
390   
391                  $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
392                  $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
393              }
394          }
395   
396          if (count($elements) && $this->getArguments()) {
397              $elements[] = '[--]';
398          }
399   
400          foreach ($this->getArguments() as $argument) {
401              $element = '<'.$argument->getName().'>';
402              if (!$argument->isRequired()) {
403                  $element = '['.$element.']';
404              } elseif ($argument->isArray()) {
405                  $element = $element.' ('.$element.')';
406              }
407   
408              if ($argument->isArray()) {
409                  $element .= '...';
410              }
411   
412              $elements[] = $element;
413          }
414   
415          return implode(' ', $elements);
416      }
417   
418      /**
419       * Returns a textual representation of the InputDefinition.
420       *
421       * @return string A string representing the InputDefinition
422       *
423       * @deprecated since version 2.3, to be removed in 3.0.
424       */
425      public function asText()
426      {
427          @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0.', E_USER_DEPRECATED);
428   
429          $descriptor = new TextDescriptor();
430          $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
431          $descriptor->describe($output, $this, array('raw_output' => true));
432   
433          return $output->fetch();
434      }
435   
436      /**
437       * Returns an XML representation of the InputDefinition.
438       *
439       * @param bool $asDom Whether to return a DOM or an XML string
440       *
441       * @return string|\DOMDocument An XML string representing the InputDefinition
442       *
443       * @deprecated since version 2.3, to be removed in 3.0.
444       */
445      public function asXml($asDom = false)
446      {
447          @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0.', E_USER_DEPRECATED);
448   
449          $descriptor = new XmlDescriptor();
450   
451          if ($asDom) {
452              return $descriptor->getInputDefinitionDocument($this);
453          }
454   
455          $output = new BufferedOutput();
456          $descriptor->describe($output, $this);
457   
458          return $output->fetch();
459      }
460  }
461