Completed
Push — master ( bbdfdc...3f4834 )
by Taosikai
26:24 queued 11:29
created

SignalHandler.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Process Library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Process;
7
8
use Slince\Process\Exception\InvalidArgumentException;
9
10
class SignalHandler
11
{
12
    /**
13
     * The handlers
14
     * @var array
15
     */
16
    protected $handlers = [];
17
18
    /**
19
     * Registers a callback for some signals
20
     * @param int|array $signals a signal or an array of signals
21
     * @param callable|int $callback a callback
22
     * @return SignalHandler;
0 ignored issues
show
The doc-type SignalHandler; could not be parsed: Expected "|" or "end of type", but got ";" at position 13. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
23
     */
24
    public function register($signals, $callback)
25
    {
26
        if (!is_array($signals)) {
27
            $signals = [$signals];
28
        }
29
        foreach ($signals as $signal) {
30
            $this->setSignalHandler($signal, $callback);
31
        }
32
        return $this;
33
    }
34
35
    /**
36
     * Register a callback for
37
     * @param $signal
38
     * @param int|callable $handler
39
     */
40
    protected function setSignalHandler($signal, $handler)
41
    {
42
        if (!is_int($handler) && !is_callable($handler)) {
43
            throw new InvalidArgumentException('The signal handler should be called or a number');
44
        }
45
        $this->handlers[$signal] = $handler;
46
        pcntl_signal($signal, $handler);
47
    }
48
49
    /**
50
     * Gets the handler for a signal
51
     * @param $signal
52
     * @return int|string
53
     */
54
    public function getHandler($signal)
55
    {
56
        if (function_exists('pcntl_signal_get_handler')) {
57
            return pcntl_signal_get_handler($signal);
58
        }
59
        return isset($this->handlers[$signal]) ? $this->handlers[$signal] : null;
60
    }
61
62
63
    /**
64
     * Creates a handler
65
     * @param boolean $disableDeclareTicks disable declare(tickets = 1)
66
     * @return SignalHandler
67
     */
68
    public static function create($disableDeclareTicks = false)
0 ignored issues
show
The parameter $disableDeclareTicks is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        $handler = new static();
71
        if (function_exists('pcntl_async_signals')) {
72
            pcntl_async_signals(true);
73
        } else {
74
            declare (ticks = 1);
75
        }
76
        return $handler;
77
    }
78
}