|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Server; |
|
4
|
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Server\Loggers\WebSocketsLogger; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Ratchet\WebSocket\MessageComponentInterface; |
|
8
|
|
|
use Ratchet\WebSocket\WsServer; |
|
9
|
|
|
use Symfony\Component\Routing\Route; |
|
10
|
|
|
use Symfony\Component\Routing\RouteCollection; |
|
11
|
|
|
|
|
12
|
|
|
class Router |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The implemented routes. |
|
16
|
|
|
* |
|
17
|
|
|
* @var \Symfony\Component\Routing\RouteCollection |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $routes; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Define the custom routes. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $customRoutes; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Initialize the class. |
|
30
|
|
|
* |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->routes = new RouteCollection; |
|
36
|
|
|
|
|
37
|
|
|
$this->customRoutes = [ |
|
38
|
|
|
'get' => new Collection, |
|
39
|
|
|
'post' => new Collection, |
|
40
|
|
|
'put' => new Collection, |
|
41
|
|
|
'patch' => new Collection, |
|
42
|
|
|
'delete' => new Collection, |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the routes. |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Symfony\Component\Routing\RouteCollection |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getRoutes(): RouteCollection |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->routes; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the list of routes that still need to be registered. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array[Collection] |
|
|
|
|
|
|
60
|
|
|
*/ |
|
61
|
|
|
public function getCustomRoutes(): array |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->customRoutes; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Register the default routes. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function registerRoutes() |
|
72
|
|
|
{ |
|
73
|
|
|
$this->get('/app/{appKey}', config('websockets.handlers.websocket')); |
|
74
|
|
|
$this->post('/apps/{appId}/events', config('websockets.handlers.trigger_event')); |
|
75
|
|
|
$this->get('/apps/{appId}/channels', config('websockets.handlers.fetch_channels')); |
|
76
|
|
|
$this->get('/apps/{appId}/channels/{channelName}', config('websockets.handlers.fetch_channel')); |
|
77
|
|
|
$this->get('/apps/{appId}/channels/{channelName}/users', config('websockets.handlers.fetch_users')); |
|
78
|
|
|
$this->get('/health', config('websockets.handlers.health')); |
|
79
|
|
|
|
|
80
|
|
|
$this->registerCustomRoutes(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Add a GET route. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $uri |
|
87
|
|
|
* @param string $action |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function get(string $uri, $action) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->addRoute('GET', $uri, $action); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Add a POST route. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $uri |
|
99
|
|
|
* @param string $action |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
public function post(string $uri, $action) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->addRoute('POST', $uri, $action); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Add a PUT route. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $uri |
|
111
|
|
|
* @param string $action |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
public function put(string $uri, $action) |
|
115
|
|
|
{ |
|
116
|
|
|
$this->addRoute('PUT', $uri, $action); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add a PATCH route. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $uri |
|
123
|
|
|
* @param string $action |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
public function patch(string $uri, $action) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->addRoute('PATCH', $uri, $action); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Add a DELETE route. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $uri |
|
135
|
|
|
* @param string $action |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
|
|
public function delete(string $uri, $action) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->addRoute('DELETE', $uri, $action); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Add a new route to the list. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $method |
|
147
|
|
|
* @param string $uri |
|
148
|
|
|
* @param string $action |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function addRoute(string $method, string $uri, $action) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->routes->add($uri, $this->getRoute($method, $uri, $action)); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Add a new custom route. Registered routes |
|
158
|
|
|
* will be resolved at server spin-up. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $method |
|
161
|
|
|
* @param string $uri |
|
162
|
|
|
* @param string $action |
|
163
|
|
|
* @return void |
|
164
|
|
|
*/ |
|
165
|
|
|
public function addCustomRoute(string $method, $uri, $action) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->customRoutes[strtolower($method)]->put($uri, $action); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Register the custom routes into the main RouteCollection. |
|
172
|
|
|
* |
|
173
|
|
|
* @return void |
|
174
|
|
|
*/ |
|
175
|
|
|
public function registerCustomRoutes() |
|
176
|
|
|
{ |
|
177
|
|
|
foreach ($this->customRoutes as $method => $actions) { |
|
178
|
|
|
$actions->each(function ($action, $uri) use ($method) { |
|
179
|
|
|
$this->{$method}($uri, $action); |
|
180
|
|
|
}); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get the route of a specified method, uri and action. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $method |
|
188
|
|
|
* @param string $uri |
|
189
|
|
|
* @param string $action |
|
190
|
|
|
* @return \Symfony\Component\Routing\Route |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function getRoute(string $method, string $uri, $action): Route |
|
193
|
|
|
{ |
|
194
|
|
|
$action = is_subclass_of($action, MessageComponentInterface::class) |
|
195
|
|
|
? $this->createWebSocketsServer($action) |
|
196
|
|
|
: app($action); |
|
197
|
|
|
|
|
198
|
|
|
return new Route($uri, ['_controller' => $action], [], [], null, [], [$method]); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Create a new websockets server to handle the action. |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $action |
|
205
|
|
|
* @return \Ratchet\WebSocket\WsServer |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function createWebSocketsServer(string $action): WsServer |
|
208
|
|
|
{ |
|
209
|
|
|
$app = app($action); |
|
210
|
|
|
|
|
211
|
|
|
if (WebsocketsLogger::isEnabled()) { |
|
212
|
|
|
$app = WebsocketsLogger::decorate($app); |
|
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return new WsServer($app); |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|