Skip to content

Commit 2edf780

Browse files
committed
[Console] Dispatch ConsoleTerminateEvent when exiting on signal
1 parent 2d5e1d6 commit 2edf780

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

Application.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,10 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
10461046
}
10471047

10481048
if (false !== $exitCode) {
1049-
exit($exitCode);
1049+
$event = new ConsoleTerminateEvent($command, $event->getInput(), $event->getOutput(), $exitCode, $signal);
1050+
$this->dispatcher->dispatch($event, ConsoleEvents::TERMINATE);
1051+
1052+
exit($event->getExitCode());
10501053
}
10511054
});
10521055
}

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Multi-line text in vertical tables is aligned properly
99
* The application can also catch errors with `Application::setCatchErrors(true)`
1010
* Add `RunCommandMessage` and `RunCommandMessageHandler`
11+
* Dispatch `ConsoleTerminateEvent` after an exit on signal handling and add `ConsoleTerminateEvent::getInterruptingSignal()`
1112

1213
6.3
1314
---

Event/ConsoleTerminateEvent.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@
1919
* Allows to manipulate the exit code of a command after its execution.
2020
*
2121
* @author Francesco Levorato <git@flevour.net>
22+
* @author Jules Pietri <jules@heahprod.com>
2223
*/
2324
final class ConsoleTerminateEvent extends ConsoleEvent
2425
{
25-
private int $exitCode;
26-
27-
public function __construct(Command $command, InputInterface $input, OutputInterface $output, int $exitCode)
28-
{
26+
public function __construct(
27+
Command $command,
28+
InputInterface $input,
29+
OutputInterface $output,
30+
private int $exitCode,
31+
private readonly ?int $interruptingSignal = null,
32+
) {
2933
parent::__construct($command, $input, $output);
30-
31-
$this->setExitCode($exitCode);
3234
}
3335

3436
public function setExitCode(int $exitCode): void
@@ -40,4 +42,9 @@ public function getExitCode(): int
4042
{
4143
return $this->exitCode;
4244
}
45+
46+
public function getInterruptingSignal(): ?int
47+
{
48+
return $this->interruptingSignal;
49+
}
4350
}

Tests/ApplicationTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -2151,8 +2151,12 @@ public function testSignalableWithEventCommandDoesNotInterruptedOnTermSignals()
21512151

21522152
$command = new TerminatableWithEventCommand();
21532153

2154+
$terminateEventDispatched = false;
21542155
$dispatcher = new EventDispatcher();
21552156
$dispatcher->addSubscriber($command);
2157+
$dispatcher->addListener('console.terminate', function () use (&$terminateEventDispatched) {
2158+
$terminateEventDispatched = true;
2159+
});
21562160
$application = new Application();
21572161
$application->setAutoExit(false);
21582162
$application->setDispatcher($dispatcher);
@@ -2167,6 +2171,7 @@ public function testSignalableWithEventCommandDoesNotInterruptedOnTermSignals()
21672171
21682172
EOTXT;
21692173
$this->assertSame($expected, $tester->getDisplay(true));
2174+
$this->assertTrue($terminateEventDispatched);
21702175
}
21712176

21722177
/**

0 commit comments

Comments
 (0)