1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Http; |
4
|
|
|
|
5
|
|
|
use Dazzle\Socket\SocketListenerInterface; |
6
|
|
|
use Dazzle\Loop\LoopInterface; |
7
|
|
|
use Dazzle\Throwable\Exception\Logic\InstantiationException; |
8
|
|
|
use Dazzle\Http\Http\Component\Router\HttpRouter; |
9
|
|
|
use Dazzle\Http\Http\Component\Router\HttpRouterInterface; |
10
|
|
|
use Dazzle\Http\Http\HttpServer; |
11
|
|
|
use Dazzle\Http\Socket\Component\Firewall\SocketFirewall; |
12
|
|
|
use Dazzle\Http\Socket\SocketServer; |
13
|
|
|
use Dazzle\Http\Socket\SocketServerInterface; |
14
|
|
|
use Error; |
15
|
|
|
use Exception; |
16
|
|
|
|
17
|
|
|
class NetworkServer implements NetworkServerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var SocketListenerInterface |
21
|
|
|
*/ |
22
|
|
|
protected $listener; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var SocketServerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $server; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var HttpServer |
31
|
|
|
*/ |
32
|
|
|
protected $http; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var SocketFirewall|null |
36
|
|
|
*/ |
37
|
|
|
protected $firewall; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var HttpRouterInterface |
41
|
|
|
*/ |
42
|
|
|
public $router; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param SocketListenerInterface $listener |
46
|
|
|
* @param mixed[] $params |
47
|
|
|
* @throws InstantiationException |
48
|
|
|
*/ |
49
|
20 |
|
public function __construct(SocketListenerInterface $listener, $params = []) |
50
|
|
|
{ |
51
|
|
|
try |
52
|
|
|
{ |
53
|
20 |
|
$router = new HttpRouter( |
54
|
20 |
|
$http = new HttpServer( |
55
|
20 |
|
$server = new SocketServer($listener) |
56
|
|
|
), |
57
|
20 |
|
$params |
58
|
|
|
); |
59
|
|
|
|
60
|
20 |
|
$this->listener = $listener; |
61
|
20 |
|
$this->server = $server; |
62
|
20 |
|
$this->http = $http; |
63
|
20 |
|
$this->firewall = null; |
64
|
20 |
|
$this->router = $router; |
65
|
|
|
} |
66
|
|
|
catch (Error $ex) |
67
|
|
|
{ |
68
|
|
|
throw new InstantiationException("[" . __CLASS__ . "] could not be created.", 0, $ex); |
69
|
|
|
} |
70
|
|
|
catch (Exception $ex) |
71
|
|
|
{ |
72
|
|
|
throw new InstantiationException("[" . __CLASS__ . "] could not be created.", 0, $ex); |
73
|
|
|
} |
74
|
20 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
1 |
|
public function __destruct() |
80
|
|
|
{ |
81
|
1 |
|
unset($this->router); |
82
|
1 |
|
unset($this->firewall); |
83
|
1 |
|
unset($this->http); |
84
|
1 |
|
unset($this->server); |
85
|
1 |
|
unset($this->listener); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @override |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
1 |
|
public function existsRoute($path) |
93
|
|
|
{ |
94
|
1 |
|
return $this->router->existsRoute($path); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @override |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
2 |
|
public function addRoute($path, NetworkComponentInterface $component) |
102
|
|
|
{ |
103
|
2 |
|
return $this->router->addRoute($path, $component); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @override |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
1 |
|
public function removeRoute($path) |
111
|
|
|
{ |
112
|
1 |
|
return $this->router->removeRoute($path); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @override |
117
|
|
|
* @inheritDoc |
118
|
|
|
*/ |
119
|
1 |
|
public function blockAddress($address) |
120
|
|
|
{ |
121
|
1 |
|
if ($this->firewall === null) |
122
|
|
|
{ |
123
|
|
|
$this->createFirewall(); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
$this->firewall->blockAddress($address); |
127
|
|
|
|
128
|
1 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @override |
133
|
|
|
* @inheritDoc |
134
|
|
|
*/ |
135
|
1 |
|
public function unblockAddress($address) |
136
|
|
|
{ |
137
|
1 |
|
if ($this->firewall === null) |
138
|
|
|
{ |
139
|
|
|
$this->createFirewall(); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
$this->firewall->unblockAddress($address); |
143
|
|
|
|
144
|
1 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @override |
149
|
|
|
* @inheritDoc |
150
|
|
|
*/ |
151
|
2 |
|
public function isAddressBlocked($address) |
152
|
|
|
{ |
153
|
2 |
|
if ($this->firewall === null) |
154
|
|
|
{ |
155
|
1 |
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
return $this->firewall->isAddressBlocked($address); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @override |
163
|
|
|
* @inheritDoc |
164
|
|
|
*/ |
165
|
2 |
|
public function getBlockedAddresses() |
166
|
|
|
{ |
167
|
2 |
|
if ($this->firewall === null) |
168
|
|
|
{ |
169
|
1 |
|
return []; |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
return $this->firewall->getBlockedAddresses(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @override |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
1 |
|
public function stop() |
180
|
|
|
{ |
181
|
1 |
|
$this->listener->close(); |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @override |
186
|
|
|
* @inheritDoc |
187
|
|
|
*/ |
188
|
1 |
|
public function close() |
189
|
|
|
{ |
190
|
1 |
|
$this->listener->close(); |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @override |
195
|
|
|
* @inheritDoc |
196
|
|
|
*/ |
197
|
1 |
|
public function getLoop() |
198
|
|
|
{ |
199
|
1 |
|
return $this->listener->getLoop(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @override |
204
|
|
|
* @inheritDoc |
205
|
|
|
*/ |
206
|
1 |
|
public function setLoop(LoopInterface $loop = null) |
207
|
|
|
{ |
208
|
1 |
|
$this->listener->setLoop($loop); |
209
|
1 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @override |
213
|
|
|
* @inheritDoc |
214
|
|
|
*/ |
215
|
1 |
|
public function isPaused() |
216
|
|
|
{ |
217
|
1 |
|
return $this->listener->isPaused(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @override |
222
|
|
|
* @inheritDoc |
223
|
|
|
*/ |
224
|
1 |
|
public function pause() |
225
|
|
|
{ |
226
|
1 |
|
$this->listener->pause(); |
227
|
1 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @override |
231
|
|
|
* @inheritDoc |
232
|
|
|
*/ |
233
|
1 |
|
public function resume() |
234
|
|
|
{ |
235
|
1 |
|
$this->listener->resume(); |
236
|
1 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Create and attach firewall to transfer server Firewall. |
240
|
|
|
*/ |
241
|
1 |
|
protected function createFirewall() |
242
|
|
|
{ |
243
|
1 |
|
$this->firewall = new SocketFirewall($this->server, $this->http); |
|
|
|
|
244
|
1 |
|
} |
245
|
|
|
} |
246
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: