diff --git a/Application.php b/Application.php index 53be6d055..29951e9c1 100644 --- a/Application.php +++ b/Application.php @@ -1012,10 +1012,6 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI }); } } - - foreach ($commandSignals as $signal) { - $this->signalRegistry->register($signal, [$command, 'handleSignal']); - } } if (null !== $this->dispatcher) { @@ -1034,6 +1030,10 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI }); } } + + foreach ($commandSignals as $signal) { + $this->signalRegistry->register($signal, [$command, 'handleSignal']); + } } if (null === $this->dispatcher) { diff --git a/Tests/ApplicationTest.php b/Tests/ApplicationTest.php index 641415d28..fdb9b3f33 100644 --- a/Tests/ApplicationTest.php +++ b/Tests/ApplicationTest.php @@ -1975,6 +1975,21 @@ public function testSignalableCommandInterfaceWithoutSignals() $this->assertSame(0, $application->run(new ArrayInput(['signal']))); } + public function testSignalableCommandHandlerCalledAfterEventListener() + { + $command = new SignableCommand(); + + $subscriber = new SignalEventSubscriber(); + + $dispatcher = new EventDispatcher(); + $dispatcher->addSubscriber($subscriber); + + $application = $this->createSignalableApplication($command, $dispatcher); + $application->setSignalsToDispatchEvent(\SIGUSR1); + $this->assertSame(1, $application->run(new ArrayInput(['signal']))); + $this->assertSame([SignalEventSubscriber::class, SignableCommand::class], $command->signalHandlers); + } + /** * @group tty */ @@ -2076,6 +2091,7 @@ public function isEnabled(): bool class BaseSignableCommand extends Command { public $signaled = false; + public $signalHandlers = []; public $loop = 1000; private $emitsSignal; @@ -2116,6 +2132,7 @@ public function getSubscribedSignals(): array public function handleSignal(int $signal): void { $this->signaled = true; + $this->signalHandlers[] = __CLASS__; } } @@ -2127,6 +2144,7 @@ public function onSignal(ConsoleSignalEvent $event): void { $this->signaled = true; $event->getCommand()->signaled = true; + $event->getCommand()->signalHandlers[] = __CLASS__; } public static function getSubscribedEvents(): array