File tree Expand file tree Collapse file tree 5 files changed +93
-1
lines changed
src/Illuminate/Foundation Expand file tree Collapse file tree 5 files changed +93
-1
lines changed Original file line number Diff line number Diff line change @@ -1326,7 +1326,8 @@ public function getCachedConfigPath()
13261326 */
13271327 public function routesAreCached ()
13281328 {
1329- return $ this ['files ' ]->exists ($ this ->getCachedRoutesPath ());
1329+ return ($ this ->bound ('routes.cached ' ) && $ this ->make ('routes.cached ' ) === true ) ||
1330+ $ this ['files ' ]->exists ($ this ->getCachedRoutesPath ());
13301331 }
13311332
13321333 /**
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Illuminate \Foundation \Testing ;
4+
5+ class CachedState
6+ {
7+ public static array $ cachedRoutes ;
8+ }
Original file line number Diff line number Diff line change @@ -29,6 +29,11 @@ public function createApplication()
2929 {
3030 $ app = require Application::inferBasePath ().'/bootstrap/app.php ' ;
3131
32+ if (isset (CachedState::$ cachedRoutes ) &&
33+ in_array (WithCachedRoutes::class, class_uses_recursive (static ::class))) {
34+ $ app ->booting (fn () => $ this ->markRoutesCached ($ app ));
35+ }
36+
3237 $ app ->make (Kernel::class)->bootstrap ();
3338
3439 return $ app ;
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Illuminate \Foundation \Testing ;
4+
5+ use Illuminate \Foundation \Application ;
6+ use Illuminate \Foundation \Support \Providers \RouteServiceProvider ;
7+
8+ trait WithCachedRoutes
9+ {
10+ /**
11+ * After creating the routes once, we can cache them for the remaining tests.
12+ *
13+ * @return void
14+ */
15+ protected function setUpWithCachedRoutes (): void
16+ {
17+ if ((CachedState::$ cachedRoutes ?? null ) === null ) {
18+ $ routes = $ this ->app ['router ' ]->getRoutes ();
19+
20+ $ routes ->refreshNameLookups ();
21+ $ routes ->refreshActionLookups ();
22+
23+ CachedState::$ cachedRoutes = $ routes ->compile ();
24+ }
25+
26+ $ this ->markRoutesCached ($ this ->app );
27+ }
28+
29+ /**
30+ * Reset the route service provider so it's not defaulting to loading cached routes.
31+ *
32+ * This is helpful if some of the tests in the suite apply this trait while others do not.
33+ *
34+ * @return void
35+ */
36+ protected function tearDownWithCachedRoutes (): void
37+ {
38+ RouteServiceProvider::loadCachedRoutesUsing (null );
39+ }
40+
41+ /**
42+ * Inform the container to treat routes as cached.
43+ */
44+ protected function markRoutesCached (Application $ app ): void
45+ {
46+ $ app ->instance ('routes.cached ' , true );
47+
48+ RouteServiceProvider::loadCachedRoutesUsing (
49+ static fn () => app ('router ' )->setCompiledRoutes (CachedState::$ cachedRoutes )
50+ );
51+ }
52+
53+ }
Original file line number Diff line number Diff line change @@ -609,6 +609,31 @@ public function testAbortAcceptsHeaders()
609609 $ this ->assertSame (['X-FOO ' => 'BAR ' ], $ exception ->getHeaders ());
610610 }
611611 }
612+
613+ public function test_routes_are_cached ()
614+ {
615+ $ app = new Application ();
616+ $ app ->instance ('routes.cached ' , true );
617+ $ this ->assertTrue ($ app ->routesAreCached ());
618+ }
619+
620+ public function test_routes_are_not_cached_by_instance_falls_back_to_file ()
621+ {
622+ $ app = new Application ();
623+ $ files = new class
624+ {
625+ public string $ pathRequested ;
626+ public function exists (string $ path ): bool
627+ {
628+ $ this ->pathRequested = $ path ;
629+ return false ;
630+ }
631+ };
632+ $ app ->instance ('files ' , $ files );
633+
634+ $ this ->assertFalse ($ app ->routesAreCached ());
635+ $ this ->assertStringContainsString ('routes-v7.php ' , $ files ->pathRequested );
636+ }
612637}
613638
614639class ApplicationBasicServiceProviderStub extends ServiceProvider
You can’t perform that action at this time.
0 commit comments