From 55136ddf95484c884533ce581dfe68ae9b726080 Mon Sep 17 00:00:00 2001 From: Claudio Dekker Date: Wed, 30 Sep 2020 15:23:44 +0200 Subject: [PATCH 1/2] Support Auto Registration of Middleware on Laravel 5.4+ --- src/ServiceProvider.php | 14 ++++++++++---- tests/ServiceProviderTest.php | 8 +++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 6b17041f..4a4048b7 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -53,12 +53,18 @@ protected function registerRouterMacro() protected function registerMiddleware() { $kernel = $this->app[Kernel::class]; + $group = Config::get('inertia.middleware_group', 'web'); + // Laravel >= 6.9.0 if (method_exists($kernel, 'appendMiddlewareToGroup')) { - $kernel->appendMiddlewareToGroup( - Config::get('inertia.middleware_group', 'web'), - Middleware::class - ); + $kernel->appendMiddlewareToGroup($group, Middleware::class); + + return; + } + + // Laravel >= 5.4.4 && < 6.9.0 + if ($this->app[Router::class]->hasMiddlewareGroup($group)) { + $this->app[Router::class]->pushMiddlewareToGroup($group, Middleware::class); } } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index fa6905bc..9bbca59b 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -6,6 +6,7 @@ use Inertia\Inertia; use Inertia\Middleware; use Illuminate\Http\Request; +use Illuminate\Routing\Router; use Illuminate\Support\MessageBag; use Illuminate\Support\Facades\App; use Illuminate\Support\ViewErrorBag; @@ -48,11 +49,12 @@ public function test_route_macro_is_registered() $this->assertEquals(['component' => 'User/Edit', 'props' => ['user' => ['name' => 'Jonathan']]], $route->defaults); } - public function test_middleware_is_registered() + public function test_middleware_is_registered_to_the_web_group() { - $middleware = App::make(Kernel::class)->getMiddlewareGroups(); + $route = Route::middleware('web')->get('/'); + $middleware = App::make(Router::class)->gatherRouteMiddleware($route); - $this->assertContains(Middleware::class, $middleware['web']); + $this->assertContains(Middleware::class, $middleware); } public function test_validation_errors_are_registered() From 1e4ae8631e18e9c5fa7400e20f37db6a971ff4ad Mon Sep 17 00:00:00 2001 From: Claudio Dekker Date: Wed, 30 Sep 2020 15:39:13 +0200 Subject: [PATCH 2/2] WIP --- src/ServiceProvider.php | 5 +---- tests/ServiceProviderTest.php | 10 +++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 4a4048b7..20c3e167 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -59,11 +59,8 @@ protected function registerMiddleware() if (method_exists($kernel, 'appendMiddlewareToGroup')) { $kernel->appendMiddlewareToGroup($group, Middleware::class); - return; - } - // Laravel >= 5.4.4 && < 6.9.0 - if ($this->app[Router::class]->hasMiddlewareGroup($group)) { + } elseif ($this->app[Router::class]->hasMiddlewareGroup($group)) { $this->app[Router::class]->pushMiddlewareToGroup($group, Middleware::class); } } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 9bbca59b..d5120fe3 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -51,10 +51,14 @@ public function test_route_macro_is_registered() public function test_middleware_is_registered_to_the_web_group() { - $route = Route::middleware('web')->get('/'); - $middleware = App::make(Router::class)->gatherRouteMiddleware($route); + $webRoute = Route::middleware('web')->get('/'); + $apiRoute = Route::middleware('api')->get('/'); - $this->assertContains(Middleware::class, $middleware); + $webMiddleware = App::make(Router::class)->gatherRouteMiddleware($webRoute); + $apiMiddleware = App::make(Router::class)->gatherRouteMiddleware($apiRoute); + + $this->assertContains(Middleware::class, $webMiddleware); + $this->assertNotContains(Middleware::class, $apiMiddleware); } public function test_validation_errors_are_registered()