Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48b3ade

Browse files
committed
[FrameworkBundle] Catch Fatal errors in commands registration
1 parent fea348c commit 48b3ade

File tree

3 files changed

+99
-7
lines changed

3 files changed

+99
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Console;
1313

14+
use Symfony\Bundle\FrameworkBundle\Console\Exception\CommandRegistrationFailedException;
15+
use Symfony\Component\Console\Exception\RuntimeException;
16+
use Symfony\Component\Debug\Exception\FatalThrowableError;
1417
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1518
use Symfony\Component\Console\Application as BaseApplication;
1619
use Symfony\Component\Console\Command\Command;
@@ -30,6 +33,7 @@ class Application extends BaseApplication
3033
{
3134
private $kernel;
3235
private $commandsRegistered = false;
36+
private $registrationErrors = array();
3337

3438
/**
3539
* Constructor.
@@ -70,9 +74,25 @@ public function doRun(InputInterface $input, OutputInterface $output)
7074

7175
$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
7276

77+
if ($this->registrationErrors) {
78+
$this->renderRegistrationErrors($output);
79+
}
80+
7381
return parent::doRun($input, $output);
7482
}
7583

84+
/**
85+
* {@inheritdoc}
86+
*/
87+
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
88+
{
89+
if ($this->registrationErrors) {
90+
$this->renderRegistrationErrors($output);
91+
}
92+
93+
return parent::doRunCommand($command, $input, $output);
94+
}
95+
7696
/**
7797
* {@inheritdoc}
7898
*/
@@ -138,7 +158,13 @@ protected function registerCommands()
138158

139159
foreach ($this->kernel->getBundles() as $bundle) {
140160
if ($bundle instanceof Bundle) {
141-
$bundle->registerCommands($this);
161+
try {
162+
$bundle->registerCommands($this);
163+
} catch (\Exception $e) {
164+
$this->registrationErrors[] = $e;
165+
} catch (\Throwable $e) {
166+
$this->registrationErrors[] = new FatalThrowableError($e);
167+
}
142168
}
143169
}
144170

@@ -149,9 +175,24 @@ protected function registerCommands()
149175
if ($container->hasParameter('console.command.ids')) {
150176
foreach ($container->getParameter('console.command.ids') as $id) {
151177
if (false !== $id) {
152-
$this->add($container->get($id));
178+
try {
179+
$this->add($container->get($id));
180+
} catch (\Exception $e) {
181+
$this->registrationErrors[] = $e;
182+
} catch (\Throwable $e) {
183+
$this->registrationErrors[] = new FatalThrowableError($e);
184+
}
153185
}
154186
}
155187
}
156188
}
189+
190+
private function renderRegistrationErrors(OutputInterface $output)
191+
{
192+
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
193+
$this->doRenderException(new RuntimeException('Some commands could not be registered.'), $output);
194+
foreach ($this->registrationErrors as $error) {
195+
$this->doRenderException($error, $output);
196+
}
197+
}
157198
}

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1616
use Symfony\Component\Console\Command\Command;
1717
use Symfony\Component\Console\Input\ArrayInput;
18+
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Output\NullOutput;
20+
use Symfony\Component\Console\Output\OutputInterface;
1921
use Symfony\Component\Console\Tester\ApplicationTester;
22+
use Symfony\Component\DependencyInjection\ContainerBuilder;
23+
use Symfony\Component\EventDispatcher\EventDispatcher;
24+
use Symfony\Component\HttpKernel\KernelInterface;
2025

2126
class ApplicationTest extends TestCase
2227
{
@@ -130,6 +135,36 @@ public function testBundleCommandCanOverriddeAPreExistingCommandWithTheSameName(
130135
$this->assertSame($newCommand, $application->get('example'));
131136
}
132137

138+
public function testRunOnlyWarnsOnUnregistrableCommand()
139+
{
140+
$container = new ContainerBuilder();
141+
$container->register('event_dispatcher', EventDispatcher::class);
142+
$container->register(ThrowingCommand::class, ThrowingCommand::class);
143+
$container->setParameter('console.command.ids', array(ThrowingCommand::class => ThrowingCommand::class));
144+
145+
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();
146+
$kernel
147+
->method('getBundles')
148+
->willReturn(array($this->createBundleMock(
149+
array((new Command('fine'))->setCode(function (InputInterface $input, OutputInterface $output) { $output->write('fine'); }))
150+
)));
151+
$kernel
152+
->method('getContainer')
153+
->willReturn($container);
154+
155+
$application = new Application($kernel);
156+
$application->setAutoExit(false);
157+
158+
$tester = new ApplicationTester($application);
159+
$tester->run(array('command' => 'fine'));
160+
$output = $tester->getDisplay();
161+
162+
$this->assertSame(0, $tester->getStatusCode());
163+
$this->assertContains('Some commands could not be registered.', $output);
164+
$this->assertContains('throwing', $output);
165+
$this->assertContains('fine', $output);
166+
}
167+
133168
private function getKernel(array $bundles, $useDispatcher = false)
134169
{
135170
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
@@ -189,3 +224,11 @@ private function createBundleMock(array $commands)
189224
return $bundle;
190225
}
191226
}
227+
228+
class ThrowingCommand extends Command
229+
{
230+
public function __construct()
231+
{
232+
throw new \Exception('throwing');
233+
}
234+
}

src/Symfony/Component/Console/Application.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,19 @@ public function renderException(\Exception $e, OutputInterface $output)
706706
{
707707
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
708708

709+
$this->doRenderException($e, $output);
710+
711+
if (null !== $this->runningCommand) {
712+
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);
713+
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
714+
}
715+
}
716+
717+
/**
718+
* @internal
719+
*/
720+
protected function doRenderException(\Exception $e, OutputInterface $output)
721+
{
709722
do {
710723
$title = sprintf(
711724
' [%s%s] ',
@@ -767,11 +780,6 @@ public function renderException(\Exception $e, OutputInterface $output)
767780
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
768781
}
769782
} while ($e = $e->getPrevious());
770-
771-
if (null !== $this->runningCommand) {
772-
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);
773-
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
774-
}
775783
}
776784

777785
/**

0 commit comments

Comments
 (0)