laravel-websockets/src/Router.php

105 lines
2.8 KiB
PHP
Raw Normal View History

2018-11-20 10:32:56 +00:00
<?php
namespace BeyondCode\LaravelWebSockets;
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;
2018-11-21 11:13:40 +00:00
use Ratchet\WebSocket\MessageComponentInterface;
2018-11-20 10:32:56 +00:00
use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController;
class Router
{
/** @var RouteCollection */
protected $routes;
public function __construct()
{
$this->routes = new RouteCollection;
}
2018-11-20 11:33:40 +00:00
/**
* Add a new WebSocket route.
*
* @param $uri
* @param $action
*/
public function websocket($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-20 16:47:15 +00:00
public function get($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-20 16:47:15 +00:00
public function post($uri, $action)
2018-11-20 10:32:56 +00:00
{
2018-11-20 16:47:15 +00:00
$this->addRoute('POST', $uri, $action);
}
public function put($uri, $action)
{
$this->addRoute('PUT', $uri, $action);
}
public function patch($uri, $action)
{
$this->addRoute('PATCH', $uri, $action);
}
public function delete($uri, $action)
{
$this->addRoute('DELETE', $uri, $action);
}
public function addRoute($method, $uri, $action)
{
$this->routes->add($uri, $this->getRoute($method, $uri, $action));
}
protected function getRoute($method, $uri, $action): Route
{
return new Route($uri, ['_controller' => $this->wrapAction($action)], [], [], null, [], [$method]);
2018-11-20 13:50:37 +00:00
}
/**
* Register the required Laravel Echo routes
*/
public function echo()
{
2018-11-21 11:13:40 +00:00
$this->get('/app/{appId}', LaravelEcho\WebSocket\EchoServer::class);
2018-11-20 16:47:15 +00:00
$this->get('/apps/{appId}/status', LaravelEcho\Http\Controllers\StatusController::class);
$this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\StatusController::class);
$this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\StatusController::class);
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\StatusController::class);
2018-11-21 11:13:40 +00:00
$this->post('/apps/{appId}/events', LaravelEcho\Http\Controllers\EventController::class);
2018-11-20 10:51:00 +00:00
}
/**
* Wrap WebSocket controllers with Ratchets WsServer.
2018-11-20 13:50:37 +00:00
* If the action is not a WebSocketController, wrap it with our HttpServerInstance
*
* @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)) {
return new WsServer(app($action));
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
}
}