From 1fc1bca6328d72ca2593f357b5f575365f930372 Mon Sep 17 00:00:00 2001 From: noniagriconomie Date: Mon, 23 Aug 2021 15:34:28 +0100 Subject: [PATCH 1/2] [Console] Add signal event --- components/console/events.rst | 17 ++++++++++++++ console.rst | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/components/console/events.rst b/components/console/events.rst index 7183c2e75f7..0fa5ec313de 100644 --- a/components/console/events.rst +++ b/components/console/events.rst @@ -155,3 +155,20 @@ Listeners receive a The exit code received in this case is the exception code. .. _`reserved exit codes`: https://www.tldp.org/LDP/abs/html/exitcodes.html + +The ``ConsoleEvents::SIGNAL`` Event +----------------------------------- + +**Typical Purposes**: To handle some signals for all application commands. + +Listeners receive a +:class:`Symfony\\Component\\Console\\Event\\ConsoleSignalEvent` event:: + + use Symfony\Component\Console\ConsoleEvents; + use Symfony\Component\Console\Event\ConsoleSignalEvent; + + $dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event) { + $signal = $event->getHandlingSignal(); + + // ... + }); diff --git a/console.rst b/console.rst index 72e7ea30e0b..e60dcc07b04 100644 --- a/console.rst +++ b/console.rst @@ -427,6 +427,48 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester`` :class:`Symfony\\Component\\Console\\Application ` and extend the normal ``\PHPUnit\Framework\TestCase``. +Command Reacting to Signals +--------------------------- + +.. versionadded:: 5.2 + + The :class:`Symfony\\Component\\Console\\Command\\SignalableCommandInterface` interface + for reacting to signals was introduced in Symfony 5.2. + +You can make your commands react to signals by implementing the ``SignalableCommandInterface`` interface:: + + // ... + use Symfony\Component\Console\Command\Command; + use Symfony\Component\Console\Command\SignalableCommandInterface; + + class CreateUserCommand extends Command implements SignalableCommandInterface + { + // ... + + public function getSubscribedSignals(): array + { + // constants defined by PCNTL extension + // https://www.php.net/manual/en/pcntl.constants.php + return [SIGINT, SIGTERM]; + } + + public function handleSignal(int $signal): void + { + if (SIGINT === $signal) { + // handle SIGINT signal + } + + // handle SIGTERM signal + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + // ... + + return Command::SUCCESS; + } + } + Logging Command Errors ---------------------- From abac9cc584e1ae771f9a69434a44c4927a2910d2 Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Tue, 21 Sep 2021 11:25:28 +0200 Subject: [PATCH 2/2] Update events.rst --- components/console/events.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components/console/events.rst b/components/console/events.rst index 0fa5ec313de..cf3ab2a0a24 100644 --- a/components/console/events.rst +++ b/components/console/events.rst @@ -159,7 +159,11 @@ Listeners receive a The ``ConsoleEvents::SIGNAL`` Event ----------------------------------- -**Typical Purposes**: To handle some signals for all application commands. +.. versionadded:: 5.2 + + The ``ConsoleSignalEvent`` class was introduced in Symfony 5.2. + +**Typical Purposes**: To handle some signals for all application commands while being executed. Listeners receive a :class:`Symfony\\Component\\Console\\Event\\ConsoleSignalEvent` event:: @@ -168,6 +172,7 @@ Listeners receive a use Symfony\Component\Console\Event\ConsoleSignalEvent; $dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event) { + // https://www.php.net/manual/en/pcntl.constants.php $signal = $event->getHandlingSignal(); // ...