TYPO3  7.6
InputDefinition.php
Go to the documentation of this file.
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 
17 
33 {
34  private $arguments;
35  private $requiredCount;
36  private $hasAnArrayArgument = false;
37  private $hasOptional;
38  private $options;
39  private $shortcuts;
40 
48  public function __construct(array $definition = array())
49  {
50  $this->setDefinition($definition);
51  }
52 
60  public function setDefinition(array $definition)
61  {
62  $arguments = array();
63  $options = array();
64  foreach ($definition as $item) {
65  if ($item instanceof InputOption) {
66  $options[] = $item;
67  } else {
68  $arguments[] = $item;
69  }
70  }
71 
72  $this->setArguments($arguments);
73  $this->setOptions($options);
74  }
75 
83  public function setArguments($arguments = array())
84  {
85  $this->arguments = array();
86  $this->requiredCount = 0;
87  $this->hasOptional = false;
88  $this->hasAnArrayArgument = false;
89  $this->addArguments($arguments);
90  }
91 
99  public function addArguments($arguments = array())
100  {
101  if (null !== $arguments) {
102  foreach ($arguments as $argument) {
103  $this->addArgument($argument);
104  }
105  }
106  }
107 
117  public function addArgument(InputArgument $argument)
118  {
119  if (isset($this->arguments[$argument->getName()])) {
120  throw new \LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName()));
121  }
122 
123  if ($this->hasAnArrayArgument) {
124  throw new \LogicException('Cannot add an argument after an array argument.');
125  }
126 
127  if ($argument->isRequired() && $this->hasOptional) {
128  throw new \LogicException('Cannot add a required argument after an optional one.');
129  }
130 
131  if ($argument->isArray()) {
132  $this->hasAnArrayArgument = true;
133  }
134 
135  if ($argument->isRequired()) {
137  } else {
138  $this->hasOptional = true;
139  }
140 
141  $this->arguments[$argument->getName()] = $argument;
142  }
143 
155  public function getArgument($name)
156  {
157  if (!$this->hasArgument($name)) {
158  throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
159  }
160 
161  $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
162 
163  return $arguments[$name];
164  }
165 
175  public function hasArgument($name)
176  {
177  $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
178 
179  return isset($arguments[$name]);
180  }
181 
189  public function getArguments()
190  {
191  return $this->arguments;
192  }
193 
199  public function getArgumentCount()
200  {
201  return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
202  }
203 
209  public function getArgumentRequiredCount()
210  {
211  return $this->requiredCount;
212  }
213 
219  public function getArgumentDefaults()
220  {
221  $values = array();
222  foreach ($this->arguments as $argument) {
223  $values[$argument->getName()] = $argument->getDefault();
224  }
225 
226  return $values;
227  }
228 
236  public function setOptions($options = array())
237  {
238  $this->options = array();
239  $this->shortcuts = array();
240  $this->addOptions($options);
241  }
242 
250  public function addOptions($options = array())
251  {
252  foreach ($options as $option) {
253  $this->addOption($option);
254  }
255  }
256 
266  public function addOption(InputOption $option)
267  {
268  if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
269  throw new \LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
270  }
271 
272  if ($option->getShortcut()) {
273  foreach (explode('|', $option->getShortcut()) as $shortcut) {
274  if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
275  throw new \LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
276  }
277  }
278  }
279 
280  $this->options[$option->getName()] = $option;
281  if ($option->getShortcut()) {
282  foreach (explode('|', $option->getShortcut()) as $shortcut) {
283  $this->shortcuts[$shortcut] = $option->getName();
284  }
285  }
286  }
287 
299  public function getOption($name)
300  {
301  if (!$this->hasOption($name)) {
302  throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
303  }
304 
305  return $this->options[$name];
306  }
307 
317  public function hasOption($name)
318  {
319  return isset($this->options[$name]);
320  }
321 
329  public function getOptions()
330  {
331  return $this->options;
332  }
333 
341  public function hasShortcut($name)
342  {
343  return isset($this->shortcuts[$name]);
344  }
345 
353  public function getOptionForShortcut($shortcut)
354  {
355  return $this->getOption($this->shortcutToName($shortcut));
356  }
357 
363  public function getOptionDefaults()
364  {
365  $values = array();
366  foreach ($this->options as $option) {
367  $values[$option->getName()] = $option->getDefault();
368  }
369 
370  return $values;
371  }
372 
382  private function shortcutToName($shortcut)
383  {
384  if (!isset($this->shortcuts[$shortcut])) {
385  throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
386  }
387 
388  return $this->shortcuts[$shortcut];
389  }
390 
398  public function getSynopsis($short = false)
399  {
400  $elements = array();
401 
402  if ($short && $this->getOptions()) {
403  $elements[] = '[options]';
404  } elseif (!$short) {
405  foreach ($this->getOptions() as $option) {
406  $value = '';
407  if ($option->acceptValue()) {
408  $value = sprintf(
409  ' %s%s%s',
410  $option->isValueOptional() ? '[' : '',
411  strtoupper($option->getName()),
412  $option->isValueOptional() ? ']' : ''
413  );
414  }
415 
416  $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
417  $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
418  }
419  }
420 
421  if (count($elements) && $this->getArguments()) {
422  $elements[] = '[--]';
423  }
424 
425  foreach ($this->getArguments() as $argument) {
426  $element = '<'.$argument->getName().'>';
427  if (!$argument->isRequired()) {
428  $element = '['.$element.']';
429  } elseif ($argument->isArray()) {
430  $element = $element.' ('.$element.')';
431  }
432 
433  if ($argument->isArray()) {
434  $element .= '...';
435  }
436 
437  $elements[] = $element;
438  }
439 
440  return implode(' ', $elements);
441  }
442 
450  public function asText()
451  {
452  @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0.', E_USER_DEPRECATED);
453 
454  $descriptor = new TextDescriptor();
456  $descriptor->describe($output, $this, array('raw_output' => true));
457 
458  return $output->fetch();
459  }
460 
470  public function asXml($asDom = false)
471  {
472  @trigger_error('The '.__METHOD__.' method is deprecated since version 2.3 and will be removed in 3.0.', E_USER_DEPRECATED);
473 
474  $descriptor = new XmlDescriptor();
475 
476  if ($asDom) {
477  return $descriptor->getInputDefinitionDocument($this);
478  }
479 
480  $output = new BufferedOutput();
481  $descriptor->describe($output, $this);
482 
483  return $output->fetch();
484  }
485 }