8000 feature #52962 [FrameworkBundle] Move Router cache directory to `kern… · symfony/symfony@454d16b · GitHub
[go: up one dir, main page]

Skip to content

Commit 454d16b

Browse files
committed
feature #52962 [FrameworkBundle] Move Router cache directory to kernel.build_dir (Okhoshi)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [FrameworkBundle] Move Router cache directory to `kernel.build_dir` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | none | License | MIT Follow up to #50391, set up Router cache directory to `kernel.build_dir` instead of `kernel.cache_dir` by default, and only warm the cache on read-only resources phase. #SymfonyHackday Commits ------- 1f031f8 [FrameworkBundle] Move Router cache directory to `kernel.build_dir`
2 parents 2bf8fb9 + 1f031f8 commit 454d16b

File tree

6 files changed

+55
-13
lines changed

6 files changed

+55
-13
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Move the Router `cache_dir` to `kernel.build_dir`
8+
* Deprecate the `router.cache_dir` config option
9+
410
7.0
511
---
612

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function __construct(ContainerInterface $container)
3636

3737
public function warmUp(string $cacheDir, string $buildDir = null): array
3838
{
39+
if (!$buildDir) {
40+
return [];
41+
}
42+
3943
$router = $this->container->get('router');
4044

4145
if ($router instanceof WarmableInterface) {

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,10 @@ private function addRouterSection(ArrayNodeDefinition $rootNode): void
613613
->children()
614614
->scalarNode('resource')->isRequired()->end()
615615
->scalarNode('type')->end()
616-
->scalarNode('cache_dir')->defaultValue('%kernel.cache_dir%')->end()
616+
->scalarNode('cache_dir')
617+
->defaultValue('%kernel.build_dir%')
618+
->setDeprecated('symfony/framework-bundle', '7.1', 'Setting the "%path%.%node%" configuration option is deprecated. It will be removed in version 8.0.')
619+
->end()
617620
->scalarNode('default_uri')
618621
->info('The default URI used to generate URLs in a non-HTTP context')
619622
->defaultNull()

src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ public function getRouteCollection(): RouteCollection
8282

8383
public function warmUp(string $cacheDir, string $buildDir = null): array
8484
{
85+
if (!$buildDir) {
86+
return [];
87+
}
88+
8589
$currentDir = $this->getOption('cache_dir');
8690

87-
// force cache generation
88-
$this->setOption('cache_dir', $cacheDir);
91+
// force cache generation in build_dir
92+
$this->setOption('cache_dir', $buildDir);
8993
$this->getMatcher();
9094
$this->getGenerator();
9195

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/RouterCacheWarmerTest.php

+34-9
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,61 @@
1919

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

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

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

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

39-
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmebleInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
39+
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
4040
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
4141
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
4242
$this->expectException(\LogicException::class);
4343
$this->expectExceptionMessage('cannot be warmed up because it does not implement "Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface"');
44-
$routerCacheWarmer->warmUp('/tmp');
44+
$routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build');
45+
}
46+
47+
public function testWarmUpWithWarmableInterfaceWithoutBuildDir()
48+
{
49+
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
50+
51+
$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
52+
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
53+
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
54+
55+
$preload = $routerCacheWarmer->warmUp('/tmp');
56+
$routerMock->expects($this->never())->method('warmUp');
57+
self::assertSame([], $preload);
58+
$this->addToAssertionCount(1);
59+
}
60+
61+
public function testWarmUpWithoutWarmableInterfaceWithoutBuildDir()
62+
{
63+
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
64+
65+
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
66+
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
67+
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
68+
$preload = $routerCacheWarmer->warmUp('/tmp');
69+
self::assertSame([], $preload);
4570
}
4671
}
4772

48-
interface testRouterInterfaceWithWarmebleInterface extends RouterInterface, WarmableInterface
73+
interface testRouterInterfaceWithWarmableInterface extends RouterInterface, WarmableInterface
4974
{
5075
}
5176

52-
interface testRouterInterfaceWithoutWarmebleInterface extends RouterInterface
77+
interface testRouterInterfaceWithoutWarmableInterface extends RouterInterface
5378
{
5479
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ protected static function getBundleDefaultConfig()
634634
'https_port' => 443,
635635
'strict_requirements' => true,
636636
'utf8' => true,
637-
'cache_dir' => '%kernel.cache_dir%',
637+
'cache_dir' => '%kernel.build_dir%',
638638
],
639639
'session' => [
640640
'enabled' => false,

0 commit comments

Comments
 (0)
0