mirror of
https://github.com/flarum/core.git
synced 2025-10-14 08:24:28 +02:00
Get rid of Server classes for Admin, API and Forum
The various middleware can be registered in the service provider, and the rest of the logic can all go through one single front controller (index.php in flarum/flarum, and Flarum\Http\Server in flarum/core). This will also simplify the necessary server setup, as only one rewrite rule remains.
This commit is contained in:
@@ -12,17 +12,28 @@
|
||||
namespace Flarum\Api;
|
||||
|
||||
use Flarum\Api\Controller\AbstractSerializeController;
|
||||
use Flarum\Api\Middleware\FakeHttpMethods;
|
||||
use Flarum\Api\Middleware\HandleErrors;
|
||||
use Flarum\Api\Serializer\AbstractSerializer;
|
||||
use Flarum\Api\Serializer\NotificationSerializer;
|
||||
use Flarum\Event\ConfigureApiRoutes;
|
||||
use Flarum\Event\ConfigureMiddleware;
|
||||
use Flarum\Event\ConfigureNotificationTypes;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Http\Middleware\AuthenticateWithHeader;
|
||||
use Flarum\Http\Middleware\AuthenticateWithSession;
|
||||
use Flarum\Http\Middleware\DispatchRoute;
|
||||
use Flarum\Http\Middleware\ParseJsonBody;
|
||||
use Flarum\Http\Middleware\RememberFromCookie;
|
||||
use Flarum\Http\Middleware\SetLocale;
|
||||
use Flarum\Http\Middleware\StartSession;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Flarum\Http\RouteHandlerFactory;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Tobscure\JsonApi\ErrorHandler;
|
||||
use Tobscure\JsonApi\Exception\Handler\FallbackExceptionHandler;
|
||||
use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler;
|
||||
use Zend\Stratigility\MiddlewarePipe;
|
||||
|
||||
class ApiServiceProvider extends AbstractServiceProvider
|
||||
{
|
||||
@@ -39,6 +50,27 @@ class ApiServiceProvider extends AbstractServiceProvider
|
||||
return new RouteCollection;
|
||||
});
|
||||
|
||||
$this->app->singleton('flarum.api.middleware', function ($app) {
|
||||
$pipe = new MiddlewarePipe;
|
||||
$pipe->raiseThrowables();
|
||||
|
||||
$pipe->pipe($app->make(HandleErrors::class));
|
||||
|
||||
$pipe->pipe($app->make(ParseJsonBody::class));
|
||||
$pipe->pipe($app->make(FakeHttpMethods::class));
|
||||
$pipe->pipe($app->make(StartSession::class));
|
||||
$pipe->pipe($app->make(RememberFromCookie::class));
|
||||
$pipe->pipe($app->make(AuthenticateWithSession::class));
|
||||
$pipe->pipe($app->make(AuthenticateWithHeader::class));
|
||||
$pipe->pipe($app->make(SetLocale::class));
|
||||
|
||||
event(new ConfigureMiddleware($pipe, 'api'));
|
||||
|
||||
$pipe->pipe($app->make(DispatchRoute::class, ['routes' => $app->make('flarum.api.routes')]));
|
||||
|
||||
return $pipe;
|
||||
});
|
||||
|
||||
$this->app->singleton(ErrorHandler::class, function () {
|
||||
$handler = new ErrorHandler;
|
||||
|
||||
|
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Api;
|
||||
|
||||
use Flarum\Event\ConfigureMiddleware;
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Http\AbstractServer;
|
||||
use Tobscure\JsonApi\Document;
|
||||
use Zend\Stratigility\MiddlewarePipe;
|
||||
|
||||
class Server extends AbstractServer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getMiddleware(Application $app)
|
||||
{
|
||||
$pipe = new MiddlewarePipe;
|
||||
$pipe->raiseThrowables();
|
||||
|
||||
$path = parse_url($app->url('api'), PHP_URL_PATH);
|
||||
|
||||
if ($app->isInstalled() && $app->isUpToDate()) {
|
||||
$pipe->pipe($path, $app->make('Flarum\Api\Middleware\HandleErrors'));
|
||||
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
|
||||
$pipe->pipe($path, $app->make('Flarum\Api\Middleware\FakeHttpMethods'));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\RememberFromCookie'));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\AuthenticateWithSession'));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\AuthenticateWithHeader'));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\SetLocale'));
|
||||
|
||||
event(new ConfigureMiddleware($pipe, $path, $this));
|
||||
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.api.routes')]));
|
||||
} else {
|
||||
$pipe->pipe($path, function () {
|
||||
$document = new Document;
|
||||
$document->setErrors([
|
||||
[
|
||||
'code' => 503,
|
||||
'title' => 'Service Unavailable'
|
||||
]
|
||||
]);
|
||||
|
||||
return new JsonApiResponse($document, 503);
|
||||
});
|
||||
}
|
||||
|
||||
return $pipe;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user