@@ -153,16 +153,19 @@ Listeners receive a
153
153
This event is also dispatched when an exception is thrown by the command.
154
154
It is then dispatched just after the ``ConsoleEvents::ERROR `` event.
155
155
The exit code received in this case is the exception code.
156
-
157
156
158
157
The ``ConsoleEvents::SIGNAL `` Event
159
158
-----------------------------------
160
159
161
160
**Typical Purposes **: To perform some actions after the command execution was interrupted.
162
161
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.
166
169
167
170
Listeners receive a
168
171
:class: `Symfony\\ Component\\ Console\\ Event\\ ConsoleSignalEvent ` event::
@@ -180,9 +183,46 @@ Listeners receive a
180
183
}
181
184
});
182
185
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;
184
200
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
186
222
223
+ The ``ConsoleSignalEvent `` and ``SignalableCommandInterface `` classes were
224
+ introduced in Symfony 5.2.
187
225
188
226
.. _`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