1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Server; |
4
|
|
|
|
5
|
|
|
use Ratchet\WebSocket\WsServer; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Symfony\Component\Routing\Route; |
8
|
|
|
use Symfony\Component\Routing\RouteCollection; |
9
|
|
|
use Ratchet\WebSocket\MessageComponentInterface; |
10
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler; |
11
|
|
|
use BeyondCode\LaravelWebSockets\Server\Logger\WebsocketsLogger; |
12
|
|
|
use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController; |
13
|
|
|
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsersController; |
14
|
|
|
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelController; |
15
|
|
|
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\TriggerEventController; |
16
|
|
|
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannelsController; |
17
|
|
|
|
18
|
|
|
class Router |
19
|
|
|
{ |
20
|
|
|
/** @var \Symfony\Component\Routing\RouteCollection */ |
21
|
|
|
protected $routes; |
22
|
|
|
protected $customRoutes; |
23
|
|
|
|
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->routes = new RouteCollection; |
27
|
|
|
$this->customRoutes = new Collection(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getRoutes(): RouteCollection |
31
|
|
|
{ |
32
|
|
|
return $this->routes; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function echo() |
36
|
|
|
{ |
37
|
|
|
$this->get('/app/{appKey}', WebSocketHandler::class); |
38
|
|
|
|
39
|
|
|
$this->post('/apps/{appId}/events', TriggerEventController::class); |
40
|
|
|
$this->get('/apps/{appId}/channels', FetchChannelsController::class); |
41
|
|
|
$this->get('/apps/{appId}/channels/{channelName}', FetchChannelController::class); |
42
|
|
|
$this->get('/apps/{appId}/channels/{channelName}/users', FetchUsersController::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function customRoutes() |
46
|
|
|
{ |
47
|
|
|
$this->customRoutes->each(function ($action, $uri) { |
48
|
|
|
$this->get($uri, $action); |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function get(string $uri, $action) |
53
|
|
|
{ |
54
|
|
|
$this->addRoute('GET', $uri, $action); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function post(string $uri, $action) |
58
|
|
|
{ |
59
|
|
|
$this->addRoute('POST', $uri, $action); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function put(string $uri, $action) |
63
|
|
|
{ |
64
|
|
|
$this->addRoute('PUT', $uri, $action); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function patch(string $uri, $action) |
68
|
|
|
{ |
69
|
|
|
$this->addRoute('PATCH', $uri, $action); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function delete(string $uri, $action) |
73
|
|
|
{ |
74
|
|
|
$this->addRoute('DELETE', $uri, $action); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function webSocket(string $uri, $action) |
78
|
|
|
{ |
79
|
|
|
if (! is_subclass_of($action, MessageComponentInterface::class)) { |
80
|
|
|
throw InvalidWebSocketController::withController($action); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->customRoutes->put($uri, $action); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function addRoute(string $method, string $uri, $action) |
87
|
|
|
{ |
88
|
|
|
$this->routes->add($uri, $this->getRoute($method, $uri, $action)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function getRoute(string $method, string $uri, $action): Route |
92
|
|
|
{ |
93
|
|
|
/** |
94
|
|
|
* If the given action is a class that handles WebSockets, then it's not a regular |
95
|
|
|
* controller but a WebSocketHandler that needs to converted to a WsServer. |
96
|
|
|
* |
97
|
|
|
* If the given action is a regular controller we'll just instanciate it. |
98
|
|
|
*/ |
99
|
|
|
$action = is_subclass_of($action, MessageComponentInterface::class) |
100
|
|
|
? $this->createWebSocketsServer($action) |
101
|
|
|
: app($action); |
102
|
|
|
|
103
|
|
|
return new Route($uri, ['_controller' => $action], [], [], null, [], [$method]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function createWebSocketsServer(string $action): WsServer |
107
|
|
|
{ |
108
|
|
|
$app = app($action); |
109
|
|
|
|
110
|
|
|
if (WebsocketsLogger::isEnabled()) { |
111
|
|
|
$app = WebsocketsLogger::decorate($app); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return new WsServer($app); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|