Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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 use Symfony\Component\Console\Exception\InvalidArgumentException;
015 use Symfony\Component\Console\Exception\LogicException;
016
017 /**
018 * A InputDefinition represents a set of valid command line arguments and options.
019 *
020 * Usage:
021 *
022 * $definition = new InputDefinition([
023 * new InputArgument('name', InputArgument::REQUIRED),
024 * new InputOption('foo', 'f', InputOption::VALUE_REQUIRED),
025 * ]);
026 *
027 * @author Fabien Potencier <fabien@symfony.com>
028 */
029 class InputDefinition
030 {
031 private $arguments;
032 private $requiredCount;
033 private $hasAnArrayArgument = false;
034 private $hasOptional;
035 private $options;
036 private $shortcuts;
037
038 /**
039 * @param array $definition An array of InputArgument and InputOption instance
040 */
041 public function __construct(array $definition = [])
042 {
043 $this->setDefinition($definition);
044 }
045
046 /**
047 * Sets the definition of the input.
048 */
049 public function setDefinition(array $definition)
050 {
051 $arguments = [];
052 $options = [];
053 foreach ($definition as $item) {
054 if ($item instanceof InputOption) {
055 $options[] = $item;
056 } else {
057 $arguments[] = $item;
058 }
059 }
060
061 $this->setArguments($arguments);
062 $this->setOptions($options);
063 }
064
065 /**
066 * Sets the InputArgument objects.
067 *
068 * @param InputArgument[] $arguments An array of InputArgument objects
069 */
070 public function setArguments($arguments = [])
071 {
072 $this->arguments = [];
073 $this->requiredCount = 0;
074 $this->hasOptional = false;
075 $this->hasAnArrayArgument = false;
076 $this->addArguments($arguments);
077 }
078
079 /**
080 * Adds an array of InputArgument objects.
081 *
082 * @param InputArgument[] $arguments An array of InputArgument objects
083 */
084 public function addArguments($arguments = [])
085 {
086 if (null !== $arguments) {
087 foreach ($arguments as $argument) {
088 $this->addArgument($argument);
089 }
090 }
091 }
092
093 /**
094 * @throws LogicException When incorrect argument is given
095 */
096 public function addArgument(InputArgument $argument)
097 {
098 if (isset($this->arguments[$argument->getName()])) {
099 throw new LogicException(sprintf('An argument with name "%s" already exists.', $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()) {
115 ++$this->requiredCount;
116 } else {
117 $this->hasOptional = true;
118 }
119
120 $this->arguments[$argument->getName()] = $argument;
121 }
122
123 /**
124 * Returns an InputArgument by name or by position.
125 *
126 * @param string|int $name The InputArgument name or position
127 *
128 * @return InputArgument An InputArgument object
129 *
130 * @throws InvalidArgumentException When argument given doesn't exist
131 */
132 public function getArgument($name)
133 {
134 if (!$this->hasArgument($name)) {
135 throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
136 }
137
138 $arguments = \is_int($name) ? array_values($this->arguments) : $this->arguments;
139
140 return $arguments[$name];
141 }
142
143 /**
144 * Returns true if an InputArgument object exists by name or position.
145 *
146 * @param string|int $name The InputArgument name or position
147 *
148 * @return bool true if the InputArgument object exists, false otherwise
149 */
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
157 /**
158 * Gets the array of InputArgument objects.
159 *
160 * @return InputArgument[] An array of InputArgument objects
161 */
162 public function getArguments()
163 {
164 return $this->arguments;
165 }
166
167 /**
168 * Returns the number of InputArguments.
169 *
170 * @return int The number of InputArguments
171 */
172 public function getArgumentCount()
173 {
174 return $this->hasAnArrayArgument ? \PHP_INT_MAX : \count($this->arguments);
175 }
176
177 /**
178 * Returns the number of required InputArguments.
179 *
180 * @return int The number of required InputArguments
181 */
182 public function getArgumentRequiredCount()
183 {
184 return $this->requiredCount;
185 }
186
187 /**
188 * Gets the default values.
189 *
190 * @return array An array of default values
191 */
192 public function getArgumentDefaults()
193 {
194 $values = [];
195 foreach ($this->arguments as $argument) {
196 $values[$argument->getName()] = $argument->getDefault();
197 }
198
199 return $values;
200 }
201
202 /**
203 * Sets the InputOption objects.
204 *
205 * @param InputOption[] $options An array of InputOption objects
206 */
207 public function setOptions($options = [])
208 {
209 $this->options = [];
210 $this->shortcuts = [];
211 $this->addOptions($options);
212 }
213
214 /**
215 * Adds an array of InputOption objects.
216 *
217 * @param InputOption[] $options An array of InputOption objects
218 */
219 public function addOptions($options = [])
220 {
221 foreach ($options as $option) {
222 $this->addOption($option);
223 }
224 }
225
226 /**
227 * @throws LogicException When option given already exist
228 */
229 public function addOption(InputOption $option)
230 {
231 if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) {
232 throw new LogicException(sprintf('An option named "%s" already exists.', $option->getName()));
233 }
234
235 if ($option->getShortcut()) {
236 foreach (explode('|', $option->getShortcut()) as $shortcut) {
237 if (isset($this->shortcuts[$shortcut]) && !$option->equals($this->options[$this->shortcuts[$shortcut]])) {
238 throw new LogicException(sprintf('An option with shortcut "%s" already exists.', $shortcut));
239 }
240 }
241 }
242
243 $this->options[$option->getName()] = $option;
244 if ($option->getShortcut()) {
245 foreach (explode('|', $option->getShortcut()) as $shortcut) {
246 $this->shortcuts[$shortcut] = $option->getName();
247 }
248 }
249 }
250
251 /**
252 * Returns an InputOption by name.
253 *
254 * @param string $name The InputOption name
255 *
256 * @return InputOption A InputOption object
257 *
258 * @throws InvalidArgumentException When option given doesn't exist
259 */
260 public function getOption($name)
261 {
262 if (!$this->hasOption($name)) {
263 throw new InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
264 }
265
266 return $this->options[$name];
267 }
268
269 /**
270 * Returns true if an InputOption object exists by name.
271 *
272 * This method can't be used to check if the user included the option when
273 * executing the command (use getOption() instead).
274 *
275 * @param string $name The InputOption name
276 *
277 * @return bool true if the InputOption object exists, false otherwise
278 */
279 public function hasOption($name)
280 {
281 return isset($this->options[$name]);
282 }
283
284 /**
285 * Gets the array of InputOption objects.
286 *
287 * @return InputOption[] An array of InputOption objects
288 */
289 public function getOptions()
290 {
291 return $this->options;
292 }
293
294 /**
295 * Returns true if an InputOption object exists by shortcut.
296 *
297 * @param string $name The InputOption shortcut
298 *
299 * @return bool true if the InputOption object exists, false otherwise
300 */
301 public function hasShortcut($name)
302 {
303 return isset($this->shortcuts[$name]);
304 }
305
306 /**
307 * Gets an InputOption by shortcut.
308 *
309 * @param string $shortcut The Shortcut name
310 *
311 * @return InputOption An InputOption object
312 */
313 public function getOptionForShortcut($shortcut)
314 {
315 return $this->getOption($this->shortcutToName($shortcut));
316 }
317
318 /**
319 * Gets an array of default values.
320 *
321 * @return array An array of all default values
322 */
323 public function getOptionDefaults()
324 {
325 $values = [];
326 foreach ($this->options as $option) {
327 $values[$option->getName()] = $option->getDefault();
328 }
329
330 return $values;
331 }
332
333 /**
334 * Returns the InputOption name given a shortcut.
335 *
336 * @param string $shortcut The shortcut
337 *
338 * @return string The InputOption name
339 *
340 * @throws InvalidArgumentException When option given does not exist
341 *
342 * @internal
343 */
344 public function shortcutToName($shortcut)
345 {
346 if (!isset($this->shortcuts[$shortcut])) {
347 throw new InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
348 }
349
350 return $this->shortcuts[$shortcut];
351 }
352
353 /**
354 * Gets the synopsis.
355 *
356 * @param bool $short Whether to return the short version (with options folded) or not
357 *
358 * @return string The synopsis
359 */
360 public function getSynopsis($short = false)
361 {
362 $elements = [];
363
364 if ($short && $this->getOptions()) {
365 $elements[] = '[options]';
366 } elseif (!$short) {
367 foreach ($this->getOptions() as $option) {
368 $value = '';
369 if ($option->acceptValue()) {
370 $value = sprintf(
371 ' %s%s%s',
372 $option->isValueOptional() ? '[' : '',
373 strtoupper($option->getName()),
374 $option->isValueOptional() ? ']' : ''
375 );
376 }
377
378 $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : '';
379 $elements[] = sprintf('[%s--%s%s]', $shortcut, $option->getName(), $value);
380 }
381 }
382
383 if (\count($elements) && $this->getArguments()) {
384 $elements[] = '[--]';
385 }
386
387 foreach ($this->getArguments() as $argument) {
388 $element = '<'.$argument->getName().'>';
389 if (!$argument->isRequired()) {
390 $element = '['.$element.']';
391 } elseif ($argument->isArray()) {
392 $element .= ' ('.$element.')';
393 }
394
395 if ($argument->isArray()) {
396 $element .= '...';
397 }
398
399 $elements[] = $element;
400 }
401
402 return implode(' ', $elements);
403 }
404 }
405