Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d06cbbb

Browse filesBrowse files
[Console] Initialize lazily to render exceptions properly
1 parent f1c65c0 commit d06cbbb
Copy full SHA for d06cbbb

File tree

1 file changed

+36
-9
lines changed
Filter options

1 file changed

+36
-9
lines changed

‎src/Symfony/Component/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+36-9Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class Application
7272
private $dispatcher;
7373
private $terminalDimensions;
7474
private $defaultCommand;
75+
private $initialized;
7576

7677
/**
7778
* Constructor.
@@ -84,12 +85,6 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
8485
$this->name = $name;
8586
$this->version = $version;
8687
$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-
}
9388
}
9489

9590
public function setDispatcher(EventDispatcherInterface $dispatcher)
@@ -121,6 +116,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
121116

122117
try {
123118
$e = null;
119+
$this->init();
124120
$exitCode = $this->doRun($input, $output);
125121
} catch (\Exception $x) {
126122
$e = $x;
@@ -189,10 +185,11 @@ public function doRun(InputInterface $input, OutputInterface $output)
189185

190186
if (!$name) {
191187
$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(),
194191
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),
196193
)
197194
));
198195
}
@@ -225,6 +222,10 @@ public function setHelperSet(HelperSet $helperSet)
225222
*/
226223
public function getHelperSet()
227224
{
225+
if (!$this->helperSet) {
226+
$this->helperSet = $this->getDefaultHelperSet();
227+
}
228+
228229
return $this->helperSet;
229230
}
230231

@@ -245,6 +246,10 @@ public function setDefinition(InputDefinition $definition)
245246
*/
246247
public function getDefinition()
247248
{
249+
if (!$this->definition) {
250+
$this->definition = $this->getDefaultInputDefinition();
251+
}
252+
248253
return $this->definition;
249254
}
250255

@@ -374,6 +379,8 @@ public function addCommands(array $commands)
374379
*/
375380
public function add(Command $command)
376381
{
382+
$this->init();
383+
377384
$command->setApplication($this);
378385

379386
if (!$command->isEnabled()) {
@@ -406,6 +413,8 @@ public function add(Command $command)
406413
*/
407414
public function get($name)
408415
{
416+
$this->init();
417+
409418
if (!isset($this->commands[$name])) {
410419
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
411420
}
@@ -433,6 +442,8 @@ public function get($name)
433442
*/
434443
public function has($name)
435444
{
445+
$this->init();
446+
436447
return isset($this->commands[$name]);
437448
}
438449

@@ -510,6 +521,8 @@ public function findNamespace($namespace)
510521
*/
511522
public function find($name)
512523
{
524+
$this->init();
525+
513526
$allCommands = array_keys($this->commands);
514527
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
515528
$commands = preg_grep('{^'.$expr.'}', $allCommands);
@@ -565,6 +578,8 @@ public function find($name)
565578
*/
566579
public function all($namespace = null)
567580
{
581+
$this->init();
582+
568583
if (null === $namespace) {
569584
return $this->commands;
570585
}
@@ -1151,4 +1166,16 @@ private function extractAllNamespaces($name)
11511166

11521167
return $namespaces;
11531168
}
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+
}
11541181
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.