mirror of
https://github.com/flarum/core.git
synced 2025-07-22 09:11:19 +02:00
Allow for the addition of new API endpoints
This commit is contained in:
49
framework/core/src/Events/RegisterApiRoutes.php
Normal file
49
framework/core/src/Events/RegisterApiRoutes.php
Normal 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);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user