From a73762c54da19f3b1b0c7c0902a3a98396db9033 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Fri, 13 Oct 2023 11:33:31 +0200 Subject: [PATCH] [Console] Dispatch `ConsoleTerminateEvent` when exiting on signal --- src/Symfony/Component/Console/Application.php | 5 ++++- src/Symfony/Component/Console/CHANGELOG.md | 1 + .../Console/Event/ConsoleTerminateEvent.php | 19 +++++++++++++------ .../Console/Tests/ApplicationTest.php | 5 +++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 842ef19070128..01b6a373db0c3 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -1046,7 +1046,10 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI } if (false !== $exitCode) { - exit($exitCode); + $event = new ConsoleTerminateEvent($command, $event->getInput(), $event->getOutput(), $exitCode, $signal); + $this->dispatcher->dispatch($event, ConsoleEvents::TERMINATE); + + exit($event->getExitCode()); } }); } diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 5af9c7cf7e3b8..9ccb41d945792 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -8,6 +8,7 @@ CHANGELOG * Multi-line text in vertical tables is aligned properly * The application can also catch errors with `Application::setCatchErrors(true)` * Add `RunCommandMessage` and `RunCommandMessageHandler` + * Dispatch `ConsoleTerminateEvent` after an exit on signal handling and add `ConsoleTerminateEvent::getInterruptingSignal()` 6.3 --- diff --git a/src/Symfony/Component/Console/Event/ConsoleTerminateEvent.php b/src/Symfony/Component/Console/Event/ConsoleTerminateEvent.php index de63c8ffa8e30..38f7253a5c899 100644 --- a/src/Symfony/Component/Console/Event/ConsoleTerminateEvent.php +++ b/src/Symfony/Component/Console/Event/ConsoleTerminateEvent.php @@ -19,16 +19,18 @@ * Allows to manipulate the exit code of a command after its execution. * * @author Francesco Levorato + * @author Jules Pietri */ final class ConsoleTerminateEvent extends ConsoleEvent { - private int $exitCode; - - public function __construct(Command $command, InputInterface $input, OutputInterface $output, int $exitCode) - { + public function __construct( + Command $command, + InputInterface $input, + OutputInterface $output, + private int $exitCode, + private readonly ?int $interruptingSignal = null, + ) { parent::__construct($command, $input, $output); - - $this->setExitCode($exitCode); } public function setExitCode(int $exitCode): void @@ -40,4 +42,9 @@ public function getExitCode(): int { return $this->exitCode; } + + public function getInterruptingSignal(): ?int + { + return $this->interruptingSignal; + } } diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index c728a46b38e26..5358a4e847349 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -2151,8 +2151,12 @@ public function testSignalableWithEventCommandDoesNotInterruptedOnTermSignals() $command = new TerminatableWithEventCommand(); + $terminateEventDispatched = false; $dispatcher = new EventDispatcher(); $dispatcher->addSubscriber($command); + $dispatcher->addListener('console.terminate', function () use (&$terminateEventDispatched) { + $terminateEventDispatched = true; + }); $application = new Application(); $application->setAutoExit(false); $application->setDispatcher($dispatcher); @@ -2167,6 +2171,7 @@ public function testSignalableWithEventCommandDoesNotInterruptedOnTermSignals() EOTXT; $this->assertSame($expected, $tester->getDisplay(true)); + $this->assertTrue($terminateEventDispatched); } /**