php-ipc/examples/signals.php

29 lines
790 B
PHP
Raw Normal View History

<?php
require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Ipc\Signal\SignalHandler;
use NoccyLabs\Ipc\Signal\Signal;
$handler = new SignalHandler(SIGUSR1);
// Add handlers like this, or as an array passed as the second argument to the constructor
$handler->addHandler(function () {
echo "First handler\n";
});
// If a handler returns true, it will prevent any further handlers from firing
$handler->addHandler(function () {
echo "Second and final handler\n";
return true;
});
// Thus, this one will not be called
$handler->addHandler(function() {
echo "Third handler, never called\n";
});
// Dispatch the signal to ourselves
(new Signal(SIGUSR1))->dispatch(posix_getpid());
// Default target of dispatch is the own pid
(new Signal(SIGUSR1))->dispatch();