From 137f55317beab3f95935f9ed3c94edbd361a9a17 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 18 Jun 2015 17:43:41 +0930 Subject: [PATCH] Add API to add routes --- src/Extend/ForumClient.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Extend/ForumClient.php b/src/Extend/ForumClient.php index fba5642ea..b0b1000ba 100644 --- a/src/Extend/ForumClient.php +++ b/src/Extend/ForumClient.php @@ -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); + }); + } + } } }