Skip to content

Commit 37827a2

Browse files
committed
bug symfony#15725 Dispatch console.terminate *after* console.exception (Seldaek)
This PR was merged into the 2.3 branch. Discussion ---------- Dispatch console.terminate *after* console.exception | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | - The problem is when using the ConsoleHandler from MonologBridge, TERMINATE closes it and removes the output, so when EXCEPTION fires if you want to log the exception it's too late and you don't get any output. See https://github.com/symfony/symfony/blob/ed4fb549014ed628200905c9e10cc0bb522f3168/src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php#L115-L145 It is my understanding that TERMINATE is always supposed to come last anyway, so it is a bug in any case, but this particular use case is what prompted the discovery. Commits ------- 7802345 Dispatch console.terminate *after* console.exception
2 parents 1e27c85 + 7802345 commit 37827a2

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -899,13 +899,15 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
899899
try {
900900
$exitCode = $command->run($input, $output);
901901
} catch (\Exception $e) {
902+
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
903+
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
904+
905+
$e = $event->getException();
906+
902907
$event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
903908
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
904909

905-
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $event->getExitCode());
906-
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
907-
908-
throw $event->getException();
910+
throw $e;
909911
}
910912

911913
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ public function testRunWithDispatcher()
799799

800800
$tester = new ApplicationTester($application);
801801
$tester->run(array('command' => 'foo'));
802-
$this->assertEquals('before.foo.after.', $tester->getDisplay());
802+
$this->assertEquals('before.foo.after.'.PHP_EOL, $tester->getDisplay());
803803
}
804804

805805
/**
@@ -835,7 +835,7 @@ public function testRunDispatchesAllEventsWithException()
835835

836836
$tester = new ApplicationTester($application);
837837
$tester->run(array('command' => 'foo'));
838-
$this->assertContains('before.foo.after.caught.', $tester->getDisplay());
838+
$this->assertContains('before.foo.caught.after.', $tester->getDisplay());
839839
}
840840

841841
protected function getDispatcher()
@@ -845,12 +845,12 @@ protected function getDispatcher()
845845
$event->getOutput()->write('before.');
846846
});
847847
$dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) {
848-
$event->getOutput()->write('after.');
848+
$event->getOutput()->writeln('after.');
849849

850850
$event->setExitCode(128);
851851
});
852852
$dispatcher->addListener('console.exception', function (ConsoleExceptionEvent $event) {
853-
$event->getOutput()->writeln('caught.');
853+
$event->getOutput()->write('caught.');
854854

855855
$event->setException(new \LogicException('caught.', $event->getExitCode(), $event->getException()));
856856
});

0 commit comments

Comments
 (0)