@@ -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 )
@@ -121,6 +116,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
121
116
122
117
try {
123
118
$ e = null ;
119
+ $ this ->init ();
124
120
$ exitCode = $ this ->doRun ($ input , $ output );
125
121
} catch (\Exception $ x ) {
126
122
$ e = $ x ;
@@ -189,10 +185,11 @@ public function doRun(InputInterface $input, OutputInterface $output)
189
185
190
186
if (!$ name ) {
191
187
$ name = $ this ->defaultCommand ;
192
- $ this ->definition ->setArguments (array_merge (
193
- $ this ->definition ->getArguments (),
188
+ $ definition = $ this ->getDefinition ();
189
+ $ definition ->setArguments (array_merge (
190
+ $ definition ->getArguments (),
194
191
array (
195
- 'command ' => new InputArgument ('command ' , InputArgument::OPTIONAL , $ this -> definition ->getArgument ('command ' )->getDescription (), $ name ),
192
+ 'command ' => new InputArgument ('command ' , InputArgument::OPTIONAL , $ definition ->getArgument ('command ' )->getDescription (), $ name ),
196
193
)
197
194
));
198
195
}
@@ -225,6 +222,10 @@ public function setHelperSet(HelperSet $helperSet)
225
222
*/
226
223
public function getHelperSet ()
227
224
{
225
+ if (!$ this ->helperSet ) {
226
+ $ this ->helperSet = $ this ->getDefaultHelperSet ();
227
+ }
228
+
228
229
return $ this ->helperSet ;
229
230
}
230
231
@@ -245,6 +246,10 @@ public function setDefinition(InputDefinition $definition)
245
246
*/
246
247
public function getDefinition ()
247
248
{
249
+ if (!$ this ->definition ) {
250
+ $ this ->definition = $ this ->getDefaultInputDefinition ();
251
+ }
252
+
248
253
return $ this ->definition ;
249
254
}
250
255
@@ -374,6 +379,8 @@ public function addCommands(array $commands)
374
379
*/
375
380
public function add (Command $ command )
376
381
{
382
+ $ this ->init ();
383
+
377
384
$ command ->setApplication ($ this );
378
385
379
386
if (!$ command ->isEnabled ()) {
@@ -406,6 +413,8 @@ public function add(Command $command)
406
413
*/
407
414
public function get ($ name )
408
415
{
416
+ $ this ->init ();
417
+
409
418
if (!isset ($ this ->commands [$ name ])) {
410
419
throw new \InvalidArgumentException (sprintf ('The command "%s" does not exist. ' , $ name ));
411
420
}
@@ -433,6 +442,8 @@ public function get($name)
433
442
*/
434
443
public function has ($ name )
435
444
{
445
+ $ this ->init ();
446
+
436
447
return isset ($ this ->commands [$ name ]);
437
448
}
438
449
@@ -510,6 +521,8 @@ public function findNamespace($namespace)
510
521
*/
511
522
public function find ($ name )
512
523
{
524
+ $ this ->init ();
525
+
513
526
$ allCommands = array_keys ($ this ->commands );
514
527
$ expr = preg_replace_callback ('{([^:]+|)} ' , function ($ matches ) { return preg_quote ($ matches [1 ]).'[^:]* ' ; }, $ name );
515
528
$ commands = preg_grep ('{^ ' .$ expr .'} ' , $ allCommands );
@@ -565,6 +578,8 @@ public function find($name)
565
578
*/
566
579
public function all ($ namespace = null )
567
580
{
581
+ $ this ->init ();
582
+
568
583
if (null === $ namespace ) {
569
584
return $ this ->commands ;
570
585
}
@@ -1151,4 +1166,16 @@ private function extractAllNamespaces($name)
1151
1166
1152
1167
return $ namespaces ;
1153
1168
}
1169
+
1170
+ private function init ()
1171
+ {
1172
+ if ($ this ->initialized ) {
1173
+ return ;
1174
+ }
1175
+ $ this ->initialized = true ;
1176
+
1177
+ foreach ($ this ->getDefaultCommands () as $ command ) {
1178
+ $ this ->add ($ command );
1179
+ }
1180
+ }
1154
1181
}
0 commit comments