[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
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 14 use Symfony\Component\Console\Descriptor\TextDescriptor; 15 use Symfony\Component\Console\Descriptor\XmlDescriptor; 16 17 /** 18 * A InputDefinition represents a set of valid command line arguments and options. 19 * 20 * Usage: 21 * 22 * $definition = new InputDefinition(array( 23 * new InputArgument('name', InputArgument::REQUIRED), 24 * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED), 25 * )); 26 * 27 * @author Fabien Potencier <fabien@symfony.com> 28 */ 29 class InputDefinition 30 { 31 private $arguments; 32 private $requiredCount; 33 private $hasAnArrayArgument = false; 34 private $hasOptional; 35 private $options; 36 private $shortcuts; 37 38 /** 39 * Constructor. 40 * 41 * @param array $definition An array of InputArgument and InputOption instance 42 */ 43 public function __construct(array $definition = array()) 44 { 45 $this->setDefinition($definition); 46 } 47 48 /** 49 * Sets the definition of the input. 50 * 51 * @param array $definition The definition array 52 */ 53 public function setDefinition(array $definition) 54 { 55 $arguments = array(); 56 $options = array(); 57 foreach ($definition as $item) { 58 if ($item instanceof InputOption) { 59 $options[] = $item; 60 } else { 61 $arguments[] = $item; 62 } 63 } 64 65 $this->setArguments($arguments); 66 $this->setOptions($options); 67 } 68 69 /** 70 * Sets the InputArgument objects. 71 * 72 * @param InputArgument[] $arguments An array of InputArgument objects 73 */ 74 public function setArguments($arguments = array()) 75 { 76 $this->arguments = array(); 77 $this->requiredCount = 0; 78 $this->hasOptional = false; 79 $this->hasAnArrayArgument = false; 80 $this->addArguments($arguments); 81 } 82 83 /** 84 * Adds an array of InputArgument objects. 85 * 86 * @param InputArgument[] $arguments An array of InputArgument objects 87 */ 88 public function addArguments($arguments = array()) 89 { 90 if (null !== $arguments) { 91 foreach ($arguments as $argument) { 92 $this->addArgument($argument); 93 } 94 } 95 } 96 97 /** 98 * Adds an InputArgument object. 99 * 100 * @param InputArgument $argument An InputArgument object 101 * 102 * @throws \LogicException When incorrect argument is given 103 */ 104 public function addArgument(InputArgument $argument) 105 { 106 if (isset($this->arguments[$argument->getName()])) { 107 throw new \LogicException(sprintf('An argument with name "%s" already exists.', $argument->getName())); 108 } 109 110 if ($this->hasAnArrayArgument) { 111 throw new \LogicException('Cannot add an argument after an array argument.'); 112 } 113 114 if ($argument->isRequired() && $this->hasOptional) { 115 throw new \LogicException('Cannot add a required argument after an optional one.'); 116 } 117 118 if ($argument->isArray()) { 119 $this->hasAnArrayArgument = true; 120 } 121 122 if ($argument->isRequired()) { 123 ++$this->requiredCount; 124 } else { 125 $this->hasOptional = true; 126 } 127 128 $this->arguments[$argument->getName()] = $argument; 129 } 130 131 /** 132 * Returns an InputArgument by name or by position. 133 * 134 * @param string|int $name The InputArgument name or position 135 * 136 * @return InputArgument An InputArgument object 137 * 138 * @throws \InvalidArgumentException When argument given doesn't exist 139 */ 140 public function getArgument($name) 141 { 142 if (!$this->hasArgument($name)) { 143 throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); 144 } 145 146 $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments; 147 148 return $arguments[$name]; 149 } 150 151 /** 152 * Returns true if an InputArgument object exists by name or position. 153 * 154 * @param string|int $name The InputArgument name or position 155 * 156 * @return bool true if the InputArgument object exists, false otherwise 157 */ 158 public function hasArgument($name) 159 { 160 $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments; 161 162 return isset($arguments[$name]); 163 } 164 165 /** 166 * Gets the array of InputArgument objects. 167 * 168 * @return InputArgument[] An array of InputArgument objects 169 */ 170 public function getArguments() 171 { 172 return $this->arguments; 173 } 174 175 /** 176 * Returns the number of InputArguments. 177 * 178 * @return int The number of InputArguments 179 */ 180 public function getArgumentCount() 181 { 182 return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments); 183 } 184 185 /** 186 * Returns the number of required InputArguments. 187 * 188 * @return int The number of required InputArguments 189 */ 190 public function getArgumentRequiredCount() 191 { 192 return $this->requiredCount; 193 } 194 195 /** 196 * Gets the default values. 197 * 198 * @return array An array of default values 199 */ 200 public function getArgumentDefaults() 201 { 202 $values = array(); 203 foreach ($this->arguments as $argument) { 204 $values[$argument->getName()] = $argument->getDefault(); 205 } 206 207 return $values; 208 } 209 210 /** 211 * Sets the InputOption objects. 212 * 213 * @param InputOption[] $options An array of InputOption objects 214 */ 215 public function setOptions($options = array()) 216 { 217 $this->options = array(); 218 $this->shortcuts = array(); 219 $this->addOptions($options); 220 } 221 222 /** 223 * Adds an array of InputOption objects. 224 * 225 * @param InputOption[] $options An array of InputOption objects 226 */ 227 public function addOptions($options = array()) 228 { 229 foreach ($options as $option) { 230 $this->addOption($option); 231 } 232 } 233 234 /** 235 * Adds an InputOption object. 236 * 237 * @param InputOption $option An InputOption object 238 * 239 * @throws \LogicException When option given already exist 240 */ 241 public function addOption(InputOption $option) 242 { 243 if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) { 244 throw new \LogicException(sprintf('An option named "%s" already exists.', $option->getName())); 245 } 246 247 if ($option->getShortcut()) { 248 foreach (explode('|', $option->getShortcut()) as $shortcut) { 249 if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) { 250 throw new \LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut)); 251 } 252 } 253 } 254 255 $this->options[$option->getName()] = $option; 256 if ($option->getShortcut()) { 257 foreach (explode('|', $option->getShortcut()) as $shortcut) { 258 $this->shortcuts[$shortcut] = $option->getName(); 259 } 260 } 261 } 262 263 /** 264 * Returns an InputOption by name. 265 * 266 * @param string $name The InputOption name 267 * 268 * @return InputOption A InputOption object 269 * 270 * @throws \InvalidArgumentException When option given doesn't exist 271 */ 272 public function getOption($name) 273 { 274 if (!$this->hasOption($name)) { 275 throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name)); 276 } 277 278 return $this->options[$name]; 279 } 280 281 /** 282 * Returns true if an InputOption object exists by name. 283 * 284 * @param string $name The InputOption name 285 * 286 * @return bool true if the InputOption object exists, false otherwise 287 */ 288 public function hasOption($name) 289 { 290 return isset($this->options[$name]); 291 } 292 293 /** 294 * Gets the array of InputOption objects. 295 * 296 * @return InputOption[] An array of InputOption objects 297 */ 298 public function getOptions() 299 { 300 return $this->options; 301 } 302 303 /** 304 * Returns true if an InputOption object exists by shortcut. 305 * 306 * @param string $name The InputOption shortcut 307 * 308 * @return bool true if the InputOption object exists, false otherwise 309 */ 310 public function hasShortcut($name) 311 { 312 return isset($this->shortcuts[$name]); 313 } 314 315 /** 316 * Gets an InputOption by shortcut. 317 * 318 * @param string $shortcut the Shortcut name 319 * 320 * @return InputOption An InputOption object 321 */ 322 public function getOptionForShortcut($shortcut) 323 { 324 return $this->getOption($this->shortcutToName($shortcut)); 325 } 326 327 /** 328 * Gets an array of default values. 329 * 330 * @return array An array of all default values 331 */ 332 public function getOptionDefaults() 333 { 334 $values = array(); 335 foreach ($this->options as $option) { 336 $values[$option->getName()] = $option->getDefault(); 337 } 338 339 return $values; 340 } 341 342 /** 343 * Returns the InputOption name given a shortcut. 344 * 345 * @param string $shortcut The shortcut 346 * 347 * @return string The InputOption name 348 * 349 * @throws \InvalidArgumentException When option given does not exist 350 */ 351 private function shortcutToName($shortcut) 352 { 353 if (!isset($this->shortcuts[$shortcut])) { 354 throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut)); 355 } 356 357 return $this->shortcuts[$shortcut]; 358 } 359 360 /** 361 * Gets the synopsis. 362 * 363 * @return string The synopsis 364 */ 365 public function getSynopsis() 366 { 367 $elements = array(); 368 foreach ($this->getOptions() as $option) { 369 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : ''; 370 $elements[] = sprintf('['.($option->isValueRequired() ? '%s--%s="..."' : ($option->isValueOptional() ? '%s--%s[="..."]' : '%s--%s')).']', $shortcut, $option->getName()); 371 } 372 373 foreach ($this->getArguments() as $argument) { 374 $elements[] = sprintf($argument->isRequired() ? '%s' : '[%s]', $argument->getName().($argument->isArray() ? '1' : '')); 375 376 if ($argument->isArray()) { 377 $elements[] = sprintf('... [%sN]', $argument->getName()); 378 } 379 } 380 381 return implode(' ', $elements); 382 } 383 384 /** 385 * Returns a textual representation of the InputDefinition. 386 * 387 * @return string A string representing the InputDefinition 388 * 389 * @deprecated Deprecated since version 2.3, to be removed in 3.0. 390 */ 391 public function asText() 392 { 393 $descriptor = new TextDescriptor(); 394 395 return $descriptor->describe($this); 396 } 397 398 /** 399 * Returns an XML representation of the InputDefinition. 400 * 401 * @param bool $asDom Whether to return a DOM or an XML string 402 * 403 * @return string|\DOMDocument An XML string representing the InputDefinition 404 * 405 * @deprecated Deprecated since version 2.3, to be removed in 3.0. 406 */ 407 public function asXml($asDom = false) 408 { 409 $descriptor = new XmlDescriptor(); 410 411 return $descriptor->describe($this, array('as_dom' => $asDom)); 412 } 413 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |