diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index ef99215b2441..c8680628b45b 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -1322,7 +1322,8 @@ public function getCachedConfigPath() */ public function routesAreCached() { - return $this['files']->exists($this->getCachedRoutesPath()); + return ($this->bound('routes.cached') && $this->make('routes.cached') === true) || + $this['files']->exists($this->getCachedRoutesPath()); } /** diff --git a/src/Illuminate/Foundation/Testing/CachedState.php b/src/Illuminate/Foundation/Testing/CachedState.php new file mode 100644 index 000000000000..79cd3e0e906b --- /dev/null +++ b/src/Illuminate/Foundation/Testing/CachedState.php @@ -0,0 +1,8 @@ +booting(fn () => $this->markRoutesCached($app)); + } + $app->make(Kernel::class)->bootstrap(); return $app; diff --git a/src/Illuminate/Foundation/Testing/WithCachedRoutes.php b/src/Illuminate/Foundation/Testing/WithCachedRoutes.php new file mode 100644 index 000000000000..05b57052ef4d --- /dev/null +++ b/src/Illuminate/Foundation/Testing/WithCachedRoutes.php @@ -0,0 +1,53 @@ +app['router']->getRoutes(); + + $routes->refreshNameLookups(); + $routes->refreshActionLookups(); + + CachedState::$cachedRoutes = $routes->compile(); + } + + $this->markRoutesCached($this->app); + } + + /** + * Reset the route service provider so it's not defaulting to loading cached routes. + * + * This is helpful if some of the tests in the suite apply this trait while others do not. + * + * @return void + */ + protected function tearDownWithCachedRoutes(): void + { + RouteServiceProvider::loadCachedRoutesUsing(null); + } + + /** + * Inform the container to treat routes as cached. + */ + protected function markRoutesCached(Application $app): void + { + $app->instance('routes.cached', true); + + RouteServiceProvider::loadCachedRoutesUsing( + static fn () => app('router')->setCompiledRoutes(CachedState::$cachedRoutes) + ); + } + +} diff --git a/tests/Foundation/FoundationApplicationTest.php b/tests/Foundation/FoundationApplicationTest.php index d403e4a0702a..86c34b542f93 100755 --- a/tests/Foundation/FoundationApplicationTest.php +++ b/tests/Foundation/FoundationApplicationTest.php @@ -609,6 +609,31 @@ public function testAbortAcceptsHeaders() $this->assertSame(['X-FOO' => 'BAR'], $exception->getHeaders()); } } + + public function test_routes_are_cached() + { + $app = new Application(); + $app->instance('routes.cached', true); + $this->assertTrue($app->routesAreCached()); + } + + public function test_routes_are_not_cached_by_instance_falls_back_to_file() + { + $app = new Application(); + $files = new class + { + public string $pathRequested; + public function exists(string $path): bool + { + $this->pathRequested = $path; + return false; + } + }; + $app->instance('files', $files); + + $this->assertFalse($app->routesAreCached()); + $this->assertStringContainsString('routes-v7.php', $files->pathRequested); + } } class ApplicationBasicServiceProviderStub extends ServiceProvider