Skip to content

Commit a73762c

Browse files
committed
[Console] Dispatch ConsoleTerminateEvent when exiting on signal
1 parent 3e9cabf commit a73762c

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 4 additions & 1 deletion
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
}

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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
---

src/Symfony/Component/Console/Event/ConsoleTerminateEvent.php

Lines changed: 13 additions & 6 deletions
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
}

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

Lines changed: 5 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy