Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,9 @@ 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());
Copy link
Contributor Author

@cosmastech cosmastech Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this check can and should be cached (regardless if this PR is accepted)

Unless there's a reason to expect that during a run (either with the traditional PHP model or using Octane) this file would be deleted, we are repeatedly performing a file_exists check.

}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Foundation/Testing/CachedState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Foundation\Testing;

class CachedState
{
public static array $cachedRoutes;
}
4 changes: 4 additions & 0 deletions src/Illuminate/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function createApplication()
{
$app = require Application::inferBasePath().'/bootstrap/app.php';

if (isset(CachedState::$cachedRoutes) && in_array(WithCachedRoutes::class, class_uses_recursive(static::class))) {
$app->booting(fn () => $this->markRoutesCached($app));
}

$app->make(Kernel::class)->bootstrap();

return $app;
Expand Down
52 changes: 52 additions & 0 deletions src/Illuminate/Foundation/Testing/WithCachedRoutes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Illuminate\Foundation\Testing;

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;

trait WithCachedRoutes
{
/**
* After creating the routes once, we can cache them for the remaining tests.
*
* @return void
*/
protected function setUpWithCachedRoutes(): void
{
// If we haven't stored the cached routes yet, then let's store them
// once so we can use them in the remaining tests.
if ((CachedState::$cachedRoutes ?? null) === null) {
$routes = $this->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.
* 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)
);
}

}
25 changes: 25 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down