Skip to content

Commit

Permalink
[Console] Dispatch ConsoleTerminateEvent when exiting on signal
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Oct 13, 2023
1 parent 3e9cabf commit a73762c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
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

0 comments on commit a73762c

Please sign in to comment.