1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* |
5
|
|
|
* @author Donii Sergii <[email protected]> |
6
|
|
|
* Date: 10/25/17 |
7
|
|
|
* Time: 11:56 AM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace sonrac\WAMP\Routers; |
11
|
|
|
|
12
|
|
|
use sonrac\WAMP\Contracts\WAMPRouterInterface; |
13
|
|
|
use Thruway\Event\ConnectionOpenEvent; |
14
|
|
|
use Thruway\Peer\ClientInterface; |
15
|
|
|
use Thruway\Peer\Router as PeerRouter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Router |
19
|
|
|
* Router class. |
20
|
|
|
* |
21
|
|
|
* <h2>Events list</h2> |
22
|
|
|
* <table class="table table-bordered table-stripped table-hover table-responsive"> |
23
|
|
|
* <thead> |
24
|
|
|
* <tr> |
25
|
|
|
* <td>Event name</td> |
26
|
|
|
* <td>Constant name</td> |
27
|
|
|
* <td>Description</td> |
28
|
|
|
* <td>Method for subscribe (is static for class <i>sonrac\WAMP\Routers\Router</i>)</td> |
29
|
|
|
* </tr> |
30
|
|
|
* </thead> |
31
|
|
|
* <tbody> |
32
|
|
|
* <tr> |
33
|
|
|
* <td>connection_open</td> |
34
|
|
|
* <td><i>sonrac\WAMP\Routers\Router::EVENT_CONNECTION_OPEN</i></td> |
35
|
|
|
* <td>Connection opened</td> |
36
|
|
|
* <td><i>sonrac\WAMP\Routers\Router::onOpen</i></td> |
37
|
|
|
* </tr> |
38
|
|
|
* </tbody> |
39
|
|
|
* </table> |
40
|
|
|
* |
41
|
|
|
* <h2>Example events adding</h2> |
42
|
|
|
* |
43
|
|
|
* <code> |
44
|
|
|
* $app->router->onOpen() |
45
|
|
|
* </code> |
46
|
|
|
*/ |
47
|
|
|
class Router extends PeerRouter implements WAMPRouterInterface |
48
|
|
|
{ |
49
|
|
|
use RouterTrait; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Connection open event name. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
* @const |
56
|
|
|
*/ |
57
|
|
|
const EVENT_CONNECTION_OPEN = 'connection_open'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Connection close event name. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
* @const |
64
|
|
|
*/ |
65
|
|
|
const EVENT_CONNECTION_CLOSE = 'connection_close'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Router start event name. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
* @const |
72
|
|
|
*/ |
73
|
|
|
const EVENT_ROUTER_START = 'router.start'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Router stop event name. |
77
|
|
|
* |
78
|
|
|
* @var string |
79
|
|
|
* @const |
80
|
|
|
*/ |
81
|
|
|
const EVENT_ROUTER_STOP = 'router.stop'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* RPC router. |
85
|
|
|
* |
86
|
|
|
* @var \sonrac\WAMP\Contracts\RPCRouterInterface |
87
|
|
|
*/ |
88
|
|
|
protected $rpcRouter; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Publisher/subscription router. |
92
|
|
|
* |
93
|
|
|
* @var \sonrac\WAMP\Contracts\PubSubRouterInterface |
94
|
|
|
*/ |
95
|
|
|
protected $pubSubRouter; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Loop object. |
99
|
|
|
* |
100
|
|
|
* @var null|\React\EventLoop\LoopInterface |
101
|
|
|
* |
102
|
|
|
* @author Donii Sergii <[email protected]> |
103
|
|
|
*/ |
104
|
|
|
protected $loop; |
105
|
|
|
/** |
106
|
|
|
* Routes. |
107
|
|
|
* |
108
|
|
|
* @var array |
109
|
|
|
*/ |
110
|
|
|
protected $routes = []; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Peer client. |
114
|
|
|
* |
115
|
|
|
* @var \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client |
116
|
|
|
* |
117
|
|
|
* @author Donii Sergii <[email protected]> |
118
|
|
|
*/ |
119
|
|
|
protected $client = null; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Default controller namespaces. |
123
|
|
|
* |
124
|
|
|
* @var null|string |
125
|
|
|
* |
126
|
|
|
* @author Donii Sergii <[email protected]> |
127
|
|
|
*/ |
128
|
|
|
protected $controllerNamespace = null; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Events. |
132
|
|
|
* |
133
|
|
|
* @var array |
134
|
|
|
* |
135
|
|
|
* @author Donii Sergii <[email protected]> |
136
|
|
|
*/ |
137
|
|
|
protected static $events = [ |
138
|
|
|
]; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Router constructor. |
142
|
|
|
* |
143
|
|
|
* @param \sonrac\WAMP\Contracts\RPCRouterInterface|\sonrac\WAMP\Routers\RPCRouter $RPCRouter RPC router |
144
|
|
|
* @param \sonrac\WAMP\Contracts\PubSubRouterInterface|\sonrac\WAMP\Routers\PubSubRouter $pubSubRouter Publisher/subscription |
145
|
|
|
* router |
146
|
|
|
* @param \React\EventLoop\LoopInterface|null $loop Loop object |
147
|
|
|
*/ |
148
|
21 |
|
public function __construct( |
149
|
|
|
\sonrac\WAMP\Contracts\RPCRouterInterface $RPCRouter, |
150
|
|
|
\sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter, |
151
|
|
|
\React\EventLoop\LoopInterface $loop = null |
152
|
|
|
) { |
153
|
21 |
|
$this->rpcRouter = $RPCRouter; |
154
|
21 |
|
$this->pubSubRouter = $pubSubRouter; |
155
|
21 |
|
$this->loop = $loop; |
156
|
|
|
|
157
|
21 |
|
parent::__construct($loop); |
158
|
21 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Add subscriber. |
162
|
|
|
* |
163
|
|
|
* @param string $path Route path |
164
|
|
|
* @param \Closure|string $callback Handler |
165
|
|
|
* |
166
|
|
|
* @return \React\Promise\Promise |
167
|
|
|
*/ |
168
|
1 |
|
public function addSubscriber($path, $callback) |
169
|
|
|
{ |
170
|
1 |
|
return $this->pubSubRouter->addRoute($path, $callback); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Add procedure. |
175
|
|
|
* |
176
|
|
|
* @param string $name Name |
177
|
|
|
* @param string|\Closure $procedure Procedure |
178
|
|
|
* |
179
|
|
|
* @return \React\Promise\Promise |
180
|
|
|
* |
181
|
|
|
* @author Donii Sergii <[email protected]> |
182
|
|
|
*/ |
183
|
|
|
public function addRoute($name, $procedure) |
184
|
|
|
{ |
185
|
|
|
return $this->rpcRouter->addRoute($name, $procedure); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Set default controller namespace. |
190
|
|
|
* |
191
|
|
|
* @param string $namespace |
192
|
|
|
* |
193
|
|
|
* @author Donii Sergii <[email protected]> |
194
|
|
|
*/ |
195
|
1 |
|
public function setControllerNamespace($namespace) |
196
|
|
|
{ |
197
|
1 |
|
$this->controllerNamespace = $namespace; |
198
|
1 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get default controller namespace. |
202
|
|
|
* |
203
|
|
|
* @return null|string |
204
|
|
|
* |
205
|
|
|
* @author Donii Sergii <[email protected]> |
206
|
|
|
*/ |
207
|
2 |
|
public function getControllerNamespace() |
208
|
|
|
{ |
209
|
2 |
|
return $this->controllerNamespace; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Add open event listener. |
214
|
|
|
* |
215
|
|
|
* @param \Closure|string $callback Callback |
216
|
|
|
* @param int $priority Callback priority |
217
|
|
|
* |
218
|
|
|
* @author Donii Sergii <[email protected]> |
219
|
|
|
*/ |
220
|
6 |
|
public function onConnectionOpen($callback, $priority = 0) |
221
|
|
|
{ |
222
|
6 |
|
$this->addEvent($callback, static::EVENT_CONNECTION_OPEN, $priority); |
223
|
6 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Add stop router event listener. |
227
|
|
|
* |
228
|
|
|
* @param \Closure|string $callback Callback |
229
|
|
|
* @param int $priority Callback priority |
230
|
|
|
* |
231
|
|
|
* @throws \Exception |
232
|
|
|
* |
233
|
|
|
* @author Donii Sergii <[email protected]> |
234
|
|
|
*/ |
235
|
1 |
|
public function onRouterStop($callback, $priority = 0) |
236
|
|
|
{ |
237
|
1 |
|
$this->addEvent($callback, static::EVENT_ROUTER_STOP, $priority); |
238
|
1 |
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Add connection close event listener. |
242
|
|
|
* |
243
|
|
|
* @param \Closure|string $callback Callback |
244
|
|
|
* @param int $priority Callback priority |
245
|
|
|
* |
246
|
|
|
* @throws \Exception |
247
|
|
|
* |
248
|
|
|
* @author Donii Sergii <[email protected]> |
249
|
|
|
*/ |
250
|
1 |
|
public function onConnectionClose($callback, $priority = 0) |
251
|
|
|
{ |
252
|
1 |
|
$this->addEvent($callback, static::EVENT_CONNECTION_CLOSE, $priority); |
253
|
1 |
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Remove event listener. |
257
|
|
|
* |
258
|
|
|
* @param string $eventName Event name |
259
|
|
|
* @param mixed $callback Callback |
260
|
|
|
* |
261
|
|
|
* @author Donii Sergii <[email protected]> |
262
|
|
|
*/ |
263
|
7 |
|
public function removeEvent($eventName, $callback) |
264
|
|
|
{ |
265
|
7 |
|
$this->getEventDispatcher()->removeListener($eventName, $callback); |
266
|
|
|
|
267
|
7 |
|
if (isset(static::$events[$eventName]) && count(static::$events[$eventName]) === 0) { |
268
|
|
|
unset(static::$events[$eventName]); |
269
|
|
|
} |
270
|
7 |
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Add open event listener. |
274
|
|
|
* |
275
|
|
|
* @param \Closure|string $callback Callback |
276
|
|
|
* @param int $priority Callback priority |
277
|
|
|
* |
278
|
|
|
* @throws \Exception |
279
|
|
|
* |
280
|
|
|
* @author Donii Sergii <[email protected]> |
281
|
|
|
*/ |
282
|
1 |
|
public function onRouterStart($callback, $priority = 0) |
283
|
|
|
{ |
284
|
1 |
|
$this->addEvent($callback, static::EVENT_ROUTER_START, $priority); |
285
|
1 |
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Get client. |
289
|
|
|
* |
290
|
|
|
* @return \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client |
291
|
|
|
* |
292
|
|
|
* @author Donii Sergii <[email protected]> |
293
|
|
|
*/ |
294
|
7 |
|
public function getClient() |
295
|
|
|
{ |
296
|
7 |
|
return $this->client; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Set client |
301
|
|
|
* |
302
|
|
|
* @param \Thruway\Peer\ClientInterface $client Client |
303
|
|
|
* |
304
|
|
|
* @author Donii Sergii <[email protected]> |
305
|
|
|
*/ |
306
|
7 |
|
public function setClient(ClientInterface $client) |
307
|
|
|
{ |
308
|
7 |
|
$this->client = $client; |
309
|
7 |
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* {@inheritdoc} |
313
|
|
|
*/ |
314
|
1 |
|
public function parseGroups() |
315
|
|
|
{ |
316
|
1 |
|
$this->rpcRouter->parseGroups(); |
|
|
|
|
317
|
1 |
|
$this->pubSubRouter->parseGroups(); |
|
|
|
|
318
|
1 |
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Add event. |
322
|
|
|
* |
323
|
|
|
* @param \Closure|string $callback Callback |
324
|
|
|
* @param string $eventName Event name |
325
|
|
|
* @param int $priority Priority |
326
|
|
|
* |
327
|
|
|
* @author Donii Sergii <[email protected]> |
328
|
|
|
*/ |
329
|
9 |
|
protected function addEvent($callback, $eventName, $priority) |
330
|
|
|
{ |
331
|
9 |
|
if (is_string($callback)) { |
332
|
|
|
list($class, $method) = explode('&', $callback); |
333
|
|
|
|
334
|
|
|
$callback = function (ConnectionOpenEvent $event) use ($class, $method) { |
335
|
|
|
return $class::{$method}($event); |
336
|
|
|
}; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
$this->getEventDispatcher()->addListener($eventName, $callback, $priority); |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|