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-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-21 23:25:24 +00:00
|
|
|
//TODO: add origin checker middleware
|
2018-11-22 16:59:56 +00:00
|
|
|
$this->get('/app/{appKey}', LaravelEcho\WebSocket\PusherServer::class);
|
2018-11-21 20:23:25 +00:00
|
|
|
|
|
|
|
|
// TODO: fleshen out http API
|
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);
|
2018-11-22 20:36:25 +00:00
|
|
|
$this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class);
|
2018-11-20 16:47:15 +00:00
|
|
|
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\StatusController::class);
|
2018-11-21 20:23:25 +00:00
|
|
|
|
2018-11-22 20:36:25 +00:00
|
|
|
$this->post('/apps/{appId}/events', LaravelEcho\Http\Controllers\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)) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|