Initial commit

This commit is contained in:
2018-04-15 16:41:46 +02:00
commit 86ff40274b
29 changed files with 1368 additions and 0 deletions

24
src/Signal/Signal.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace NoccyLabs\Ipc\Signal;
class Signal
{
private $signo;
public function __construct(int $signo)
{
$this->signo = $signo;
}
public function setHandler(callable $handler):void
{
pcntl_signal($this->signo, $handler);
}
public function dispatch($pid):bool
{
return posix_kill($pid, $this->signo);
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace NoccyLabs\Ipc\Signal;
class SignalHandler
{
private $signo;
private $callbacks;
public function __construct(int $signo, array $callbacks=[])
{
$this->signo = $signo;
$this->callbacks = $callbacks;
pcntl_signal($this->signo, [ $this, "onSignal" ]);
}
public function __destruct()
{
}
public function addHandler(callable $handler):void
{
$this->callbacks[] = $handler;
}
public function onSignal($signo)
{
foreach ($this->callbacks as $callback) {
if (call_user_func($callback) === true)
return;
}
}
}

32
src/Signal/SignalTrap.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace NoccyLabs\Ipc\Signal;
class SignalTrap
{
protected $signal;
protected $trapped = false;
public function __construct(int $signo)
{
$this->signal = new Signal($signo);
$this->signal->setHandler([ $this, "onSignal" ]);
}
public function onSignal()
{
$this->trapped = true;
}
public function isTrapped($reset=true):bool
{
if ($this->trapped) {
$reset && ($this->trapped = false);
return true;
}
return false;
}
}