php-ipc/src/Signal/SignalHandler.php

56 lines
1.0 KiB
PHP

<?php
namespace NoccyLabs\Ipc\Signal;
class SignalHandler
{
private $signo;
private $callbacks;
/**
* Constructor, should only be called once for each signal but can have
* multiple handlers attached.
*
* @param integer $signo
* @param array $callbacks
*/
public function __construct(int $signo, array $callbacks=[])
{
$this->signo = $signo;
$this->callbacks = $callbacks;
pcntl_signal($this->signo, [ $this, "onSignal" ]);
}
public function __destruct()
{
}
/**
* Append a handler to the signal
*
* @param callable $handler
* @return void
*/
public function addHandler(callable $handler):void
{
$this->callbacks[] = $handler;
}
/**
* Callback for signals
*
* @param int $signo
* @return void
*/
public function onSignal($signo)
{
foreach ($this->callbacks as $callback) {
if (call_user_func($callback) === true)
return;
}
}
}