diff --git a/framework/core/src/Admin/Server.php b/framework/core/src/Admin/Server.php index f81c03727..dc5ac9504 100644 --- a/framework/core/src/Admin/Server.php +++ b/framework/core/src/Admin/Server.php @@ -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)); } } diff --git a/framework/core/src/Api/Server.php b/framework/core/src/Api/Server.php index d74f8a493..59c3090f0 100644 --- a/framework/core/src/Api/Server.php +++ b/framework/core/src/Api/Server.php @@ -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([ [ diff --git a/framework/core/src/Event/ConfigureMiddleware.php b/framework/core/src/Event/ConfigureMiddleware.php new file mode 100644 index 000000000..26f06248b --- /dev/null +++ b/framework/core/src/Event/ConfigureMiddleware.php @@ -0,0 +1,67 @@ + + * + * 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; + } +} diff --git a/framework/core/src/Forum/Server.php b/framework/core/src/Forum/Server.php index ed6faec98..f3285fee8 100644 --- a/framework/core/src/Forum/Server.php +++ b/framework/core/src/Forum/Server.php @@ -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)); }); }