This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
/
Router.php
217 lines (194 loc) · 5.19 KB
/
Router.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace BeyondCode\LaravelWebSockets\Server;
use BeyondCode\LaravelWebSockets\Server\Loggers\WebSocketsLogger;
use Illuminate\Support\Collection;
use Ratchet\WebSocket\MessageComponentInterface;
use Ratchet\WebSocket\WsServer;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class Router
{
/**
* The implemented routes.
*
* @var \Symfony\Component\Routing\RouteCollection
*/
protected $routes;
/**
* Define the custom routes.
*
* @var array
*/
protected $customRoutes;
/**
* Initialize the class.
*
* @return void
*/
public function __construct()
{
$this->routes = new RouteCollection;
$this->customRoutes = [
'get' => new Collection,
'post' => new Collection,
'put' => new Collection,
'patch' => new Collection,
'delete' => new Collection,
];
}
/**
* Get the routes.
*
* @return \Symfony\Component\Routing\RouteCollection
*/
public function getRoutes(): RouteCollection
{
return $this->routes;
}
/**
* Get the list of routes that still need to be registered.
*
* @return array[Collection]
*/
public function getCustomRoutes(): array
{
return $this->customRoutes;
}
/**
* Register the default routes.
*
* @return void
*/
public function registerRoutes()
{
$this->get('/app/{appKey}', config('websockets.handlers.websocket'));
$this->post('/apps/{appId}/events', config('websockets.handlers.trigger_event'));
$this->get('/apps/{appId}/channels', config('websockets.handlers.fetch_channels'));
$this->get('/apps/{appId}/channels/{channelName}', config('websockets.handlers.fetch_channel'));
$this->get('/apps/{appId}/channels/{channelName}/users', config('websockets.handlers.fetch_users'));
$this->get('/health', config('websockets.handlers.health'));
$this->registerCustomRoutes();
}
/**
* Add a GET route.
*
* @param string $uri
* @param string $action
* @return void
*/
public function get(string $uri, $action)
{
$this->addRoute('GET', $uri, $action);
}
/**
* Add a POST route.
*
* @param string $uri
* @param string $action
* @return void
*/
public function post(string $uri, $action)
{
$this->addRoute('POST', $uri, $action);
}
/**
* Add a PUT route.
*
* @param string $uri
* @param string $action
* @return void
*/
public function put(string $uri, $action)
{
$this->addRoute('PUT', $uri, $action);
}
/**
* Add a PATCH route.
*
* @param string $uri
* @param string $action
* @return void
*/
public function patch(string $uri, $action)
{
$this->addRoute('PATCH', $uri, $action);
}
/**
* Add a DELETE route.
*
* @param string $uri
* @param string $action
* @return void
*/
public function delete(string $uri, $action)
{
$this->addRoute('DELETE', $uri, $action);
}
/**
* Add a new route to the list.
*
* @param string $method
* @param string $uri
* @param string $action
* @return void
*/
public function addRoute(string $method, string $uri, $action)
{
$this->routes->add($uri, $this->getRoute($method, $uri, $action));
}
/**
* Add a new custom route. Registered routes
* will be resolved at server spin-up.
*
* @param string $method
* @param string $uri
* @param string $action
* @return void
*/
public function addCustomRoute(string $method, $uri, $action)
{
$this->customRoutes[strtolower($method)]->put($uri, $action);
}
/**
* Register the custom routes into the main RouteCollection.
*
* @return void
*/
public function registerCustomRoutes()
{
foreach ($this->customRoutes as $method => $actions) {
$actions->each(function ($action, $uri) use ($method) {
$this->{$method}($uri, $action);
});
}
}
/**
* Get the route of a specified method, uri and action.
*
* @param string $method
* @param string $uri
* @param string $action
* @return \Symfony\Component\Routing\Route
*/
protected function getRoute(string $method, string $uri, $action): Route
{
$action = is_subclass_of($action, MessageComponentInterface::class)
? $this->createWebSocketsServer($action)
: app($action);
return new Route($uri, ['_controller' => $action], [], [], null, [], [$method]);
}
/**
* Create a new websockets server to handle the action.
*
* @param string $action
* @return \Ratchet\WebSocket\WsServer
*/
protected function createWebSocketsServer(string $action): WsServer
{
$app = app($action);
if (WebsocketsLogger::isEnabled()) {
$app = WebsocketsLogger::decorate($app);
}
return new WsServer($app);
}
}