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