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