8000 Merge branch '5.3' into 5.4 · symfony/symfony-docs@2620b49 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2620b49

Browse files
committed
Merge branch '5.3' into 5.4
* 5.3: [Console] Add docs for ConsoleSignalEvent
2 parents f39873e + 915bf49 commit 2620b49

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

components/console/events.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,75 @@ Listeners receive a
154154
It is then dispatched just after the ``ConsoleEvents::ERROR`` event.
155155
The exit code received in this case is the exception code.
156156

157+
The ``ConsoleEvents::SIGNAL`` Event
158+
-----------------------------------
159+
160+
**Typical Purposes**: To perform some actions after the command execution was interrupted.
161+
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.
169+
170+
Listeners receive a
171+
:class:`Symfony\\Component\\Console\\Event\\ConsoleSignalEvent` event::
172+
173+
use Symfony\Component\Console\ConsoleEvents;
174+
use Symfony\Component\Console\Event\ConsoleSignalEvent;
175+
176+
$dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event) {
177+
178+
// gets the signal number
179+
$signal = $event->getHandlingSignal();
180+
181+
if (\SIGINT === $signal) {
182+
echo "bye bye!";
183+
}
184+
});
185+
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;
200+
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
222+
223+
The ``ConsoleSignalEvent`` and ``SignalableCommandInterface`` classes were
224+
introduced in Symfony 5.2.
225+
157226
.. _`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)
0