@@ -72,6 +72,7 @@ class Application
72
72
private $ dispatcher ;
73
73
private $ terminalDimensions ;
74
74
private $ defaultCommand ;
75
+ private $ initialized ;
75
76
76
77
/**
77
78
* Constructor.
@@ -84,12 +85,6 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
84
85
$ this ->name = $ name ;
85
86
$ this ->version = $ version ;
86
87
$ this ->defaultCommand = 'list ' ;
87
- $ this ->helperSet = $ this ->getDefaultHelperSet ();
88
- $ this ->definition = $ this ->getDefaultInputDefinition ();
89
-
90
- foreach ($ this ->getDefaultCommands () as $ command ) {
91
- $ this ->add ($ command );
92
- }
93
88
}
94
89
95
90
public function setDispatcher (EventDispatcherInterface $ dispatcher )
@@ -189,10 +184,11 @@ public function doRun(InputInterface $input, OutputInterface $output)
189
184
190
185
if (!$ name ) {
191
186
$ name = $ this ->defaultCommand ;
192
- $ this ->definition ->setArguments (array_merge (
193
- $ this ->definition ->getArguments (),
187
+ $ definition = $ this ->getDefinition ();
188
+ $ definition ->setArguments (array_merge (
189
+ $ definition ->getArguments (),
194
190
array (
195
- 'command ' => new InputArgument ('command ' , InputArgument::OPTIONAL , $ this -> definition ->getArgument ('command ' )->getDescription (), $ name ),
191
+ 'command ' => new InputArgument ('command ' , InputArgument::OPTIONAL , $ definition ->getArgument ('command ' )->getDescription (), $ name ),
196
192
)
197
193
));
198
194
}
@@ -225,6 +221,10 @@ public function setHelperSet(HelperSet $helperSet)
225
221
*/
226
222
public function getHelperSet ()
227
223
{
224
+ if (!$ this ->helperSet ) {
225
+ $ this ->helperSet = $ this ->getDefaultHelperSet ();
226
+ }
227
+
228
228
return $ this ->helperSet ;
229
229
}
230
230
@@ -245,6 +245,10 @@ public function setDefinition(InputDefinition $definition)
245
245
*/
246
246
public function getDefinition ()
247
247
{
248
+ if (!$ this ->definition ) {
249
+ $ this ->definition = $ this ->getDefaultInputDefinition ();
250
+ }
251
+
248
252
return $ this ->definition ;
249
253
}
250
254
@@ -374,6 +378,8 @@ public function addCommands(array $commands)
374
378
*/
375
379
public function add (Command $ command )
376
380
{
381
+ $ this ->init ();
382
+
377
383
$ command ->setApplication ($ this );
378
384
379
385
if (!$ command ->isEnabled ()) {
@@ -406,6 +412,8 @@ public function add(Command $command)
406
412
*/
407
413
public function get ($ name )
408
414
{
415
+ $ this ->init ();
416
+
409
417
if (!isset ($ this ->commands [$ name ])) {
410
418
throw new \InvalidArgumentException (sprintf ('The command "%s" does not exist. ' , $ name ));
411
419
}
@@ -433,6 +441,8 @@ public function get($name)
433
441
*/
434
442
public function has ($ name )
435
443
{
444
+ $ this ->init ();
445
+
436
446
return isset ($ this ->commands [$ name ]);
437
447
}
438
448
@@ -510,6 +520,8 @@ public function findNamespace($namespace)
510
520
*/
511
521
public function find ($ name )
512
522
{
523
+ $ this ->init ();
524
+
513
525
$ allCommands = array_keys ($ this ->commands );
514
526
$ expr = preg_replace_callback ('{([^:]+|)} ' , function ($ matches ) { return preg_quote ($ matches [1 ]).'[^:]* ' ; }, $ name );
515
527
$ commands = preg_grep ('{^ ' .$ expr .'} ' , $ allCommands );
@@ -565,6 +577,8 @@ public function find($name)
565
577
*/
566
578
public function all ($ namespace = null )
567
579
{
580
+ $ this ->init ();
581
+
568
582
if (null === $ namespace ) {
569
583
return $ this ->commands ;
570
584
}
@@ -1151,4 +1165,16 @@ private function extractAllNamespaces($name)
1151
1165
1152
1166
return $ namespaces ;
1153
1167
}
1168
+
1169
+ private function init ()
1170
+ {
1171
+ if ($ this ->initialized ) {
1172
+ return ;
1173
+ }
1174
+ $ this ->initialized = true ;
1175
+
1176
+ foreach ($ this ->getDefaultCommands () as $ command ) {
1177
+ $ this ->add ($ command );
1178
+ }
1179
+ }
1154
1180
}
0 commit comments