Skip to content

[FrameworkBundle] Move Router cache directory to kernel.build_dir #52962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

7.1
---

* Move the Router `cache_dir` to `kernel.build_dir`
* Deprecate the `router.cache_dir` config option

7.0
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function __construct(ContainerInterface $container)

public function warmUp(string $cacheDir, string $buildDir = null): array
{
if (!$buildDir) {
return [];
}

$router = $this->container->get('router');

if ($router instanceof WarmableInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,10 @@ private function addRouterSection(ArrayNodeDefinition $rootNode): void
->children()
->scalarNode('resource')->isRequired()->end()
->scalarNode('type')->end()
->scalarNode('cache_dir')->defaultValue('%kernel.cache_dir%')->end()
->scalarNode('cache_dir')
->defaultValue('%kernel.build_dir%')
->setDeprecated('symfony/framework-bundle', '7.1', 'Setting the "%path%.%node%" configuration option is deprecated. It will be removed in version 8.0.')
->end()
->scalarNode('default_uri')
->info('The default URI used to generate URLs in a non-HTTP context')
->defaultNull()
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,14 @@ public function getRouteCollection(): RouteCollection

public function warmUp(string $cacheDir, string $buildDir = null): array
{
if (!$buildDir) {
return [];
}

$currentDir = $this->getOption('cache_dir');

// force cache generation
$this->setOption('cache_dir', $cacheDir);
// force cache generation in build_dir
$this->setOption('cache_dir', $buildDir);
$this->getMatcher();
$this->getGenerator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,61 @@

class RouterCacheWarmerTest extends TestCase
{
public function testWarmUpWithWarmebleInterface()
public function testWarmUpWithWarmableInterfaceWithBuildDir()
{
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();

$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmebleInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
$routerCacheWarmer = new RouterCacheWarmer($containerMock);

$routerCacheWarmer->warmUp('/tmp');
$routerMock->expects($this->any())->method('warmUp')->with('/tmp')->willReturn([]);
$routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build');
$routerMock->expects($this->any())->method('warmUp')->with('/tmp/cache', '/tmp/build')->willReturn([]);
$this->addToAssertionCount(1);
}

public function testWarmUpWithoutWarmebleInterface()
public function testWarmUpWithoutWarmableInterfaceWithBuildDir()
{
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();

$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmebleInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('cannot be warmed up because it does not implement "Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface"');
$routerCacheWarmer->warmUp('/tmp');
$routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build');
}

public function testWarmUpWithWarmableInterfaceWithoutBuildDir()
{
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();

$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
$routerCacheWarmer = new RouterCacheWarmer($containerMock);

$preload = $routerCacheWarmer->warmUp('/tmp');
$routerMock->expects($this->never())->method('warmUp');
self::assertSame([], $preload);
$this->addToAssertionCount(1);
}

public function testWarmUpWithoutWarmableInterfaceWithoutBuildDir()
{
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();

$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
$preload = $routerCacheWarmer->warmUp('/tmp');
self::assertSame([], $preload);
}
}

interface testRouterInterfaceWithWarmebleInterface extends RouterInterface, WarmableInterface
interface testRouterInterfaceWithWarmableInterface extends RouterInterface, WarmableInterface
{
}

interface testRouterInterfaceWithoutWarmebleInterface extends RouterInterface
interface testRouterInterfaceWithoutWarmableInterface extends RouterInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ protected static function getBundleDefaultConfig()
'https_port' => 443,
'strict_requirements' => true,
'utf8' => true,
'cache_dir' => '%kernel.cache_dir%',
'cache_dir' => '%kernel.build_dir%',
],
'session' => [
'enabled' => false,
Expand Down