mirror of
https://github.com/flarum/framework.git
synced 2024-11-29 04:33:47 +08:00
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:
parent
4a576ba1f4
commit
1c6ac80d01
35
src/Extend/Api.php
Normal file
35
src/Extend/Api.php
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user