2018-11-20 10:32:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-11-27 14:55:30 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Server;
|
2018-11-20 10:32:56 +00:00
|
|
|
|
2018-11-27 14:55:30 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\LaravelEcho;
|
2018-11-24 14:23:59 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Server\Logger\MessageLogger;
|
2018-11-27 15:35:28 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Controllers\FetchChannel;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Controllers\FetchChannels;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Controllers\FetchUsers;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Controllers\TriggerEvent;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Pusher\PusherController;
|
2018-11-20 10:51:00 +00:00
|
|
|
use Ratchet\WebSocket\WsServer;
|
2018-11-20 10:32:56 +00:00
|
|
|
use Symfony\Component\Routing\Route;
|
2018-11-20 13:50:37 +00:00
|
|
|
use Ratchet\Http\HttpServerInterface;
|
2018-11-20 10:32:56 +00:00
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
|
|
|
use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController;
|
|
|
|
|
|
|
|
|
|
class Router
|
|
|
|
|
{
|
|
|
|
|
/** @var RouteCollection */
|
|
|
|
|
protected $routes;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->routes = new RouteCollection;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:25:24 +00:00
|
|
|
public function websocket(string $uri, $action)
|
2018-11-20 10:32:56 +00:00
|
|
|
{
|
2018-11-20 10:51:00 +00:00
|
|
|
if (!is_subclass_of($action, WebSocketController::class)) {
|
2018-11-20 10:32:56 +00:00
|
|
|
throw InvalidWebSocketController::withController($action);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 16:47:15 +00:00
|
|
|
$this->get($uri, $action);
|
2018-11-20 11:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:25:24 +00:00
|
|
|
public function get(string $uri, $action)
|
2018-11-20 11:33:40 +00:00
|
|
|
{
|
2018-11-20 16:47:15 +00:00
|
|
|
$this->addRoute('GET', $uri, $action);
|
2018-11-20 10:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:25:24 +00:00
|
|
|
public function post(string $uri, $action)
|
2018-11-20 10:32:56 +00:00
|
|
|
{
|
2018-11-20 16:47:15 +00:00
|
|
|
$this->addRoute('POST', $uri, $action);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:25:24 +00:00
|
|
|
public function put(string $uri, $action)
|
2018-11-20 16:47:15 +00:00
|
|
|
{
|
|
|
|
|
$this->addRoute('PUT', $uri, $action);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:25:24 +00:00
|
|
|
public function patch(string $uri, $action)
|
2018-11-20 16:47:15 +00:00
|
|
|
{
|
|
|
|
|
$this->addRoute('PATCH', $uri, $action);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:25:24 +00:00
|
|
|
public function delete(string $uri, $action)
|
2018-11-20 16:47:15 +00:00
|
|
|
{
|
|
|
|
|
$this->addRoute('DELETE', $uri, $action);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:25:24 +00:00
|
|
|
public function addRoute(string $method, string $uri, $action)
|
2018-11-20 16:47:15 +00:00
|
|
|
{
|
|
|
|
|
$this->routes->add($uri, $this->getRoute($method, $uri, $action));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 23:25:24 +00:00
|
|
|
protected function getRoute(string $method, string $uri, $action): Route
|
2018-11-20 16:47:15 +00:00
|
|
|
{
|
|
|
|
|
return new Route($uri, ['_controller' => $this->wrapAction($action)], [], [], null, [], [$method]);
|
2018-11-20 13:50:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function echo()
|
|
|
|
|
{
|
2018-11-27 14:58:52 +00:00
|
|
|
$this->get('/app/{appKey}', PusherController::class);
|
2018-11-21 20:23:25 +00:00
|
|
|
|
2018-11-27 14:58:52 +00:00
|
|
|
$this->get('/apps/{appId}/channels', FetchChannels::class);
|
|
|
|
|
$this->get('/apps/{appId}/channels/{channelName}', FetchChannel::class);
|
|
|
|
|
$this->get('/apps/{appId}/channels/{channelName}/users', FetchUsers::class);
|
2018-11-21 20:23:25 +00:00
|
|
|
|
2018-11-27 14:59:51 +00:00
|
|
|
$this->post('/apps/{appId}/events', TriggerEvent::class);
|
2018-11-20 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-11-20 13:50:37 +00:00
|
|
|
* @param $action
|
|
|
|
|
* @return WsServer|HttpServerInterface
|
2018-11-20 10:51:00 +00:00
|
|
|
*/
|
2018-11-20 13:50:37 +00:00
|
|
|
protected function wrapAction($action)
|
2018-11-20 10:51:00 +00:00
|
|
|
{
|
2018-11-20 13:50:37 +00:00
|
|
|
if (is_subclass_of($action, WebSocketController::class)) {
|
2018-11-23 21:46:09 +00:00
|
|
|
$app = app($action);
|
|
|
|
|
|
2018-11-24 14:23:59 +00:00
|
|
|
if (MessageLogger::isEnabled()) {
|
|
|
|
|
$app = MessageLogger::decorate($app);
|
2018-11-24 00:25:40 +00:00
|
|
|
}
|
2018-11-23 21:46:09 +00:00
|
|
|
|
2018-11-24 00:25:40 +00:00
|
|
|
return new WsServer($app);
|
2018-11-20 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-20 13:50:37 +00:00
|
|
|
return app($action);
|
2018-11-20 10:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getRoutes(): RouteCollection
|
|
|
|
|
{
|
|
|
|
|
return $this->routes;
|
2018-11-20 10:32:56 +00:00
|
|
|
}
|
|
|
|
|
}
|