Skip to content

Commit 915bf49

Browse files
committed
Rewords and tweaks
1 parent 87113d8 commit 915bf49

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

components/console/events.rst

+46-6
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,19 @@ Listeners receive a
153153
This event is also dispatched when an exception is thrown by the command.
154154
It is then dispatched just after the ``ConsoleEvents::ERROR`` event.
155155
The exit code received in this case is the exception code.
156-
157156

158157
The ``ConsoleEvents::SIGNAL`` Event
159158
-----------------------------------
160159

161160
**Typical Purposes**: To perform some actions after the command execution was interrupted.
162161

163-
After the command has been interrupted, the ``ConsoleEvents::SIGNAL`` event is
164-
dispatched. It can be used to do any actions
165-
(to log or profile commands / to perform some cleanup tasks when quitting a command).
162+
`Signals`_ are asynchronous notifications sent to a process in order to notify
163+
it of an event that occurred. For example, when you press ``Ctrl + C`` in a
164+
command, the operating system sends the ``SIGINT`` signal to it.
165+
166+
When a command is interrupted, Symfony dispatches the ``ConsoleEvents::SIGNAL``
167+
event. Listen to this event so you can perform some actions (e.g. logging some
168+
results, cleaning some temporary files, etc.) before finishing the command execution.
166169

167170
Listeners receive a
168171
:class:`Symfony\\Component\\Console\\Event\\ConsoleSignalEvent` event::
@@ -180,9 +183,46 @@ Listeners receive a
180183
}
181184
});
182185

183-
.. versionadded:: 5.2
186+
.. tip::
187+
188+
All the available signals (``SIGINT``, ``SIGQUIT``, etc.) are defined as
189+
`constants of the PCNTL PHP extension`_.
190+
191+
If you use the Console component inside a Symfony application, commands can
192+
handle signals themselves. To do so, implement the
193+
``SignalableCommandInterface`` and subscribe to one or more signals::
194+
195+
// src/Command/SomeCommand.php
196+
namespace App\Command;
197+
198+
use Symfony\Component\Console\Command\Command;
199+
use Symfony\Component\Console\Command\SignalableCommandInterface;
184200

185-
The ``ConsoleSignalEvent`` class was introduced in Symfony 5.2.
201+
class SomeCommand extends Command implements SignalableCommandInterface
202+
{
203+
// ...
204+
205+
public function getSubscribedSignals(): array
206+
{
207+
// return here any of the constants defined by PCNTL extension
208+
return [\SIGINT, \SIGTERM];
209+
}
210+
211+
public function handleSignal(int $signal)
212+
{
213+
if (\SIGINT === $signal) {
214+
// ...
215+
}
216+
217+
// ...
218+
}
219+
}
220+
221+
.. versionadded:: 5.2
186222

223+
The ``ConsoleSignalEvent`` and ``SignalableCommandInterface`` classes were
224+
introduced in Symfony 5.2.
187225

188226
.. _`reserved exit codes`: https://www.tldp.org/LDP/abs/html/exitcodes.html
227+
.. _`Signals`: https://en.wikipedia.org/wiki/Signal_(IPC)
228+
.. _`constants of the PCNTL PHP extension`: https://www.php.net/manual/en/pcntl.constants.php

0 commit comments

Comments
 (0)