|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/console package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[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 Webmozart\Console\Api\Config; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
|
16
|
|
|
use Webmozart\Assert\Assert; |
|
17
|
|
|
use Webmozart\Console\Api\Args\ArgsParser; |
|
18
|
|
|
use Webmozart\Console\Api\Args\Format\ArgsFormatBuilder; |
|
19
|
|
|
use Webmozart\Console\Api\Args\Format\Argument; |
|
20
|
|
|
use Webmozart\Console\Api\Args\Format\Option; |
|
21
|
|
|
use Webmozart\Console\Args\DefaultArgsParser; |
|
22
|
|
|
use Webmozart\Console\Handler\NullHandler; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Implements methods shared by all configurations. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class Config |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var ArgsFormatBuilder |
|
35
|
|
|
*/ |
|
36
|
|
|
private $formatBuilder; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var HelperSet |
|
40
|
|
|
*/ |
|
41
|
|
|
private $helperSet; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ArgsParser |
|
45
|
|
|
*/ |
|
46
|
|
|
private $argsParser; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
private $lenientArgsParsing; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var object|callable |
|
55
|
|
|
*/ |
|
56
|
|
|
private $handler; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
private $handlerMethod; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Creates a new configuration. |
|
65
|
|
|
*/ |
|
66
|
438 |
|
public function __construct() |
|
67
|
|
|
{ |
|
68
|
438 |
|
$this->formatBuilder = new ArgsFormatBuilder(); |
|
69
|
|
|
|
|
70
|
438 |
|
$this->configure(); |
|
71
|
438 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the configured arguments. |
|
75
|
|
|
* |
|
76
|
|
|
* Read {@link Argument} for a more detailed description of arguments. |
|
77
|
|
|
* |
|
78
|
|
|
* @return Argument[] The configured arguments. |
|
79
|
|
|
* |
|
80
|
|
|
* @see addArgument() |
|
81
|
|
|
*/ |
|
82
|
251 |
|
public function getArguments() |
|
83
|
|
|
{ |
|
84
|
251 |
|
return $this->formatBuilder->getArguments(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Adds an argument. |
|
89
|
|
|
* |
|
90
|
|
|
* Read {@link Argument} for a more detailed description of console |
|
91
|
|
|
* arguments. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $name The argument name. |
|
94
|
|
|
* @param int $flags A bitwise combination of the flag constants in |
|
95
|
|
|
* the {@link Argument} class. |
|
96
|
|
|
* @param string $description A one-line description of the argument. |
|
|
|
|
|
|
97
|
|
|
* @param mixed $default The default value. Must be `null` if the |
|
98
|
|
|
* flags contain {@link Argument::REQUIRED}. |
|
99
|
|
|
* |
|
100
|
|
|
* @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. |
|
101
|
|
|
* |
|
102
|
|
|
* @see getArguments() |
|
103
|
|
|
*/ |
|
104
|
98 |
|
public function addArgument($name, $flags = 0, $description = null, $default = null) |
|
105
|
|
|
{ |
|
106
|
98 |
|
$this->formatBuilder->addArgument(new Argument($name, $flags, $description, $default)); |
|
107
|
|
|
|
|
108
|
98 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns the configured options. |
|
113
|
|
|
* |
|
114
|
|
|
* Read {@link Option} for a more detailed description of console options. |
|
115
|
|
|
* |
|
116
|
|
|
* @return Option[] The configured options. |
|
117
|
|
|
* |
|
118
|
|
|
* @see addOption() |
|
119
|
|
|
*/ |
|
120
|
251 |
|
public function getOptions() |
|
121
|
|
|
{ |
|
122
|
251 |
|
return $this->formatBuilder->getOptions(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Adds an option. |
|
127
|
|
|
* |
|
128
|
|
|
* Read {@link Option} for a more detailed description of console options. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $longName The long option name. |
|
131
|
|
|
* @param string $shortName The short option name. Can be `null`. |
|
|
|
|
|
|
132
|
|
|
* @param int $flags A bitwise combination of the flag constants in |
|
133
|
|
|
* the {@link Option} class. |
|
134
|
|
|
* @param string $description A one-line description of the option. |
|
|
|
|
|
|
135
|
|
|
* @param mixed $default The default value. Must be `null` if the |
|
136
|
|
|
* flags contain {@link Option::REQUIRED_VALUE}. |
|
137
|
|
|
* @param string $valueName The name of the value to be used in usage |
|
138
|
|
|
* examples of the option. |
|
139
|
|
|
* |
|
140
|
|
|
* @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. |
|
141
|
|
|
* |
|
142
|
|
|
* @see getOptions() |
|
143
|
|
|
*/ |
|
144
|
103 |
|
public function addOption($longName, $shortName = null, $flags = 0, $description = null, $default = null, $valueName = '...') |
|
145
|
|
|
{ |
|
146
|
103 |
|
$this->formatBuilder->addOption(new Option($longName, $shortName, $flags, $description, $default, $valueName)); |
|
147
|
|
|
|
|
148
|
103 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns the configured helper set. |
|
153
|
|
|
* |
|
154
|
|
|
* @return HelperSet The helper set. |
|
155
|
|
|
* |
|
156
|
|
|
* @see setHelperSet() |
|
157
|
|
|
*/ |
|
158
|
19 |
|
public function getHelperSet() |
|
159
|
|
|
{ |
|
160
|
19 |
|
if (!$this->helperSet) { |
|
161
|
18 |
|
return $this->getDefaultHelperSet(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
5 |
|
return $this->helperSet; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Sets the used helper set. |
|
169
|
|
|
* |
|
170
|
|
|
* @param HelperSet $helperSet The helper set. |
|
171
|
|
|
* |
|
172
|
|
|
* @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. |
|
173
|
|
|
* |
|
174
|
|
|
* @see getHelperSet() |
|
175
|
|
|
*/ |
|
176
|
5 |
|
public function setHelperSet(HelperSet $helperSet) |
|
177
|
|
|
{ |
|
178
|
5 |
|
$this->helperSet = $helperSet; |
|
179
|
|
|
|
|
180
|
5 |
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns the configured argument parser. |
|
185
|
|
|
* |
|
186
|
|
|
* @return ArgsParser The argument parser. |
|
187
|
|
|
* |
|
188
|
|
|
* @see setArgsParser() |
|
189
|
|
|
*/ |
|
190
|
272 |
|
public function getArgsParser() |
|
191
|
|
|
{ |
|
192
|
272 |
|
if (!$this->argsParser) { |
|
193
|
268 |
|
return $this->getDefaultArgsParser(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
6 |
|
return $this->argsParser; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Sets the used argument parser. |
|
201
|
|
|
* |
|
202
|
|
|
* @param ArgsParser $argsParser The argument parser. |
|
203
|
|
|
* |
|
204
|
|
|
* @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. |
|
205
|
|
|
* |
|
206
|
|
|
* @see getArgsParser() |
|
207
|
|
|
*/ |
|
208
|
6 |
|
public function setArgsParser(ArgsParser $argsParser) |
|
209
|
|
|
{ |
|
210
|
6 |
|
$this->argsParser = $argsParser; |
|
211
|
|
|
|
|
212
|
6 |
|
return $this; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Returns whether lenient argument parsing is enabled. |
|
217
|
|
|
* |
|
218
|
|
|
* When lenient argument parsing is enabled, the argument parser will not |
|
219
|
|
|
* fail if the console arguments contain invalid or missing arguments. |
|
220
|
|
|
* |
|
221
|
|
|
* @return bool Returns `true` if lenient parsing is enabled and `false` |
|
222
|
|
|
* otherwise. |
|
223
|
|
|
*/ |
|
224
|
263 |
|
public function isLenientArgsParsingEnabled() |
|
225
|
|
|
{ |
|
226
|
263 |
|
if (null === $this->lenientArgsParsing) { |
|
227
|
261 |
|
return $this->getDefaultLenientArgsParsing(); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
5 |
|
return $this->lenientArgsParsing; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Enables lenient argument parsing. |
|
235
|
|
|
* |
|
236
|
|
|
* When lenient argument parsing is enabled, the argument parser will not |
|
237
|
|
|
* fail if the console arguments contain invalid or missing arguments. |
|
238
|
|
|
* |
|
239
|
|
|
* Lenient argument parsing is disabled by default. |
|
240
|
|
|
* |
|
241
|
|
|
* @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. |
|
242
|
|
|
* |
|
243
|
|
|
* @see disableLenientArgsParsing() |
|
244
|
|
|
*/ |
|
245
|
5 |
|
public function enableLenientArgsParsing() |
|
246
|
|
|
{ |
|
247
|
5 |
|
$this->lenientArgsParsing = true; |
|
248
|
|
|
|
|
249
|
5 |
|
return $this; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Disables lenient argument parsing. |
|
254
|
|
|
* |
|
255
|
|
|
* When lenient argument parsing is enabled, the argument parser will not |
|
256
|
|
|
* fail if the console arguments contain invalid or missing arguments. |
|
257
|
|
|
* |
|
258
|
|
|
* Lenient argument parsing is disabled by default. |
|
259
|
|
|
* |
|
260
|
|
|
* @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. |
|
261
|
|
|
* |
|
262
|
|
|
* @see enableLenientArgsParsing() |
|
263
|
|
|
*/ |
|
264
|
3 |
|
public function disableLenientArgsParsing() |
|
265
|
|
|
{ |
|
266
|
3 |
|
$this->lenientArgsParsing = false; |
|
267
|
|
|
|
|
268
|
3 |
|
return $this; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Returns the command handler to execute when a command is run. |
|
273
|
|
|
* |
|
274
|
|
|
* @return object The command handler. |
|
275
|
|
|
* |
|
276
|
|
|
* @see setHandler() |
|
277
|
|
|
*/ |
|
278
|
50 |
|
public function getHandler() |
|
279
|
|
|
{ |
|
280
|
50 |
|
if (!$this->handler) { |
|
281
|
3 |
|
return $this->getDefaultHandler(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
49 |
|
if (is_callable($this->handler)) { |
|
285
|
12 |
|
$this->handler = call_user_func($this->handler); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
49 |
|
return $this->handler; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* Sets the command handler to execute when a command is run. |
|
293
|
|
|
* |
|
294
|
|
|
* You can pass: |
|
295
|
|
|
* |
|
296
|
|
|
* * An object with handler methods. |
|
297
|
|
|
* * A callable that receives a {@link Command} and returns an object with |
|
298
|
|
|
* handler methods. |
|
299
|
|
|
* |
|
300
|
|
|
* The name of the executed handler method can be configured with |
|
301
|
|
|
* {@link setHandlerMethod()}. By default, the method `handle()` is |
|
302
|
|
|
* executed. |
|
303
|
|
|
* |
|
304
|
|
|
* @param object|callback $handler The command handler or the callable |
|
305
|
|
|
* creating a new command handler on demand. |
|
306
|
|
|
* |
|
307
|
|
|
* @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. |
|
308
|
|
|
* |
|
309
|
|
|
* @see getHandler() |
|
310
|
|
|
*/ |
|
311
|
102 |
|
public function setHandler($handler) |
|
312
|
|
|
{ |
|
313
|
102 |
|
if (!is_object($handler) && !is_callable($handler)) { |
|
314
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
315
|
1 |
|
'Expected an object or a callable. Got: %s', |
|
316
|
1 |
|
is_object($handler) ? get_class($handler) : gettype($handler) |
|
317
|
|
|
)); |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
101 |
|
$this->handler = $handler; |
|
321
|
|
|
|
|
322
|
101 |
|
return $this; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* Returns the method of the command handler that should be executed when |
|
327
|
|
|
* the configured command is run. |
|
328
|
|
|
* |
|
329
|
|
|
* @return string The method name. |
|
330
|
|
|
* |
|
331
|
|
|
* @see setHandlerMethod() |
|
332
|
|
|
*/ |
|
333
|
49 |
|
public function getHandlerMethod() |
|
334
|
|
|
{ |
|
335
|
49 |
|
if (!$this->handlerMethod) { |
|
336
|
47 |
|
return $this->getDefaultHandlerMethod(); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
4 |
|
return $this->handlerMethod; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Sets the method of the command handler that should be executed when the |
|
344
|
|
|
* configured command is run. |
|
345
|
|
|
* |
|
346
|
|
|
* The method receives three arguments: |
|
347
|
|
|
* |
|
348
|
|
|
* * {@link Args} `$args`: The console arguments. |
|
349
|
|
|
* * {@link IO} `$io`: The I/O. |
|
350
|
|
|
* * {@link Command} `$command`: The executed command. |
|
351
|
|
|
* |
|
352
|
|
|
* @param string $handlerMethod The method name. |
|
353
|
|
|
* |
|
354
|
|
|
* @return ApplicationConfig|CommandConfig|SubCommandConfig|OptionCommandConfig The current instance. |
|
355
|
|
|
* |
|
356
|
|
|
* @see getHandlerMethod() |
|
357
|
|
|
*/ |
|
358
|
7 |
|
public function setHandlerMethod($handlerMethod) |
|
359
|
|
|
{ |
|
360
|
7 |
|
Assert::string($handlerMethod, 'The handler method must be a string. Got: %s'); |
|
361
|
5 |
|
Assert::notEmpty($handlerMethod, 'The handler method must not be empty.'); |
|
362
|
|
|
|
|
363
|
4 |
|
$this->handlerMethod = $handlerMethod; |
|
364
|
|
|
|
|
365
|
4 |
|
return $this; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Returns the helper set to use if none is set. |
|
370
|
|
|
* |
|
371
|
|
|
* @return HelperSet The default helper set. |
|
372
|
|
|
*/ |
|
373
|
15 |
|
protected function getDefaultHelperSet() |
|
374
|
|
|
{ |
|
375
|
15 |
|
return new HelperSet(); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
/** |
|
379
|
|
|
* Returns the command handler to use if none is set. |
|
380
|
|
|
* |
|
381
|
|
|
* @return object The default command handler. |
|
|
|
|
|
|
382
|
|
|
*/ |
|
383
|
1 |
|
protected function getDefaultHandler() |
|
384
|
|
|
{ |
|
385
|
1 |
|
return new NullHandler(); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
/** |
|
389
|
|
|
* Returns the handler method to use if none is set. |
|
390
|
|
|
* |
|
391
|
|
|
* @return string The default handler method. |
|
392
|
|
|
*/ |
|
393
|
45 |
|
protected function getDefaultHandlerMethod() |
|
394
|
|
|
{ |
|
395
|
45 |
|
return 'handle'; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Returns the arguments parser to use if none is set. |
|
400
|
|
|
* |
|
401
|
|
|
* @return ArgsParser The default args parser. |
|
402
|
|
|
*/ |
|
403
|
266 |
|
protected function getDefaultArgsParser() |
|
404
|
|
|
{ |
|
405
|
266 |
|
return new DefaultArgsParser(); |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Returns whether the arguments parsing handles errors gracefully. |
|
410
|
|
|
* |
|
411
|
|
|
* @return bool The default value for lenient args parsing. |
|
412
|
|
|
*/ |
|
413
|
259 |
|
protected function getDefaultLenientArgsParsing() |
|
414
|
|
|
{ |
|
415
|
259 |
|
return false; |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Adds the default configuration. |
|
420
|
|
|
* |
|
421
|
|
|
* Override this method in your own subclasses. |
|
422
|
|
|
*/ |
|
423
|
421 |
|
protected function configure() |
|
424
|
|
|
{ |
|
425
|
421 |
|
} |
|
426
|
|
|
} |
|
427
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.