laravel-websockets/src/Server/Router.php

112 lines
3.1 KiB
PHP
Raw Normal View History

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-24 14:23:59 +00:00
use BeyondCode\LaravelWebSockets\Server\Logger\MessageLogger;
2018-11-27 15:42:19 +00:00
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannel;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchChannels;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\FetchUsers;
use BeyondCode\LaravelWebSockets\HttpApi\Controllers\TriggerEvent;
2018-11-27 20:06:04 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Controllers\WebSocketHandler;
use Ratchet\WebSocket\MessageComponentInterface;
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
{
2018-11-27 19:14:43 +00:00
/** @var \Symfony\Component\Routing\RouteCollection */
2018-11-20 10:32:56 +00:00
protected $routes;
public function __construct()
{
$this->routes = new RouteCollection;
}
2018-11-27 20:14:08 +00:00
public function getRoutes(): RouteCollection
{
return $this->routes;
}
2018-11-21 23:25:24 +00:00
public function websocket(string $uri, $action)
2018-11-20 10:32:56 +00:00
{
2018-11-27 20:07:21 +00:00
if (!is_subclass_of($action, MessageComponentInterface::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 20:06:04 +00:00
$this->get('/app/{appKey}', WebSocketHandler::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-27 20:14:08 +00:00
* @param string $action
2018-11-27 19:14:43 +00:00
*
2018-11-27 19:20:17 +00:00
* @return \Ratchet\WebSocket\WsServer|\Ratchet\Http\HttpServerInterface
2018-11-20 10:51:00 +00:00
*/
2018-11-27 20:14:08 +00:00
protected function wrapAction(string $action)
2018-11-20 10:51:00 +00:00
{
2018-11-27 20:06:04 +00:00
if (is_subclass_of($action, MessageComponentInterface::class)) {
2018-11-27 20:14:08 +00:00
return $this->createWebSocketsServer($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
}
2018-11-27 20:14:08 +00:00
protected function createWebSocketsServer(MessageComponentInterface $action): WsServer
2018-11-20 10:51:00 +00:00
{
2018-11-27 20:14:08 +00:00
$app = app($action);
if (MessageLogger::isEnabled()) {
$app = MessageLogger::decorate($app);
}
return new WsServer($app);
2018-11-20 10:32:56 +00:00
}
}