Issues (61)

src/Routers/Router.php (3 issues)

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;
0 ignored issues
show
The trait sonrac\WAMP\Routers\RouterTrait requires the property $wampRouter which is not provided by sonrac\WAMP\Routers\Router.
Loading history...
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
     * Callback groups
132
     *
133
     * @var array
134
     *
135
     * @author Donii Sergii <[email protected]>
136
     */
137
    protected $groups = [];
138
139
    /**
140
     * Events.
141
     *
142
     * @var array
143
     *
144
     * @author Donii Sergii <[email protected]>
145
     */
146
    protected static $events = [
147
    ];
148
149
    /**
150
     * Router constructor.
151
     *
152
     * @param \sonrac\WAMP\Contracts\RPCRouterInterface|\sonrac\WAMP\Routers\RPCRouter       $RPCRouter    RPC router
153
     * @param \sonrac\WAMP\Contracts\PubSubRouterInterface|\sonrac\WAMP\Routers\PubSubRouter $pubSubRouter Publisher/subscription
154
     *                                                                                                     router
155
     * @param \React\EventLoop\LoopInterface|null                                            $loop         Loop object
156
     */
157 21
    public function __construct(
158
        \sonrac\WAMP\Contracts\RPCRouterInterface $RPCRouter,
159
        \sonrac\WAMP\Contracts\PubSubRouterInterface $pubSubRouter,
160
        \React\EventLoop\LoopInterface $loop = null
161
    ) {
162 21
        $this->rpcRouter = $RPCRouter;
163 21
        $this->pubSubRouter = $pubSubRouter;
164 21
        $this->loop = $loop;
165
166 21
        parent::__construct($loop);
167 21
    }
168
169
    /**
170
     * Add subscriber.
171
     *
172
     * @param string          $path     Route path
173
     * @param \Closure|string $callback Handler
174
     *
175
     * @return \React\Promise\Promise
176
     */
177 1
    public function addSubscriber($path, $callback)
178
    {
179 1
        return $this->pubSubRouter->addRoute($path, $callback);
0 ignored issues
show
The method addRoute() does not exist on sonrac\WAMP\Contracts\PubSubRouterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to sonrac\WAMP\Contracts\PubSubRouterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        return $this->pubSubRouter->/** @scrutinizer ignore-call */ addRoute($path, $callback);
Loading history...
180
    }
181
182
    /**
183
     * Add procedure.
184
     *
185
     * @param string          $name      Name
186
     * @param string|\Closure $procedure Procedure
187
     *
188
     * @return \React\Promise\Promise
189
     *
190
     * @author Donii Sergii <[email protected]>
191
     */
192
    public function addRoute($name, $procedure)
193
    {
194
        return $this->rpcRouter->addRoute($name, $procedure);
0 ignored issues
show
The method addRoute() does not exist on sonrac\WAMP\Contracts\RPCRouterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to sonrac\WAMP\Contracts\RPCRouterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

194
        return $this->rpcRouter->/** @scrutinizer ignore-call */ addRoute($name, $procedure);
Loading history...
195
    }
196
197
    /**
198
     * Get default controller namespace.
199
     *
200
     * @return null|string
201
     *
202
     * @author Donii Sergii <[email protected]>
203
     */
204 2
    public function getControllerNamespace()
205
    {
206 2
        return $this->controllerNamespace;
207
    }
208
209
    /**
210
     * Set default controller namespace.
211
     *
212
     * @param string $namespace
213
     *
214
     * @author Donii Sergii <[email protected]>
215
     */
216 1
    public function setControllerNamespace($namespace)
217
    {
218 1
        $this->controllerNamespace = $namespace;
219 1
    }
220
221
    /**
222
     * Add open event listener.
223
     *
224
     * @param \Closure|string $callback Callback
225
     * @param int             $priority Callback priority
226
     *
227
     * @author Donii Sergii <[email protected]>
228
     */
229 6
    public function onConnectionOpen($callback, $priority = 0)
230
    {
231 6
        $this->addEvent($callback, static::EVENT_CONNECTION_OPEN, $priority);
232 6
    }
233
234
    /**
235
     * Add stop router event listener.
236
     *
237
     * @param \Closure|string $callback Callback
238
     * @param int             $priority Callback priority
239
     *
240
     * @throws \Exception
241
     *
242
     * @author Donii Sergii <[email protected]>
243
     */
244 1
    public function onRouterStop($callback, $priority = 0)
245
    {
246 1
        $this->addEvent($callback, static::EVENT_ROUTER_STOP, $priority);
247 1
    }
248
249
    /**
250
     * Add connection close event listener.
251
     *
252
     * @param \Closure|string $callback Callback
253
     * @param int             $priority Callback priority
254
     *
255
     * @throws \Exception
256
     *
257
     * @author Donii Sergii <[email protected]>
258
     */
259 1
    public function onConnectionClose($callback, $priority = 0)
260
    {
261 1
        $this->addEvent($callback, static::EVENT_CONNECTION_CLOSE, $priority);
262 1
    }
263
264
    /**
265
     * Remove event listener.
266
     *
267
     * @param string $eventName Event name
268
     * @param mixed  $callback  Callback
269
     *
270
     * @author Donii Sergii <[email protected]>
271
     */
272 7
    public function removeEvent($eventName, $callback)
273
    {
274 7
        $this->getEventDispatcher()->removeListener($eventName, $callback);
275
276 7
        if (isset(static::$events[$eventName]) && count(static::$events[$eventName]) === 0) {
277
            unset(static::$events[$eventName]);
278
        }
279 7
    }
280
281
    /**
282
     * Add open event listener.
283
     *
284
     * @param \Closure|string $callback Callback
285
     * @param int             $priority Callback priority
286
     *
287
     * @throws \Exception
288
     *
289
     * @author Donii Sergii <[email protected]>
290
     */
291 1
    public function onRouterStart($callback, $priority = 0)
292
    {
293 1
        $this->addEvent($callback, static::EVENT_ROUTER_START, $priority);
294 1
    }
295
296
    /**
297
     * Get client.
298
     *
299
     * @return \Thruway\Peer\ClientInterface|\sonrac\WAMP\Client
300
     *
301
     * @author Donii Sergii <[email protected]>
302
     */
303 7
    public function getClient()
304
    {
305 7
        return $this->client;
306
    }
307
308
    /**
309
     * Set client
310
     *
311
     * @param \Thruway\Peer\ClientInterface $client Client
312
     *
313
     * @author Donii Sergii <[email protected]>
314
     */
315 7
    public function setClient(ClientInterface $client)
316
    {
317 7
        $this->client = $client;
318 7
    }
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
            [$class, $method] = explode('&', $callback);
333
334
            $callback = function (ConnectionOpenEvent $event) use ($class, $method) {
335
                return $class::{$method}($event);
336
            };
337
        }
338
339 9
        $this->getEventDispatcher()->addListener($eventName, $callback, $priority);
340 9
    }
341
}
342