Signal Handler for PHP
, (*1)
A simple signal handler to manage incoming posix signals., (*2)
It is implemented as a singleton as pcntl_signal
can only register one
callback per signal., (*3)
Using this one, you can register as many callbacks per signal as needed., (*4)
For more information about signals, man signal
., (*5)
// mandatory to listen to signals
declare(ticks=1);
$handler = Neutron\SignalHandler\SignalHandler::getInstance();
$handler->register(array(SIGINT, SIGTERM), function () { echo "stoppin !"; exit(); });
$handler->register(SIGCONT, function () { echo "all systems go..."; });
Register a signal handler
// register a handler for SIGCONT in default namespace
$handler->register(SIGCONT, function () { echo "SIGCONT received"; });
// register a handler for SIGCONT in "a namespace"
$handler->register(SIGCONT, function () { echo "SIGCONT received"; }, 'a namespace');
// register a handler for SIGCONT in "another namespace"
$handler->register(SIGINT, function () { echo "Interrupted !"; exit(); }, 'another namespace');
Unregister signals handler
Two ways are available to unregister signals., (*6)
By namespace :, (*7)
// unregister all handlers in "another namespace"
$handler->unregisterNamespace('another namespace');
// unregister all SIGINT handlers in "a namespace"
$handler->unregisterNamespace('a namespace', SIGINT);
By signal :, (*8)
// unregister all SIGINT handlers in any namespace
$handler->unregisterSignal(SIGINT);
License
This project is released under the MIT license., (*9)