Skip to content

Commit ffc97f8

Browse files
committed
[FrameworkBundle] Make SerializeCacheWarmer use kernel.build_dir instead of cache_dir
Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com>
1 parent 3247977 commit ffc97f8

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Move the Router `cache_dir` to `kernel.build_dir`
1111
* Deprecate the `router.cache_dir` config option
1212
* Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
13+
* Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
1314
* Add `rate_limiter` tags to rate limiter services
1415
* Add `secrets:reveal` command
1516
* Add `rate_limiter` option to `http_client.default_options` and `http_client.scoped_clients`

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public function __construct(
4141

4242
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool
4343
{
44+
if (!$buildDir) {
45+
return false;
46+
}
4447
if (!$this->loaders) {
4548
return true;
4649
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
return static function (ContainerConfigurator $container) {
5656
$container->parameters()
57-
->set('serializer.mapping.cache.file', '%kernel.cache_dir%/serialization.php')
57+
->set('serializer.mapping.cache.file', '%kernel.build_dir%/serialization.php')
5858
;
5959

6060
$container->services()
@@ -160,7 +160,10 @@
160160

161161
->set('serializer.mapping.cache.symfony', CacheItemPoolInterface::class)
162162
->factory([PhpArrayAdapter::class, 'create'])
163-
->args([param('serializer.mapping.cache.file'), service('cache.serializer')])
163+
->args([
164+
param('serializer.mapping.cache.file'),
165+
service('cache.serializer'),
166+
])
164167

165168
->set('serializer.mapping.cache_class_metadata_factory', CacheClassMetadataFactory::class)
166169
->decorate('serializer.mapping.class_metadata_factory')

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

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testWarmUp(array $loaders)
3030
@unlink($file);
3131

3232
$warmer = new SerializerCacheWarmer($loaders, $file);
33-
$warmer->warmUp(\dirname($file));
33+
$warmer->warmUp(\dirname($file), \dirname($file));
3434

3535
$this->assertFileExists($file);
3636

@@ -40,6 +40,47 @@ public function testWarmUp(array $loaders)
4040
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
4141
}
4242

43+
/**
44+
* @dataProvider loaderProvider
45+
*/
46+
public function testWarmUpAbsoluteFilePath(array $loaders)
47+
{
48+
$file = sys_get_temp_dir().'/0/cache-serializer.php';
49+
@unlink($file);
50+
51+
$cacheDir = sys_get_temp_dir().'/1';
52+
53+
$warmer = new SerializerCacheWarmer($loaders, $file);
54+
$warmer->warmUp($cacheDir, $cacheDir);
55+
56+
$this->assertFileExists($file);
57+
$this->assertFileDoesNotExist($cacheDir.'/cache-serializer.php');
58+
59+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
60+
61+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
62+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
63+
}
64+
65+
/**
66+
* @dataProvider loaderProvider
67+
*/
68+
public function testWarmUpWithoutBuildDir(array $loaders)
69+
{
70+
$file = sys_get_temp_dir().'/cache-serializer.php';
71+
@unlink($file);
72+
73+
$warmer = new SerializerCacheWarmer($loaders, $file);
74+
$warmer->warmUp(\dirname($file));
75+
76+
$this->assertFileDoesNotExist($file);
77+
78+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
79+
80+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
81+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
82+
}
83+
4384
public static function loaderProvider(): array
4485
{
4586
return [
@@ -66,7 +107,7 @@ public function testWarmUpWithoutLoader()
66107
@unlink($file);
67108

68109
$warmer = new SerializerCacheWarmer([], $file);
69-
$warmer->warmUp(\dirname($file));
110+
$warmer->warmUp(\dirname($file), \dirname($file));
70111

71112
$this->assertFileExists($file);
72113
}
@@ -79,15 +120,19 @@ public function testClassAutoloadException()
79120
{
80121
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));
81122

82-
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__));
123+
$file = tempnam(sys_get_temp_dir(), __FUNCTION__);
124+
@unlink($file);
125+
126+
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], $file);
83127

84128
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
85129
if ($class === $mappedClass) {
86130
throw new \DomainException('This exception should be caught by the warmer.');
87131
}
88132
}, true, true);
89133

90-
$warmer->warmUp('foo');
134+
$warmer->warmUp(\dirname($file), \dirname($file));
135+
$this->assertFileExists($file);
91136

92137
spl_autoload_unregister($classLoader);
93138
}
@@ -103,7 +148,10 @@ public function testClassAutoloadExceptionWithUnrelatedException()
103148

104149
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));
105150

106-
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__));
151+
$file = tempnam(sys_get_temp_dir(), __FUNCTION__);
152+
@unlink($file);
153+
154+
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], basename($file));
107155

108156
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
109157
if ($class === $mappedClass) {
@@ -112,8 +160,12 @@ public function testClassAutoloadExceptionWithUnrelatedException()
112160
}
113161
}, true, true);
114162

115-
$warmer->warmUp('foo');
163+
try {
164+
$warmer->warmUp(\dirname($file), \dirname($file));
165+
} finally {
166+
$this->assertFileDoesNotExist($file);
116167

117-
spl_autoload_unregister($classLoader);
168+
spl_autoload_unregister($classLoader);
169+
}
118170
}
119171
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy