Skip to content

Commit

Permalink
bug #23909 [Console] Initialize lazily to render exceptions properly …
Browse files Browse the repository at this point in the history
…(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
  • Loading branch information
chalasr committed Aug 17, 2017
2 parents f1c65c0 + 62174fd commit d22d924
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -72,6 +72,7 @@ class Application
private $dispatcher;
private $terminalDimensions;
private $defaultCommand;
private $initialized;

/**
* Constructor.
Expand All @@ -84,12 +85,6 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
$this->name = $name;
$this->version = $version;
$this->defaultCommand = 'list';
$this->helperSet = $this->getDefaultHelperSet();
$this->definition = $this->getDefaultInputDefinition();

foreach ($this->getDefaultCommands() as $command) {
$this->add($command);
}
}

public function setDispatcher(EventDispatcherInterface $dispatcher)
Expand Down Expand Up @@ -189,10 +184,11 @@ public function doRun(InputInterface $input, OutputInterface $output)

if (!$name) {
$name = $this->defaultCommand;
$this->definition->setArguments(array_merge(
$this->definition->getArguments(),
$definition = $this->getDefinition();
$definition->setArguments(array_merge(
$definition->getArguments(),
array(
'command' => new InputArgument('command', InputArgument::OPTIONAL, $this->definition->getArgument('command')->getDescription(), $name),
'command' => new InputArgument('command', InputArgument::OPTIONAL, $definition->getArgument('command')->getDescription(), $name),
)
));
}
Expand Down Expand Up @@ -225,6 +221,10 @@ public function setHelperSet(HelperSet $helperSet)
*/
public function getHelperSet()
{
if (!$this->helperSet) {
$this->helperSet = $this->getDefaultHelperSet();
}

return $this->helperSet;
}

Expand All @@ -245,6 +245,10 @@ public function setDefinition(InputDefinition $definition)
*/
public function getDefinition()
{
if (!$this->definition) {
$this->definition = $this->getDefaultInputDefinition();
}

return $this->definition;
}

Expand Down Expand Up @@ -374,6 +378,8 @@ public function addCommands(array $commands)
*/
public function add(Command $command)
{
$this->init();

$command->setApplication($this);

if (!$command->isEnabled()) {
Expand Down Expand Up @@ -406,6 +412,8 @@ public function add(Command $command)
*/
public function get($name)
{
$this->init();

if (!isset($this->commands[$name])) {
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
}
Expand Down Expand Up @@ -433,6 +441,8 @@ public function get($name)
*/
public function has($name)
{
$this->init();

return isset($this->commands[$name]);
}

Expand Down Expand Up @@ -510,6 +520,8 @@ public function findNamespace($namespace)
*/
public function find($name)
{
$this->init();

$allCommands = array_keys($this->commands);
$expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
$commands = preg_grep('{^'.$expr.'}', $allCommands);
Expand Down Expand Up @@ -565,6 +577,8 @@ public function find($name)
*/
public function all($namespace = null)
{
$this->init();

if (null === $namespace) {
return $this->commands;
}
Expand Down Expand Up @@ -1151,4 +1165,16 @@ private function extractAllNamespaces($name)

return $namespaces;
}

private function init()
{
if ($this->initialized) {
return;
}
$this->initialized = true;

foreach ($this->getDefaultCommands() as $command) {
$this->add($command);
}
}
}

0 comments on commit d22d924

Please sign in to comment.