-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from SAREhub/future/pcntl_signal
added PcntlSignals helper class
- Loading branch information
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
use SAREhub\Commons\Process\PcntlSignals; | ||
|
||
echo "PcntlSignals example\n\n"; | ||
echo "press CTRL+C to call SIGINT signal handler \n"; | ||
|
||
PcntlSignals::get()->handle(\SIGINT, function () { | ||
echo "\nkilled via signal\n"; | ||
exit(0); | ||
})->install(); | ||
|
||
while (true) { | ||
PcntlSignals::get()->checkPendingSignals(); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\Process; | ||
|
||
/** | ||
* Helper class for handling linux signals. | ||
* Installed signals: | ||
* SIGHUP | ||
* SIGINT | ||
* SIGTERM | ||
* SIGPIPE | ||
* SIGUSR1 | ||
*/ | ||
class PcntlSignals { | ||
|
||
const DEFAULT_NAMESPACE = 'default'; | ||
|
||
protected $handlers = []; | ||
|
||
private static $instance = null; | ||
|
||
public function __construct() { | ||
|
||
} | ||
|
||
/** | ||
* @return PcntlSignals | ||
*/ | ||
public static function getGlobal() { | ||
if (self::$instance === null) { | ||
self::$instance = new self(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
/** | ||
* Installs pcntl signals. | ||
*/ | ||
public function install() { | ||
$handler = function ($signal) { | ||
$this->dispatchSignal($signal); | ||
}; | ||
|
||
\pcntl_signal(\SIGHUP, $handler); | ||
\pcntl_signal(\SIGINT, $handler); | ||
\pcntl_signal(\SIGTERM, $handler); | ||
\pcntl_signal(\SIGPIPE, $handler); | ||
\pcntl_signal(\SIGUSR1, $handler); | ||
} | ||
|
||
/** | ||
* Registers handler for signal in selected namespace. | ||
* @param int $signal Signal id (\SIG* constants) | ||
* @param callable $handler Handler callback | ||
* @param string $namespace | ||
* @return $this | ||
*/ | ||
public function handle($signal, callable $handler, $namespace = self::DEFAULT_NAMESPACE) { | ||
if (empty($this->handlers[$signal])) { | ||
$this->handlers[$signal] = []; | ||
} | ||
|
||
if (empty($this->handlers[$signal][$namespace])) { | ||
$this->handlers[$signal][$namespace] = []; | ||
} | ||
|
||
$this->handlers[$signal][$namespace][] = $handler; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Dispatch signal on registered handlers. | ||
* @param int $signal | ||
* @return $this | ||
*/ | ||
public function dispatchSignal($signal) { | ||
if (isset($this->handlers[$signal])) { | ||
foreach ($this->handlers[$signal] as $namespaceHandlers) { | ||
foreach ($namespaceHandlers as $handler) { | ||
$handler(); | ||
} | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns all registered handlers. | ||
* @return array | ||
*/ | ||
public function getHandlers() { | ||
return $this->handlers; | ||
} | ||
|
||
public function getHandlersForSignal($signal) { | ||
return isset($this->handlers[$signal]) ? $this->handlers[$signal] : []; | ||
} | ||
|
||
|
||
/** | ||
* Calls pcntl_signal_dispatch for process pending signals. | ||
*/ | ||
public function checkPendingSignals() { | ||
pcntl_signal_dispatch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace SAREhub\Commons\Process; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class PcntlSignalsTest extends TestCase { | ||
|
||
/** @var PcntlSignals */ | ||
private $signals; | ||
|
||
protected function setUp() { | ||
$this->signals = new PcntlSignals(); | ||
} | ||
|
||
public function testHandle() { | ||
$handlerMock = $this->createSignalHandler(); | ||
$this->signals->handle(2, $handlerMock); | ||
$this->assertEquals([ | ||
2 => [ | ||
PcntlSignals::DEFAULT_NAMESPACE => [$handlerMock] | ||
] | ||
], $this->signals->getHandlers()); | ||
} | ||
|
||
public function testHandleSameSignal() { | ||
$handlerMock = $this->createSignalHandler(); | ||
$this->signals->handle(2, $handlerMock); | ||
$handlerMock2 = $this->createSignalHandler(); | ||
$this->signals->handle(2, $handlerMock2); | ||
|
||
$this->assertEquals([ | ||
2 => [ | ||
PcntlSignals::DEFAULT_NAMESPACE => [$handlerMock, $handlerMock2] | ||
] | ||
], $this->signals->getHandlers()); | ||
} | ||
|
||
public function testGetHandlersForSignal() { | ||
$handlerMock = $this->createSignalHandler(); | ||
$signals = new PcntlSignals(); | ||
$signals->handle(2, $handlerMock); | ||
$signals->handle(5, $this->createSignalHandler()); | ||
$this->assertEquals([ | ||
PcntlSignals::DEFAULT_NAMESPACE => [$handlerMock] | ||
], $signals->getHandlersForSignal(2)); | ||
} | ||
|
||
public function testDispatchSignal() { | ||
$handlerMock = $this->createSignalHandler(); | ||
$handlerMock->expects($this->once())->method('__invoke'); | ||
$this->signals->handle(2, $handlerMock); | ||
$this->signals->dispatchSignal(2); | ||
} | ||
|
||
private function createSignalHandler() { | ||
return $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock(); | ||
} | ||
} |