Allow for the addition of new API endpoints

This commit is contained in:
Toby Zerner 2015-07-31 20:11:44 +09:30
parent 1d5586165c
commit 8a83d01bba

View File

@ -0,0 +1,49 @@
<?php namespace Flarum\Events;
use Flarum\Api\Request;
use Flarum\Http\RouteCollection;
use Psr\Http\Message\ServerRequestInterface;
class RegisterApiRoutes
{
/**
* @var RouteCollection
*/
public $routes;
/**
* @param RouteCollection $routes
*/
public function __construct(RouteCollection $routes)
{
$this->routes = $routes;
}
public function get($url, $name, $action)
{
$this->route('get', $url, $name, $action);
}
public function patch($url, $name, $action)
{
$this->route('patch', $url, $name, $action);
}
protected function route($method, $url, $name, $action)
{
$this->routes->$method($url, $name, $this->action($action));
}
protected function action($class)
{
return function (ServerRequestInterface $httpRequest, $routeParams) use ($class) {
$action = app($class);
$actor = app('flarum.actor');
$input = array_merge($httpRequest->getQueryParams(), $httpRequest->getAttributes(), $routeParams);
$request = new Request($input, $actor, $httpRequest);
return $action->handle($request);
};
}
}