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.
This commit is contained in:
Franz Liedke 2020-05-24 08:47:10 +02:00
parent c6fc8a8cd6
commit 82c110c7b6
3 changed files with 24 additions and 17 deletions

View File

@ -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));

View File

@ -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));

View File

@ -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));