Skip to content

Commit d22d924

Browse files
committed
bug #23909 [Console] Initialize lazily to render exceptions properly (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [Console] Initialize lazily to render exceptions properly | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - When an exception occurs in the constructor of an `Application`, exception handling is not yet set up. This typically happens when the container cannot be build for some reason, where calling `$this->add()` builds the container and fails. This PR makes initialization lazy so that it happens later on, when exception handling might be OK. Before: ![before](https://user-images.githubusercontent.com/243674/29403397-7f7a1fce-8338-11e7-8e62-b6302ff0a65e.png) After: ![after](https://user-images.githubusercontent.com/243674/29403403-83f7b5c0-8338-11e7-9a22-0c5ee702f1ff.png) Commits ------- 62174fd [Console] Initialize lazily to render exceptions properly
2 parents f1c65c0 + 62174fd commit d22d924

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/Symfony/Component/Console/Application.php

+35-9
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)
@@ -189,10 +184,11 @@ public function doRun(InputInterface $input, OutputInterface $output)
189184

190185
if (!$name) {
191186
$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(),
194190
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),
196192
)
197193
));
198194
}
@@ -225,6 +221,10 @@ public function setHelperSet(HelperSet $helperSet)
225221
*/
226222
public function getHelperSet()
227223
{
224+
if (!$this->helperSet) {
225+
$this->helperSet = $this->getDefaultHelperSet();
226+
}
227+
228228
return $this->helperSet;
229229
}
230230

@@ -245,6 +245,10 @@ public function setDefinition(InputDefinition $definition)
245245
*/
246246
public function getDefinition()
247247
{
248+
if (!$this->definition) {
249+
$this->definition = $this->getDefaultInputDefinition();
250+
}
251+
248252
return $this->definition;
249253
}
250254

@@ -374,6 +378,8 @@ public function addCommands(array $commands)
374378
*/
375379
public function add(Command $command)
376380
{
381+
$this->init();
382+
377383
$command->setApplication($this);
378384

379385
if (!$command->isEnabled()) {
@@ -406,6 +412,8 @@ public function add(Command $command)
406412
*/
407413
public function get($name)
408414
{
415+
$this->init();
416+
409417
if (!isset($this->commands[$name])) {
410418
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
411419
}
@@ -433,6 +441,8 @@ public function get($name)
433441
*/
434442
public function has($name)
435443
{
444+
$this->init();
445+
436446
return isset($this->commands[$name]);
437447
}
438448

@@ -510,6 +520,8 @@ public function findNamespace($namespace)
510520
*/
511521
public function find($name)
512522
{
523+
$this->init();
524+
513525
$allCommands = array_keys($this->commands);
514526
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
515527
$commands = preg_grep('{^'.$expr.'}', $allCommands);
@@ -565,6 +577,8 @@ public function find($name)
565577
*/
566578
public function all($namespace = null)
567579
{
580+
$this->init();
581+
568582
if (null === $namespace) {
569583
return $this->commands;
570584
}
@@ -1151,4 +1165,16 @@ private function extractAllNamespaces($name)
11511165

11521166
return $namespaces;
11531167
}
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+
}
11541180
}

0 commit comments

Comments
 (0)