|
|
@@ -24,49 +24,45 @@ class SignalHandler
|
|
|
{
|
|
|
private $logger;
|
|
|
|
|
|
- private $previousSignalHandler = array();
|
|
|
- private $signalLevelMap = array();
|
|
|
- private $signalRestartSyscalls = array();
|
|
|
+ private $previousSignalHandler = [];
|
|
|
+ private $signalLevelMap = [];
|
|
|
+ private $signalRestartSyscalls = [];
|
|
|
|
|
|
public function __construct(LoggerInterface $logger)
|
|
|
{
|
|
|
$this->logger = $logger;
|
|
|
}
|
|
|
|
|
|
- public function registerSignalHandler($signo, $level = LogLevel::CRITICAL, bool $callPrevious = true, bool $restartSyscalls = true, bool $async = true): self
|
|
|
+ public function registerSignalHandler($signo, $level = LogLevel::CRITICAL, bool $callPrevious = true, bool $restartSyscalls = true, ?bool $async = true): self
|
|
|
{
|
|
|
if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) {
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
if ($callPrevious) {
|
|
|
- if (function_exists('pcntl_signal_get_handler')) {
|
|
|
- $handler = pcntl_signal_get_handler($signo);
|
|
|
- if ($handler === false) {
|
|
|
- return $this;
|
|
|
- }
|
|
|
- $this->previousSignalHandler[$signo] = $handler;
|
|
|
- } else {
|
|
|
- $this->previousSignalHandler[$signo] = true;
|
|
|
+ $handler = pcntl_signal_get_handler($signo);
|
|
|
+ if ($handler === false) {
|
|
|
+ return $this;
|
|
|
}
|
|
|
+ $this->previousSignalHandler[$signo] = $handler;
|
|
|
} else {
|
|
|
unset($this->previousSignalHandler[$signo]);
|
|
|
}
|
|
|
$this->signalLevelMap[$signo] = $level;
|
|
|
$this->signalRestartSyscalls[$signo] = $restartSyscalls;
|
|
|
|
|
|
- if (function_exists('pcntl_async_signals') && $async !== null) {
|
|
|
+ if ($async !== null) {
|
|
|
pcntl_async_signals($async);
|
|
|
}
|
|
|
|
|
|
- pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls);
|
|
|
+ pcntl_signal($signo, [$this, 'handleSignal'], $restartSyscalls);
|
|
|
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
public function handleSignal($signo, array $siginfo = null)
|
|
|
{
|
|
|
- static $signals = array();
|
|
|
+ static $signals = [];
|
|
|
|
|
|
if (!$signals && extension_loaded('pcntl')) {
|
|
|
$pcntl = new ReflectionExtension('pcntl');
|
|
|
@@ -86,7 +82,7 @@ class SignalHandler
|
|
|
|
|
|
$level = isset($this->signalLevelMap[$signo]) ? $this->signalLevelMap[$signo] : LogLevel::CRITICAL;
|
|
|
$signal = isset($signals[$signo]) ? $signals[$signo] : $signo;
|
|
|
- $context = isset($siginfo) ? $siginfo : array();
|
|
|
+ $context = isset($siginfo) ? $siginfo : [];
|
|
|
$this->logger->log($level, sprintf('Program received signal %s', $signal), $context);
|
|
|
|
|
|
if (!isset($this->previousSignalHandler[$signo])) {
|
|
|
@@ -95,21 +91,18 @@ class SignalHandler
|
|
|
|
|
|
if ($this->previousSignalHandler[$signo] === true || $this->previousSignalHandler[$signo] === SIG_DFL) {
|
|
|
if (extension_loaded('pcntl') && function_exists('pcntl_signal') && function_exists('pcntl_sigprocmask') && function_exists('pcntl_signal_dispatch')
|
|
|
- && extension_loaded('posix') && function_exists('posix_getpid') && function_exists('posix_kill')) {
|
|
|
- $restartSyscalls = isset($this->restartSyscalls[$signo]) ? $this->restartSyscalls[$signo] : true;
|
|
|
- pcntl_signal($signo, SIG_DFL, $restartSyscalls);
|
|
|
- pcntl_sigprocmask(SIG_UNBLOCK, array($signo), $oldset);
|
|
|
- posix_kill(posix_getpid(), $signo);
|
|
|
- pcntl_signal_dispatch();
|
|
|
- pcntl_sigprocmask(SIG_SETMASK, $oldset);
|
|
|
- pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls);
|
|
|
- }
|
|
|
- } elseif (is_callable($this->previousSignalHandler[$signo])) {
|
|
|
- if (PHP_VERSION_ID >= 70100) {
|
|
|
- $this->previousSignalHandler[$signo]($signo, $siginfo);
|
|
|
- } else {
|
|
|
- $this->previousSignalHandler[$signo]($signo);
|
|
|
+ && extension_loaded('posix') && function_exists('posix_getpid') && function_exists('posix_kill')
|
|
|
+ ) {
|
|
|
+ $restartSyscalls = isset($this->restartSyscalls[$signo]) ? $this->restartSyscalls[$signo] : true;
|
|
|
+ pcntl_signal($signo, SIG_DFL, $restartSyscalls);
|
|
|
+ pcntl_sigprocmask(SIG_UNBLOCK, [$signo], $oldset);
|
|
|
+ posix_kill(posix_getpid(), $signo);
|
|
|
+ pcntl_signal_dispatch();
|
|
|
+ pcntl_sigprocmask(SIG_SETMASK, $oldset);
|
|
|
+ pcntl_signal($signo, [$this, 'handleSignal'], $restartSyscalls);
|
|
|
}
|
|
|
+ } elseif (is_callable($this->previousSignalHandler[$signo])) {
|
|
|
+ $this->previousSignalHandler[$signo]($signo, $siginfo);
|
|
|
}
|
|
|
}
|
|
|
}
|