From 82c110c7b6fa793467b1c99d043a1a0f907c8676 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 24 May 2020 08:47:10 +0200 Subject: [PATCH] Allow manipulating error handler through extender By giving each middleware a name, they can now be replaced or moved around using the Middleware extender. Fixes #2115. --- framework/core/src/Admin/AdminServiceProvider.php | 14 ++++++++------ framework/core/src/Api/ApiServiceProvider.php | 13 ++++++++----- framework/core/src/Forum/ForumServiceProvider.php | 14 ++++++++------ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/framework/core/src/Admin/AdminServiceProvider.php b/framework/core/src/Admin/AdminServiceProvider.php index 291144cee..103bd35df 100644 --- a/framework/core/src/Admin/AdminServiceProvider.php +++ b/framework/core/src/Admin/AdminServiceProvider.php @@ -49,6 +49,7 @@ class AdminServiceProvider extends AbstractServiceProvider $this->app->singleton('flarum.admin.middleware', function () { return [ + 'flarum.admin.error_handler', HttpMiddleware\ParseJsonBody::class, HttpMiddleware\StartSession::class, HttpMiddleware\RememberFromCookie::class, @@ -59,15 +60,16 @@ class AdminServiceProvider extends AbstractServiceProvider ]; }); - $this->app->singleton('flarum.admin.handler', function () { - $pipe = new MiddlewarePipe; - - // All requests should first be piped through our global error handler - $pipe->pipe(new HttpMiddleware\HandleErrors( + $this->app->bind('flarum.admin.error_handler', function () { + return new HttpMiddleware\HandleErrors( $this->app->make(Registry::class), $this->app['flarum']->inDebugMode() ? $this->app->make(WhoopsFormatter::class) : $this->app->make(ViewFormatter::class), $this->app->tagged(Reporter::class) - )); + ); + }); + + $this->app->singleton('flarum.admin.handler', function () { + $pipe = new MiddlewarePipe; foreach ($this->app->make('flarum.admin.middleware') as $middleware) { $pipe->pipe($this->app->make($middleware)); diff --git a/framework/core/src/Api/ApiServiceProvider.php b/framework/core/src/Api/ApiServiceProvider.php index 18fb23ab2..4737d2e8c 100644 --- a/framework/core/src/Api/ApiServiceProvider.php +++ b/framework/core/src/Api/ApiServiceProvider.php @@ -44,6 +44,7 @@ class ApiServiceProvider extends AbstractServiceProvider $this->app->singleton('flarum.api.middleware', function () { return [ + 'flarum.api.error_handler', HttpMiddleware\ParseJsonBody::class, Middleware\FakeHttpMethods::class, HttpMiddleware\StartSession::class, @@ -55,14 +56,16 @@ class ApiServiceProvider extends AbstractServiceProvider ]; }); - $this->app->singleton('flarum.api.handler', function () { - $pipe = new MiddlewarePipe; - - $pipe->pipe(new HttpMiddleware\HandleErrors( + $this->app->bind('flarum.api.error_handler', function () { + return new HttpMiddleware\HandleErrors( $this->app->make(Registry::class), new JsonApiFormatter($this->app['flarum']->inDebugMode()), $this->app->tagged(Reporter::class) - )); + ); + }); + + $this->app->singleton('flarum.api.handler', function () { + $pipe = new MiddlewarePipe; foreach ($this->app->make('flarum.api.middleware') as $middleware) { $pipe->pipe($this->app->make($middleware)); diff --git a/framework/core/src/Forum/ForumServiceProvider.php b/framework/core/src/Forum/ForumServiceProvider.php index 4640df804..1903783d6 100644 --- a/framework/core/src/Forum/ForumServiceProvider.php +++ b/framework/core/src/Forum/ForumServiceProvider.php @@ -58,6 +58,7 @@ class ForumServiceProvider extends AbstractServiceProvider $this->app->singleton('flarum.forum.middleware', function () { return [ + 'flarum.forum.error_handler', HttpMiddleware\ParseJsonBody::class, HttpMiddleware\CollectGarbage::class, HttpMiddleware\StartSession::class, @@ -69,15 +70,16 @@ class ForumServiceProvider extends AbstractServiceProvider ]; }); - $this->app->singleton('flarum.forum.handler', function () { - $pipe = new MiddlewarePipe; - - // All requests should first be piped through our global error handler - $pipe->pipe(new HttpMiddleware\HandleErrors( + $this->app->bind('flarum.forum.error_handler', function () { + return new HttpMiddleware\HandleErrors( $this->app->make(Registry::class), $this->app['flarum']->inDebugMode() ? $this->app->make(WhoopsFormatter::class) : $this->app->make(ViewFormatter::class), $this->app->tagged(Reporter::class) - )); + ); + }); + + $this->app->singleton('flarum.forum.handler', function () { + $pipe = new MiddlewarePipe; foreach ($this->app->make('flarum.forum.middleware') as $middleware) { $pipe->pipe($this->app->make($middleware));