Skip to content

Commit

Permalink
[FrameworkBundle] Move Router cache directory to kernel.build_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Okhoshi authored and fabpot committed Dec 13, 2023
1 parent c4e97eb commit 1f031f8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
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

0 comments on commit 1f031f8

Please sign in to comment.