From 1dfbc39594906958f55ce01ed034d8c8b3814d6d Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 18 Jan 2024 19:17:26 +0800 Subject: [PATCH 1/4] [11.x] Allow `RouteServiceProvider` to be loaded more than once --- .../Configuration/ApplicationBuilder.php | 2 +- .../Providers/RouteServiceProvider.php | 10 +-- .../Providers/RouteServiceProviderTest.php | 71 +++++++++++++++++++ .../Support/Providers/bootstrap.php | 27 +++++++ 4 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php create mode 100644 tests/Integration/Foundation/Support/Providers/bootstrap.php diff --git a/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php b/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php index 9ee99e97646a..21fcade21dde 100644 --- a/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php +++ b/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php @@ -130,7 +130,7 @@ public function withRouting(?Closure $using = null, AppRouteServiceProvider::loadRoutesUsing($using); $this->app->booting(function () { - $this->app->register(AppRouteServiceProvider::class); + $this->app->register(AppRouteServiceProvider::class, force: true); }); if (is_string($commands) && realpath($commands) !== false) { diff --git a/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php b/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php index 9c9ba4f90be6..323f0f32b42a 100644 --- a/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php +++ b/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php @@ -90,7 +90,7 @@ protected function routes(Closure $routesCallback) */ public static function loadRoutesUsing(Closure $routesCallback) { - static::$alwaysLoadRoutesUsing = $routesCallback; + self::$alwaysLoadRoutesUsing = $routesCallback; } /** @@ -134,9 +134,11 @@ protected function loadCachedRoutes() */ protected function loadRoutes() { - if (! is_null(static::$alwaysLoadRoutesUsing)) { - $this->app->call(static::$alwaysLoadRoutesUsing); - } elseif (! is_null($this->loadRoutesUsing)) { + if (! is_null(self::$alwaysLoadRoutesUsing)) { + $this->app->call(self::$alwaysLoadRoutesUsing); + } + + if (! is_null($this->loadRoutesUsing)) { $this->app->call($this->loadRoutesUsing); } elseif (method_exists($this, 'map')) { $this->app->call([$this, 'map']); diff --git a/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php new file mode 100644 index 000000000000..6b542539f421 --- /dev/null +++ b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php @@ -0,0 +1,71 @@ +> + */ + protected function getPackageProviders($app) + { + return [ + AppRouteServiceProvider::class, + ]; + } + + public function test_it_can_register_multiple_route_service_providers() + { + Assert::assertArraySubset([ + RouteServiceProvider::class => true, + AppRouteServiceProvider::class => true, + ], $this->app->getLoadedProviders()); + } + + public function test_it_can_uses_routes_registered_using_bootstrap_file() + { + $this->get(route('login')) + ->assertOk() + ->assertSee('Login'); + } + + public function test_it_can_uses_routes_registered_using_configuration_file() + { + $this->get('dashboard') + ->assertOk() + ->assertSee('Hello'); + } +} + +class AppRouteServiceProvider extends RouteServiceProvider +{ + /** + * Bootstrap any application services. + * + * @return void + */ + public function boot() + { + $this->routes(function () { + Route::get('dashboard', fn () => 'Hello')->name('dashboard'); + }); + } +} diff --git a/tests/Integration/Foundation/Support/Providers/bootstrap.php b/tests/Integration/Foundation/Support/Providers/bootstrap.php new file mode 100644 index 000000000000..117c0b804d03 --- /dev/null +++ b/tests/Integration/Foundation/Support/Providers/bootstrap.php @@ -0,0 +1,27 @@ +withProviders() + ->withRouting( + using: function () { + Route::get('login', fn () => 'Login')->name('login'); + } + ) + ->withMiddleware(function (Middleware $middleware) { + // + }) + ->withExceptions(function (Exceptions $exceptions) { + // + })->create(); From aa83dac13089700451ae526ecc2cb9b9d6dd6e44 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 18 Jan 2024 19:25:17 +0800 Subject: [PATCH 2/4] wip Signed-off-by: Mior Muhammad Zaki --- .../Foundation/Support/Providers/RouteServiceProviderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php index 6b542539f421..f707955a589e 100644 --- a/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php +++ b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php @@ -49,7 +49,7 @@ public function test_it_can_uses_routes_registered_using_bootstrap_file() public function test_it_can_uses_routes_registered_using_configuration_file() { - $this->get('dashboard') + $this->get(route('dashboard')) ->assertOk() ->assertSee('Hello'); } From 88ca3abadad748ffb35403344fb90ee5f498d742 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 18 Jan 2024 19:36:19 +0800 Subject: [PATCH 3/4] wip Signed-off-by: Mior Muhammad Zaki --- .../Providers/RouteServiceProviderTest.php | 17 +++++++++++- .../Support/Providers/bootstrap.php | 27 ------------------- 2 files changed, 16 insertions(+), 28 deletions(-) delete mode 100644 tests/Integration/Foundation/Support/Providers/bootstrap.php diff --git a/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php index f707955a589e..84a0badda83f 100644 --- a/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php +++ b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php @@ -2,6 +2,9 @@ namespace Illuminate\Tests\Integration\Foundation\Support\Providers; +use Illuminate\Foundation\Application; +use Illuminate\Foundation\Configuration\Exceptions; +use Illuminate\Foundation\Configuration\Middleware; use Illuminate\Foundation\Support\Providers\RouteServiceProvider; use Illuminate\Support\Facades\Route; use Illuminate\Testing\Assert; @@ -16,7 +19,19 @@ class RouteServiceProviderTest extends TestCase */ protected function resolveApplication() { - return require __DIR__.'/bootstrap.php'; + return Application::configure(static::applicationBasePath()) + ->withProviders() + ->withRouting( + using: function () { + Route::get('login', fn () => 'Login')->name('login'); + } + ) + ->withMiddleware(function (Middleware $middleware) { + // + }) + ->withExceptions(function (Exceptions $exceptions) { + // + })->create(); } /** diff --git a/tests/Integration/Foundation/Support/Providers/bootstrap.php b/tests/Integration/Foundation/Support/Providers/bootstrap.php deleted file mode 100644 index 117c0b804d03..000000000000 --- a/tests/Integration/Foundation/Support/Providers/bootstrap.php +++ /dev/null @@ -1,27 +0,0 @@ -withProviders() - ->withRouting( - using: function () { - Route::get('login', fn () => 'Login')->name('login'); - } - ) - ->withMiddleware(function (Middleware $middleware) { - // - }) - ->withExceptions(function (Exceptions $exceptions) { - // - })->create(); From 4acd16c4b22240aaddbb58d48d033c0474e844e2 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Thu, 18 Jan 2024 19:37:05 +0800 Subject: [PATCH 4/4] wip Signed-off-by: Mior Muhammad Zaki --- .../Providers/RouteServiceProviderTest.php | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php index 84a0badda83f..20005e6270dc 100644 --- a/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php +++ b/tests/Integration/Foundation/Support/Providers/RouteServiceProviderTest.php @@ -20,7 +20,9 @@ class RouteServiceProviderTest extends TestCase protected function resolveApplication() { return Application::configure(static::applicationBasePath()) - ->withProviders() + ->withProviders([ + AppRouteServiceProvider::class, + ]) ->withRouting( using: function () { Route::get('login', fn () => 'Login')->name('login'); @@ -34,19 +36,6 @@ protected function resolveApplication() })->create(); } - /** - * Get package providers. - * - * @param \Illuminate\Foundation\Application $app - * @return array> - */ - protected function getPackageProviders($app) - { - return [ - AppRouteServiceProvider::class, - ]; - } - public function test_it_can_register_multiple_route_service_providers() { Assert::assertArraySubset([