mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 14:13:37 +08:00
API: Add an event to configure server middleware
This commit is contained in:
parent
5091dd038f
commit
d53a525383
|
@ -11,9 +11,9 @@
|
|||
|
||||
namespace Flarum\Admin;
|
||||
|
||||
use Flarum\Event\ConfigureMiddleware;
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Http\AbstractServer;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
use Zend\Stratigility\MiddlewarePipe;
|
||||
use Flarum\Http\Middleware\HandleErrors;
|
||||
|
||||
|
@ -27,23 +27,26 @@ class Server extends AbstractServer
|
|||
$pipe = new MiddlewarePipe;
|
||||
|
||||
if ($app->isInstalled()) {
|
||||
$adminPath = parse_url($app->url('admin'), PHP_URL_PATH);
|
||||
$path = parse_url($app->url('admin'), PHP_URL_PATH);
|
||||
$errorDir = __DIR__ . '/../../error';
|
||||
|
||||
if ($app->isUpToDate()) {
|
||||
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
|
||||
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\StartSession'));
|
||||
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\RememberFromCookie'));
|
||||
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\AuthenticateWithSession'));
|
||||
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\SetLocale'));
|
||||
$pipe->pipe($adminPath, $app->make('Flarum\Admin\Middleware\RequireAdministrateAbility'));
|
||||
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.admin.routes')]));
|
||||
$pipe->pipe($adminPath, new HandleErrors($errorDir, $app->inDebugMode()));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
|
||||
$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\SetLocale'));
|
||||
$pipe->pipe($path, $app->make('Flarum\Admin\Middleware\RequireAdministrateAbility'));
|
||||
|
||||
event(new ConfigureMiddleware($pipe, $path, $this));
|
||||
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.admin.routes')]));
|
||||
$pipe->pipe($path, new HandleErrors($errorDir, $app->inDebugMode()));
|
||||
} else {
|
||||
$app->register('Flarum\Update\UpdateServiceProvider');
|
||||
|
||||
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.update.routes')]));
|
||||
$pipe->pipe($adminPath, new HandleErrors($errorDir, true));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.update.routes')]));
|
||||
$pipe->pipe($path, new HandleErrors($errorDir, true));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Flarum\Api;
|
||||
|
||||
use Flarum\Event\ConfigureMiddleware;
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Http\AbstractServer;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
@ -25,20 +26,23 @@ class Server extends AbstractServer
|
|||
{
|
||||
$pipe = new MiddlewarePipe;
|
||||
|
||||
$apiPath = parse_url($app->url('api'), PHP_URL_PATH);
|
||||
$path = parse_url($app->url('api'), PHP_URL_PATH);
|
||||
|
||||
if ($app->isInstalled() && $app->isUpToDate()) {
|
||||
$pipe->pipe($apiPath, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
|
||||
$pipe->pipe($apiPath, $app->make('Flarum\Api\Middleware\FakeHttpMethods'));
|
||||
$pipe->pipe($apiPath, $app->make('Flarum\Http\Middleware\StartSession'));
|
||||
$pipe->pipe($apiPath, $app->make('Flarum\Http\Middleware\RememberFromCookie'));
|
||||
$pipe->pipe($apiPath, $app->make('Flarum\Http\Middleware\AuthenticateWithSession'));
|
||||
$pipe->pipe($apiPath, $app->make('Flarum\Http\Middleware\AuthenticateWithHeader'));
|
||||
$pipe->pipe($apiPath, $app->make('Flarum\Http\Middleware\SetLocale'));
|
||||
$pipe->pipe($apiPath, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.api.routes')]));
|
||||
$pipe->pipe($apiPath, $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')]));
|
||||
$pipe->pipe($path, $app->make('Flarum\Api\Middleware\HandleErrors'));
|
||||
} else {
|
||||
$pipe->pipe($apiPath, function () {
|
||||
$pipe->pipe($path, function () {
|
||||
$document = new Document;
|
||||
$document->setErrors([
|
||||
[
|
||||
|
|
67
framework/core/src/Event/ConfigureMiddleware.php
Normal file
67
framework/core/src/Event/ConfigureMiddleware.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?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\Event;
|
||||
|
||||
use Flarum\Forum\Server as ForumServer;
|
||||
use Flarum\Api\Server as ApiServer;
|
||||
use Flarum\Admin\Server as AdminServer;
|
||||
use Flarum\Foundation\AbstractServer;
|
||||
use Zend\Stratigility\MiddlewarePipe;
|
||||
|
||||
class ConfigureMiddleware
|
||||
{
|
||||
/**
|
||||
* @var MiddlewarePipe
|
||||
*/
|
||||
public $pipe;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $path;
|
||||
|
||||
/**
|
||||
* @var AbstractServer
|
||||
*/
|
||||
public $server;
|
||||
|
||||
/**
|
||||
* @param MiddlewarePipe $pipe
|
||||
* @param string $path
|
||||
* @param AbstractServer $server
|
||||
*/
|
||||
public function __construct(MiddlewarePipe $pipe, $path, AbstractServer $server)
|
||||
{
|
||||
$this->pipe = $pipe;
|
||||
$this->path = $path;
|
||||
$this->server = $server;
|
||||
}
|
||||
|
||||
public function pipe(callable $middleware)
|
||||
{
|
||||
$this->pipe->pipe($this->path, $middleware);
|
||||
}
|
||||
|
||||
public function isForum()
|
||||
{
|
||||
return $this->server instanceof ForumServer;
|
||||
}
|
||||
|
||||
public function isAdmin()
|
||||
{
|
||||
return $this->server instanceof AdminServer;
|
||||
}
|
||||
|
||||
public function isApi()
|
||||
{
|
||||
return $this->server instanceof ApiServer;
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Flarum\Forum;
|
||||
|
||||
use Flarum\Event\ConfigureMiddleware;
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Http\AbstractServer;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
|
@ -26,25 +27,28 @@ class Server extends AbstractServer
|
|||
{
|
||||
$pipe = new MiddlewarePipe;
|
||||
|
||||
$basePath = parse_url($app->url(), PHP_URL_PATH);
|
||||
$path = parse_url($app->url(), PHP_URL_PATH);
|
||||
$errorDir = __DIR__.'/../../error';
|
||||
|
||||
if (! $app->isInstalled()) {
|
||||
$app->register('Flarum\Install\InstallServiceProvider');
|
||||
|
||||
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\StartSession'));
|
||||
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.install.routes')]));
|
||||
$pipe->pipe($basePath, new HandleErrors($errorDir, true));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.install.routes')]));
|
||||
$pipe->pipe($path, new HandleErrors($errorDir, true));
|
||||
} elseif ($app->isUpToDate() && ! $app->isDownForMaintenance()) {
|
||||
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
|
||||
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\StartSession'));
|
||||
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\RememberFromCookie'));
|
||||
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\AuthenticateWithSession'));
|
||||
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\SetLocale'));
|
||||
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.forum.routes')]));
|
||||
$pipe->pipe($basePath, new HandleErrors($errorDir, $app->inDebugMode()));
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
|
||||
$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\SetLocale'));
|
||||
|
||||
event(new ConfigureMiddleware($pipe, $path, $this));
|
||||
|
||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.forum.routes')]));
|
||||
$pipe->pipe($path, new HandleErrors($errorDir, $app->inDebugMode()));
|
||||
} else {
|
||||
$pipe->pipe($basePath, function () use ($errorDir) {
|
||||
$pipe->pipe($path, function () use ($errorDir) {
|
||||
return new HtmlResponse(file_get_contents($errorDir.'/503.html', 503));
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user