PHP code example of temkaa / signal

1. Go to this page and download the library: Download temkaa/signal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

temkaa / signal example snippets




use Temkaa\Signal\SignalSubscriberInterface;
use Temkaa\Signal\SignalManager;
use const PHP_EOL;
use const SIGTERM;

final class SigtermListener implements SignalSubscriberInterface
{
    private bool $isCalled = false;
 
    public function handle(): void
    {
        $this->isCalled = true;

        echo 'Got sigterm signal, closing database connection...'.PHP_EOL;
    }
    
    public function isCalled(): bool
    {
        return $this->isCalled;    
    }
}

$sigtermListener = new SigtermListener();

$signalManager = new SignalManager();
$signalManager->subscribe($sigtermListener, SIGTERM);

posix_kill(posix_getpid(), SIGTERM);

// $sigtermListener->isCalled is true at this point
assert($sigtermListener->isCalled() === true);



use Temkaa\Signal\SignalSubscriberInterface;
use Temkaa\Signal\SignalManager;
use const PHP_EOL;
use const SIGTERM;

final class SigtermListener implements SignalSubscriberInterface
{
    public function handle(): void
    {
        echo 'Got sigterm signal, closing database connection...'.PHP_EOL;
    }
}

$sigtermListener = new SigtermListener();

$signalManager = new SignalManager();
$signalManager->subscribe($sigtermListener, SIGTERM);
assert($signalManager->hasSubscriber(SIGTERM) === true);
assert($signalManager->getSubscriber(SIGTERM) === $sigtermListener);

$signalManager->unsubscribe(SIGTERM);
assert($signalManager->hasSubscriber(SIGTERM) === false);