innmind / signals
Manage multiple callbacks per signal
Installs: 92 733
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
pkg:composer/innmind/signals
Requires
- php: ~8.4
- innmind/immutable: ~6.0
Requires (Dev)
- innmind/black-box: ~6.5
- innmind/coding-standard: ~2.0
- innmind/static-analysis: ~1.3
README
Small abstraction on top of pcntl_signal to allow to register multiple callables for a single signal.
Installation
composer require innmind/signals
Usage
use Innmind\Signals\{ Handler, Signal, Info, }; $handler = Handler::main(); // automatically enable async signal on first `->listen()` call $handler ->listen(Signal::interrupt, function(Signal $signal, Info $info): void { echo 'foo'; }) ->unwrap(); $handler ->listen(Signal::interrupt, function(Signal $signal, Info $info): void { echo 'bar'; }) ->unwrap(); // do some logic here
When above script is executed in a terminal and you do a ctrl + c to stop the process it will print foobar instead of stopping the script.
If for some reason you need to remove a handler (for example when a child process ended) you can call $handler->remove($listener)->unwrap() (remove the listener for all signals).