Add API to add routes

This commit is contained in:
Toby Zerner 2015-06-18 17:43:41 +09:30
parent 555bb18acd
commit 137f55317b

View File

@ -2,6 +2,7 @@
use Illuminate\Contracts\Container\Container;
use Flarum\Forum\Actions\IndexAction;
use Psr\Http\Message\ServerRequestInterface;
class ForumClient implements ExtenderInterface
{
@ -9,6 +10,8 @@ class ForumClient implements ExtenderInterface
protected $translations = [];
protected $routes = [];
public function assets($assets)
{
$this->assets = array_merge($this->assets, $assets);
@ -23,12 +26,36 @@ class ForumClient implements ExtenderInterface
return $this;
}
public function route($method, $url, $name, $action = 'Flarum\Forum\Actions\IndexAction')
{
$this->routes[] = compact('method', 'url', 'name', 'action');
return $this;
}
public function extend(Container $container)
{
if ($container->make('type') !== 'forum') {
return;
}
$container->make('events')->listen('Flarum\Forum\Events\RenderView', function ($event) {
$event->assets->addFiles($this->assets);
});
IndexAction::$translations = array_merge(IndexAction::$translations, $this->translations);
if (count($this->routes)) {
$routes = $container->make('flarum.forum.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);
});
}
}
}
}