-
Notifications
You must be signed in to change notification settings - Fork 42
/
AbstractSignalManager.php
201 lines (172 loc) · 4.7 KB
/
AbstractSignalManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
* This file is part of Icicle, a library for writing asynchronous code in PHP using promises and coroutines.
*
* @copyright 2014-2015 Aaron Piotrowski. All rights reserved.
* @license Apache-2.0 See the LICENSE file that was distributed with this source code for more information.
*/
namespace Icicle\Loop\Manager;
use Icicle\Loop\Events\EventFactoryInterface;
use Icicle\Loop\Events\SignalInterface;
use Icicle\Loop\Exception\InvalidSignalError;
use Icicle\Loop\LoopInterface;
abstract class AbstractSignalManager implements SignalManagerInterface
{
/**
* @var \Icicle\Loop\LoopInterface
*/
private $loop;
/**
* @var \Icicle\Loop\Events\EventFactoryInterface
*/
private $factory;
/**
* @var \SplObjectStorage[]
*/
private $signals = [];
/**
* @param \Icicle\Loop\LoopInterface $loop
* @param \Icicle\Loop\Events\EventFactoryInterface $factory
*/
public function __construct(LoopInterface $loop, EventFactoryInterface $factory)
{
$this->loop = $loop;
$this->factory = $factory;
foreach ($this->getSignalList() as $signo) {
$this->signals[$signo] = new \SplObjectStorage();
}
}
/**
* {@inheritdoc}
*/
public function create($signo, callable $callback, array $args = [])
{
if (!isset($this->signals[$signo])) {
throw new InvalidSignalError(sprintf('Invalid signal number: %d.', $signo));
}
$signal = $this->factory->signal($this, $signo, $callback);
$this->signals[$signo]->attach($signal);
return $signal;
}
public function enable(SignalInterface $signal)
{
$signo = $signal->getSignal();
if (isset($this->signals[$signo]) && !$this->signals[$signo]->contains($signal)) {
$this->signals[$signo]->attach($signal);
}
}
/**
* {@inheritdoc}
*/
public function disable(SignalInterface $signal)
{
$signo = $signal->getSignal();
if (isset($this->signals[$signo]) && $this->signals[$signo]->contains($signal)) {
$this->signals[$signo]->detach($signal);
}
}
/**
* {@inheritdoc}
*/
public function isEnabled(SignalInterface $signal)
{
$signo = $signal->getSignal();
return isset($this->signals[$signo]) && $this->signals[$signo]->contains($signal);
}
/**
* {@inheritdoc}
*/
public function clear()
{
foreach ($this->signals as $signo => $signals) {
$this->signals[$signo] = new \SplObjectStorage();
}
}
/**
* Returns an array of signals to be handled. Exploits the fact that PHP will not notice the signal constants are
* undefined if the pcntl extension is not installed.
*
* @return int[]
*/
protected function getSignalList()
{
$signals = [
SIGHUP,
SIGINT,
SIGQUIT,
SIGILL,
SIGABRT,
SIGTRAP,
SIGBUS,
SIGTERM,
SIGSEGV,
SIGFPE,
SIGALRM,
SIGVTALRM,
SIGPROF,
SIGIO,
SIGCONT,
SIGURG,
SIGPIPE,
SIGXCPU,
SIGXFSZ,
SIGTTIN,
SIGTTOU,
SIGUSR1,
SIGUSR2,
];
if (defined('SIGIOT')) {
$signals[] = SIGIOT;
}
if (defined('SIGSTKFLT')) {
$signals[] = SIGSTKFLT;
}
if (defined('SIGCLD')) {
$signals[] = SIGCLD;
}
if (defined('SIGCHLD')) {
$signals[] = SIGCHLD;
}
return $signals;
}
/**
* Creates callback function for handling signals.
*
* @return callable
*/
protected function createSignalCallback()
{
return function ($signo) {
$handled = false;
foreach ($this->signals[$signo] as $signal) {
$handled = true;
$signal->call();
}
switch ($signo) {
case SIGHUP:
case SIGINT:
case SIGQUIT:
case SIGABRT:
case SIGTRAP:
case SIGXCPU:
if (!$handled) {
$this->loop->stop();
}
break;
case SIGTERM:
case SIGBUS:
case SIGSEGV:
case SIGFPE:
$this->loop->stop();
break;
}
};
}
/**
* @return \Icicle\Loop\LoopInterface
*/
protected function getLoop()
{
return $this->loop;
}
}