From 8a83d01bba65a6687f4ac1fadef438a8356c174d Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 31 Jul 2015 20:11:44 +0930 Subject: [PATCH] Allow for the addition of new API endpoints --- .../core/src/Events/RegisterApiRoutes.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 framework/core/src/Events/RegisterApiRoutes.php diff --git a/framework/core/src/Events/RegisterApiRoutes.php b/framework/core/src/Events/RegisterApiRoutes.php new file mode 100644 index 000000000..943a9437a --- /dev/null +++ b/framework/core/src/Events/RegisterApiRoutes.php @@ -0,0 +1,49 @@ +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); + }; + } +}