From 31480dd1ed60150469eb97643afc539839aa45a3 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 22 Aug 2018 09:17:43 +0200 Subject: [PATCH] Load middleware stacks lazily This way, the forum middleware does not need to be loaded for API requests, and vice-versa. --- framework/core/composer.json | 2 ++ .../core/src/Foundation/InstalledApp.php | 21 +++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/framework/core/composer.json b/framework/core/composer.json index f0f3ce9ff..a2fdf8000 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -44,6 +44,8 @@ "league/flysystem": "^1.0.11", "league/oauth2-client": "~1.0", "matthiasmullie/minify": "^1.3", + "middlewares/base-path-router": "^0.2.1", + "middlewares/request-handler": "^1.2", "monolog/monolog": "^1.16.0", "nikic/fast-route": "^0.6", "oyejorge/less.php": "^1.7", diff --git a/framework/core/src/Foundation/InstalledApp.php b/framework/core/src/Foundation/InstalledApp.php index 9149f3806..d5a94089a 100644 --- a/framework/core/src/Foundation/InstalledApp.php +++ b/framework/core/src/Foundation/InstalledApp.php @@ -18,8 +18,9 @@ use Flarum\Foundation\Console\CacheClearCommand; use Flarum\Foundation\Console\InfoCommand; use Flarum\Http\Middleware\DispatchRoute; use Flarum\Settings\SettingsRepositoryInterface; +use Middlewares\BasePathRouter; +use Middlewares\RequestHandler; use Zend\Stratigility\MiddlewarePipe; -use function Zend\Stratigility\path; class InstalledApp implements AppInterface { @@ -52,9 +53,14 @@ class InstalledApp implements AppInterface $pipe = new MiddlewarePipe; - $pipe->pipe($this->subPath('api', 'flarum.api.middleware')); - $pipe->pipe($this->subPath('admin', 'flarum.admin.middleware')); - $pipe->pipe($this->subPath('', 'flarum.forum.middleware')); + $pipe->pipe( + new BasePathRouter([ + $this->subPath('api') => 'flarum.api.middleware', + $this->subPath('admin') => 'flarum.admin.middleware', + $this->subPath('') => 'flarum.forum.middleware', + ]) + ); + $pipe->pipe(new RequestHandler($this->laravel)); return $pipe; } @@ -85,12 +91,9 @@ class InstalledApp implements AppInterface return $pipe; } - private function subPath($pathName, $middlewareStack) + private function subPath($pathName): string { - return path( - parse_url($this->laravel->url($pathName), PHP_URL_PATH) ?: '/', - $this->laravel->make($middlewareStack) - ); + return parse_url($this->laravel->url($pathName), PHP_URL_PATH) ?: '/'; } /**