Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Dispatch ConsoleTerminateEvent when exiting on signal #52038

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
19 changes: 13 additions & 6 deletions src/Symfony/Component/Console/Event/ConsoleTerminateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
* Allows to manipulate the exit code of a command after its execution.
*
* @author Francesco Levorato <git@flevour.net>
* @author Jules Pietri <jules@heahprod.com>
*/
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
Expand All @@ -40,4 +42,9 @@ public function getExitCode(): int
{
return $this->exitCode;
}

public function getInterruptingSignal(): ?int
{
return $this->interruptingSignal;
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -2167,6 +2171,7 @@ public function testSignalableWithEventCommandDoesNotInterruptedOnTermSignals()

EOTXT;
$this->assertSame($expected, $tester->getDisplay(true));
$this->assertTrue($terminateEventDispatched);
}

/**
Expand Down