Skip to content

Commit

Permalink
Fixes for PcntlSingals in windows (#20)
Browse files Browse the repository at this point in the history
fixes for windows compatible
  • Loading branch information
Mararok authored Oct 18, 2016
1 parent b3f67b9 commit ba7e9bf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
54 changes: 42 additions & 12 deletions src/SAREhub/Commons/Process/PcntlSignals.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

namespace SAREhub\Commons\Process;

if (PcntlSignals::isSupported()) {
define('PcntlSignals_SIGHUP', \SIGHUP);
define('PcntlSignals_SIGINT', \SIGINT);
define('PcntlSignals_SIGTERM', \SIGTERM);
define('PcntlSignals_SIGPIPE', \SIGPIPE);
define('PcntlSignals_SIGUSR1', \SIGUSR1);
} else {
define('PcntlSignals_SIGHUP', 1);
define('PcntlSignals_SIGINT', 2);
define('PcntlSignals_SIGTERM', 15);
define('PcntlSignals_SIGPIPE', 13);
define('PcntlSignals_SIGUSR1', 10);
}

/**
* Helper class for handling linux signals.
* Installed signals:
Expand All @@ -13,6 +27,12 @@
*/
class PcntlSignals {

const SIGHUP = PcntlSignals_SIGHUP;
const SIGINT = PcntlSignals_SIGINT;
const SIGTERM = PcntlSignals_SIGTERM;
const SIGPIPE = PcntlSignals_SIGPIPE;
const SIGUSR1 = PcntlSignals_SIGUSR1;

const DEFAULT_NAMESPACE = 'default';

protected $handlers = [];
Expand All @@ -38,15 +58,17 @@ public static function getGlobal() {
* 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);
if (self::isSupported()) {
$handler = function ($signal) {
$this->dispatchSignal($signal);
};

\pcntl_signal(self::SIGHUP, $handler);
\pcntl_signal(self::SIGINT, $handler);
\pcntl_signal(self::SIGTERM, $handler);
\pcntl_signal(self::SIGPIPE, $handler);
\pcntl_signal(self::SIGUSR1, $handler);
}
}

/**
Expand Down Expand Up @@ -99,11 +121,19 @@ 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();
public static function checkPendingSignals() {
if (self::isSupported()) {
\pcntl_signal_dispatch();
}
}

/**
* @return bool
*/
public static function isSupported() {
return !defined('PHP_WINDOWS_VERSION_MAJOR');
}
}
2 changes: 1 addition & 1 deletion tests/SAREhub/Commons/Process/PcntlSignalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected function setUp() {

public function testHandle() {
$handlerMock = $this->createSignalHandler();
$this->signals->handle(2, $handlerMock);
$this->signals->handle(PcntlSignals::SIGINT, $handlerMock);
$this->assertEquals([
2 => [
PcntlSignals::DEFAULT_NAMESPACE => [$handlerMock]
Expand Down

0 comments on commit ba7e9bf

Please sign in to comment.