MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
InputDefinition.php
Ir para a documentação deste ficheiro.
1<?php
2
4
5/*
6 * This file is part of the Symfony framework.
7 *
8 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
9 *
10 * This source file is subject to the MIT license that is bundled
11 * with this source code in the file LICENSE.
12 */
13
27{
28 protected $arguments;
29 protected $requiredCount;
30 protected $hasAnArrayArgument = false;
31 protected $hasOptional;
32 protected $options;
33 protected $shortcuts;
34
40 public function __construct(array $definition = array())
41 {
42 $this->setDefinition($definition);
43 }
44
45 public function setDefinition(array $definition)
46 {
47 $arguments = array();
48 $options = array();
49 foreach ($definition as $item) {
50 if ($item instanceof InputOption) {
51 $options[] = $item;
52 } else {
53 $arguments[] = $item;
54 }
55 }
56
58 $this->setOptions($options);
59 }
60
66 public function setArguments($arguments = array())
67 {
68 $this->arguments = array();
69 $this->requiredCount = 0;
70 $this->hasOptional = false;
71 $this->hasAnArrayArgument = false;
73 }
74
80 public function addArguments($arguments = array())
81 {
82 if (null !== $arguments) {
83 foreach ($arguments as $argument) {
84 $this->addArgument($argument);
85 }
86 }
87 }
88
96 public function addArgument(InputArgument $argument)
97 {
98 if (isset($this->arguments[$argument->getName()])) {
99 throw new \LogicException(sprintf('An argument with name "%s" already exist.', $argument->getName()));
100 }
101
102 if ($this->hasAnArrayArgument) {
103 throw new \LogicException('Cannot add an argument after an array argument.');
104 }
105
106 if ($argument->isRequired() && $this->hasOptional) {
107 throw new \LogicException('Cannot add a required argument after an optional one.');
108 }
109
110 if ($argument->isArray()) {
111 $this->hasAnArrayArgument = true;
112 }
113
114 if ($argument->isRequired()) {
116 } else {
117 $this->hasOptional = true;
118 }
119
120 $this->arguments[$argument->getName()] = $argument;
121 }
122
132 public function getArgument($name)
133 {
134 $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
135
136 if (!$this->hasArgument($name)) {
137 throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
138 }
139
140 return $arguments[$name];
141 }
142
150 public function hasArgument($name)
151 {
152 $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments;
153
154 return isset($arguments[$name]);
155 }
156
162 public function getArguments()
163 {
164 return $this->arguments;
165 }
166
172 public function getArgumentCount()
173 {
174 return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments);
175 }
176
182 public function getArgumentRequiredCount()
183 {
185 }
186
192 public function getArgumentDefaults()
193 {
194 $values = array();
195 foreach ($this->arguments as $argument) {
196 $values[$argument->getName()] = $argument->getDefault();
197 }
198
199 return $values;
200 }
201
207 public function setOptions($options = array())
208 {
209 $this->options = array();
210 $this->shortcuts = array();
211 $this->addOptions($options);
212 }
213
219 public function addOptions($options = array())
220 {
221 foreach ($options as $option) {
222 $this->addOption($option);
223 }
224 }
225
233 public function addOption(InputOption $option)
234 {
235 if (isset($this->options[$option->getName()])) {
236 throw new \LogicException(sprintf('An option named "%s" already exist.', $option->getName()));
237 } else if (isset($this->shortcuts[$option->getShortcut()])) {
238 throw new \LogicException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut()));
239 }
240
241 $this->options[$option->getName()] = $option;
242 if ($option->getShortcut()) {
243 $this->shortcuts[$option->getShortcut()] = $option->getName();
244 }
245 }
246
254 public function getOption($name)
255 {
256 if (!$this->hasOption($name)) {
257 throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
258 }
259
260 return $this->options[$name];
261 }
262
270 public function hasOption($name)
271 {
272 return isset($this->options[$name]);
273 }
274
280 public function getOptions()
281 {
282 return $this->options;
283 }
284
292 public function hasShortcut($name)
293 {
294 return isset($this->shortcuts[$name]);
295 }
296
302 public function getOptionForShortcut($shortcut)
303 {
304 return $this->getOption($this->shortcutToName($shortcut));
305 }
306
312 public function getOptionDefaults()
313 {
314 $values = array();
315 foreach ($this->options as $option) {
316 $values[$option->getName()] = $option->getDefault();
317 }
318
319 return $values;
320 }
321
331 protected function shortcutToName($shortcut)
332 {
333 if (!isset($this->shortcuts[$shortcut])) {
334 throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
335 }
336
337 return $this->shortcuts[$shortcut];
338 }
339
345 public function getSynopsis()
346 {
347 $elements = array();
348 foreach ($this->getOptions() as $option) {
349 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
350 $elements[] = sprintf('['.($option->isValueRequired() ? '%s--%s="..."' : ($option->isValueOptional() ? '%s--%s[="..."]' : '%s--%s')).']', $shortcut, $option->getName());
351 }
352
353 foreach ($this->getArguments() as $argument) {
354 $elements[] = sprintf($argument->isRequired() ? '%s' : '[%s]', $argument->getName().($argument->isArray() ? '1' : ''));
355
356 if ($argument->isArray()) {
357 $elements[] = sprintf('... [%sN]', $argument->getName());
358 }
359 }
360
361 return implode(' ', $elements);
362 }
363
369 public function asText()
370 {
371 // find the largest option or argument name
372 $max = 0;
373 foreach ($this->getOptions() as $option) {
374 $max = strlen($option->getName()) + 2 > $max ? strlen($option->getName()) + 2 : $max;
375 }
376 foreach ($this->getArguments() as $argument) {
377 $max = strlen($argument->getName()) > $max ? strlen($argument->getName()) : $max;
378 }
379 ++$max;
380
381 $text = array();
382
383 if ($this->getArguments()) {
384 $text[] = '<comment>Arguments:</comment>';
385 foreach ($this->getArguments() as $argument) {
386 if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
387 $default = sprintf('<comment> (default: %s)</comment>', is_array($argument->getDefault()) ? str_replace("\n", '', var_export($argument->getDefault(), true)): $argument->getDefault());
388 } else {
389 $default = '';
390 }
391
392 $text[] = sprintf(" <info>%-${max}s</info> %s%s", $argument->getName(), $argument->getDescription(), $default);
393 }
394
395 $text[] = '';
396 }
397
398 if ($this->getOptions()) {
399 $text[] = '<comment>Options:</comment>';
400
401 foreach ($this->getOptions() as $option) {
402 if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
403 $default = sprintf('<comment> (default: %s)</comment>', is_array($option->getDefault()) ? str_replace("\n", '', print_r($option->getDefault(), true)): $option->getDefault());
404 } else {
405 $default = '';
406 }
407
408 $multiple = $option->isArray() ? '<comment> (multiple values allowed)</comment>' : '';
409 $text[] = sprintf(' %-'.$max.'s %s%s%s%s', '<info>--'.$option->getName().'</info>', $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $option->getDescription(), $default, $multiple);
410 }
411
412 $text[] = '';
413 }
414
415 return implode("\n", $text);
416 }
417
425 public function asXml($asDom = false)
426 {
427 $dom = new \DOMDocument('1.0', 'UTF-8');
428 $dom->formatOutput = true;
429 $dom->appendChild($definitionXML = $dom->createElement('definition'));
430
431 $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments'));
432 foreach ($this->getArguments() as $argument) {
433 $argumentsXML->appendChild($argumentXML = $dom->createElement('argument'));
434 $argumentXML->setAttribute('name', $argument->getName());
435 $argumentXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0);
436 $argumentXML->setAttribute('is_array', $argument->isArray() ? 1 : 0);
437 $argumentXML->appendChild($descriptionXML = $dom->createElement('description'));
438 $descriptionXML->appendChild($dom->createTextNode($argument->getDescription()));
439
440 $argumentXML->appendChild($defaultsXML = $dom->createElement('defaults'));
441 $defaults = is_array($argument->getDefault()) ? $argument->getDefault() : ($argument->getDefault() ? array($argument->getDefault()) : array());
442 foreach ($defaults as $default) {
443 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
444 $defaultXML->appendChild($dom->createTextNode($default));
445 }
446 }
447
448 $definitionXML->appendChild($optionsXML = $dom->createElement('options'));
449 foreach ($this->getOptions() as $option) {
450 $optionsXML->appendChild($optionXML = $dom->createElement('option'));
451 $optionXML->setAttribute('name', '--'.$option->getName());
452 $optionXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : '');
453 $optionXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0);
454 $optionXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0);
455 $optionXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0);
456 $optionXML->appendChild($descriptionXML = $dom->createElement('description'));
457 $descriptionXML->appendChild($dom->createTextNode($option->getDescription()));
458
459 if ($option->acceptValue()) {
460 $optionXML->appendChild($defaultsXML = $dom->createElement('defaults'));
461 $defaults = is_array($option->getDefault()) ? $option->getDefault() : ($option->getDefault() ? array($option->getDefault()) : array());
462 foreach ($defaults as $default) {
463 $defaultsXML->appendChild($defaultXML = $dom->createElement('default'));
464 $defaultXML->appendChild($dom->createTextNode($default));
465 }
466 }
467 }
468
469 return $asDom ? $dom : $dom->saveXml();
470 }
471}