comparison vendor/symfony/console/Input/InputDefinition.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
17 /** 17 /**
18 * A InputDefinition represents a set of valid command line arguments and options. 18 * A InputDefinition represents a set of valid command line arguments and options.
19 * 19 *
20 * Usage: 20 * Usage:
21 * 21 *
22 * $definition = new InputDefinition(array( 22 * $definition = new InputDefinition([
23 * new InputArgument('name', InputArgument::REQUIRED), 23 * new InputArgument('name', InputArgument::REQUIRED),
24 * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED), 24 * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
25 * )); 25 * ]);
26 * 26 *
27 * @author Fabien Potencier <fabien@symfony.com> 27 * @author Fabien Potencier <fabien@symfony.com>
28 */ 28 */
29 class InputDefinition 29 class InputDefinition
30 { 30 {
36 private $shortcuts; 36 private $shortcuts;
37 37
38 /** 38 /**
39 * @param array $definition An array of InputArgument and InputOption instance 39 * @param array $definition An array of InputArgument and InputOption instance
40 */ 40 */
41 public function __construct(array $definition = array()) 41 public function __construct(array $definition = [])
42 { 42 {
43 $this->setDefinition($definition); 43 $this->setDefinition($definition);
44 } 44 }
45 45
46 /** 46 /**
47 * Sets the definition of the input. 47 * Sets the definition of the input.
48 */ 48 */
49 public function setDefinition(array $definition) 49 public function setDefinition(array $definition)
50 { 50 {
51 $arguments = array(); 51 $arguments = [];
52 $options = array(); 52 $options = [];
53 foreach ($definition as $item) { 53 foreach ($definition as $item) {
54 if ($item instanceof InputOption) { 54 if ($item instanceof InputOption) {
55 $options[] = $item; 55 $options[] = $item;
56 } else { 56 } else {
57 $arguments[] = $item; 57 $arguments[] = $item;
65 /** 65 /**
66 * Sets the InputArgument objects. 66 * Sets the InputArgument objects.
67 * 67 *
68 * @param InputArgument[] $arguments An array of InputArgument objects 68 * @param InputArgument[] $arguments An array of InputArgument objects
69 */ 69 */
70 public function setArguments($arguments = array()) 70 public function setArguments($arguments = [])
71 { 71 {
72 $this->arguments = array(); 72 $this->arguments = [];
73 $this->requiredCount = 0; 73 $this->requiredCount = 0;
74 $this->hasOptional = false; 74 $this->hasOptional = false;
75 $this->hasAnArrayArgument = false; 75 $this->hasAnArrayArgument = false;
76 $this->addArguments($arguments); 76 $this->addArguments($arguments);
77 } 77 }
79 /** 79 /**
80 * Adds an array of InputArgument objects. 80 * Adds an array of InputArgument objects.
81 * 81 *
82 * @param InputArgument[] $arguments An array of InputArgument objects 82 * @param InputArgument[] $arguments An array of InputArgument objects
83 */ 83 */
84 public function addArguments($arguments = array()) 84 public function addArguments($arguments = [])
85 { 85 {
86 if (null !== $arguments) { 86 if (null !== $arguments) {
87 foreach ($arguments as $argument) { 87 foreach ($arguments as $argument) {
88 $this->addArgument($argument); 88 $this->addArgument($argument);
89 } 89 }
133 { 133 {
134 if (!$this->hasArgument($name)) { 134 if (!$this->hasArgument($name)) {
135 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); 135 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
136 } 136 }
137 137
138 $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments; 138 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
139 139
140 return $arguments[$name]; 140 return $arguments[$name];
141 } 141 }
142 142
143 /** 143 /**
147 * 147 *
148 * @return bool true if the InputArgument object exists, false otherwise 148 * @return bool true if the InputArgument object exists, false otherwise
149 */ 149 */
150 public function hasArgument($name) 150 public function hasArgument($name)
151 { 151 {
152 $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments; 152 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
153 153
154 return isset($arguments[$name]); 154 return isset($arguments[$name]);
155 } 155 }
156 156
157 /** 157 /**
169 * 169 *
170 * @return int The number of InputArguments 170 * @return int The number of InputArguments
171 */ 171 */
172 public function getArgumentCount() 172 public function getArgumentCount()
173 { 173 {
174 return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments); 174 return $this->hasAnArrayArgument ? PHP_INT_MAX : \count($this->arguments);
175 } 175 }
176 176
177 /** 177 /**
178 * Returns the number of required InputArguments. 178 * Returns the number of required InputArguments.
179 * 179 *
189 * 189 *
190 * @return array An array of default values 190 * @return array An array of default values
191 */ 191 */
192 public function getArgumentDefaults() 192 public function getArgumentDefaults()
193 { 193 {
194 $values = array(); 194 $values = [];
195 foreach ($this->arguments as $argument) { 195 foreach ($this->arguments as $argument) {
196 $values[$argument->getName()] = $argument->getDefault(); 196 $values[$argument->getName()] = $argument->getDefault();
197 } 197 }
198 198
199 return $values; 199 return $values;
202 /** 202 /**
203 * Sets the InputOption objects. 203 * Sets the InputOption objects.
204 * 204 *
205 * @param InputOption[] $options An array of InputOption objects 205 * @param InputOption[] $options An array of InputOption objects
206 */ 206 */
207 public function setOptions($options = array()) 207 public function setOptions($options = [])
208 { 208 {
209 $this->options = array(); 209 $this->options = [];
210 $this->shortcuts = array(); 210 $this->shortcuts = [];
211 $this->addOptions($options); 211 $this->addOptions($options);
212 } 212 }
213 213
214 /** 214 /**
215 * Adds an array of InputOption objects. 215 * Adds an array of InputOption objects.
216 * 216 *
217 * @param InputOption[] $options An array of InputOption objects 217 * @param InputOption[] $options An array of InputOption objects
218 */ 218 */
219 public function addOptions($options = array()) 219 public function addOptions($options = [])
220 { 220 {
221 foreach ($options as $option) { 221 foreach ($options as $option) {
222 $this->addOption($option); 222 $this->addOption($option);
223 } 223 }
224 } 224 }
320 * 320 *
321 * @return array An array of all default values 321 * @return array An array of all default values
322 */ 322 */
323 public function getOptionDefaults() 323 public function getOptionDefaults()
324 { 324 {
325 $values = array(); 325 $values = [];
326 foreach ($this->options as $option) { 326 foreach ($this->options as $option) {
327 $values[$option->getName()] = $option->getDefault(); 327 $values[$option->getName()] = $option->getDefault();
328 } 328 }
329 329
330 return $values; 330 return $values;
355 * 355 *
356 * @return string The synopsis 356 * @return string The synopsis
357 */ 357 */
358 public function getSynopsis($short = false) 358 public function getSynopsis($short = false)
359 { 359 {
360 $elements = array(); 360 $elements = [];
361 361
362 if ($short && $this->getOptions()) { 362 if ($short && $this->getOptions()) {
363 $elements[] = '[options]'; 363 $elements[] = '[options]';
364 } elseif (!$short) { 364 } elseif (!$short) {
365 foreach ($this->getOptions() as $option) { 365 foreach ($this->getOptions() as $option) {
376 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : ''; 376 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
377 $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value); 377 $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
378 } 378 }
379 } 379 }
380 380
381 if (count($elements) && $this->getArguments()) { 381 if (\count($elements) && $this->getArguments()) {
382 $elements[] = '[--]'; 382 $elements[] = '[--]';
383 } 383 }
384 384
385 foreach ($this->getArguments() as $argument) { 385 foreach ($this->getArguments() as $argument) {
386 $element = '<'.$argument->getName().'>'; 386 $element = '<'.$argument->getName().'>';
387 if (!$argument->isRequired()) { 387 if (!$argument->isRequired()) {
388 $element = '['.$element.']'; 388 $element = '['.$element.']';
389 } elseif ($argument->isArray()) { 389 } elseif ($argument->isArray()) {
390 $element = $element.' ('.$element.')'; 390 $element .= ' ('.$element.')';
391 } 391 }
392 392
393 if ($argument->isArray()) { 393 if ($argument->isArray()) {
394 $element .= '...'; 394 $element .= '...';
395 } 395 }