Yo dawg I heard you like APIs so I put an API in your API so you can API while you API

This commit is contained in:
Toby Zerner 2015-06-23 10:38:17 +09:30
parent 4a576ba1f4
commit 1c6ac80d01

35
src/Extend/Api.php Normal file
View File

@ -0,0 +1,35 @@
<?php namespace Flarum\Extend;
use Illuminate\Contracts\Container\Container;
class Api implements ExtenderInterface
{
protected $routes = [];
public function route($method, $url, $name, $action)
{
$this->routes[] = compact('method', 'url', 'name', 'action');
return $this;
}
public function extend(Container $container)
{
if ($container->make('type') !== 'api') {
return;
}
if (count($this->routes)) {
$routes = $container->make('flarum.api.routes');
foreach ($this->routes as $route) {
$method = $route['method'];
$routes->$method($route['url'], $route['name'], function (ServerRequestInterface $httpRequest, $routeParams) use ($container, $route) {
$action = $container->make($route['action']);
return $action->handle($httpRequest, $routeParams);
});
}
}
}
}