Skip to content

Commit 38a2688

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 e0fd2ba commit 38a2688

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Remove `AbstractPhpFileCacheWarmer`'s constructor argument, override new `getPhpArrayFile` method instead
88
* Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
9+
* Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
910

1011
7.0
1112
---

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,27 @@ class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
2828
{
2929
private array $loaders;
3030

31+
private string $phpArrayFile;
32+
3133
/**
3234
* @param LoaderInterface[] $loaders The serializer metadata loaders
3335
* @param string $phpArrayFile The PHP file where metadata are cached
3436
*/
3537
public function __construct(array $loaders, string $phpArrayFile)
3638
{
37-
parent::__construct($phpArrayFile);
39+
if ($phpArrayFile !== basename($phpArrayFile)) {
40+
trigger_deprecation('symfony/framework-bundle', '7.1', 'Providing more than a filename to "string $phpArrayFile" argument is deprecated. The argument will be considered as a filename in 8.0.');
41+
}
42+
parent::__construct();
3843
$this->loaders = $loaders;
44+
$this->phpArrayFile = $phpArrayFile;
3945
}
4046

4147
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, string $buildDir = null): bool
4248
{
49+
if (!$buildDir) {
50+
return false;
51+
}
4352
if (!$this->loaders) {
4453
return true;
4554
}
@@ -59,6 +68,18 @@ protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, string
5968
return true;
6069
}
6170

71+
protected function getPhpArrayFile(string $cacheDir, string $buildDir = null): ?string
72+
{
73+
if (!$buildDir) {
74+
return null;
75+
}
76+
if ($this->phpArrayFile !== basename($this->phpArrayFile)) {
77+
return $this->phpArrayFile;
78+
}
79+
80+
return $buildDir.\DIRECTORY_SEPARATOR.$this->phpArrayFile;
81+
}
82+
6283
/**
6384
* @param LoaderInterface[] $loaders
6485
*

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', '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('kernel.build_dir').'/'.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: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function testWarmUp(array $loaders)
2929
$file = sys_get_temp_dir().'/cache-serializer.php';
3030
@unlink($file);
3131

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

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

@@ -40,6 +40,25 @@ 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 testWarmUpWithoutBuildDir(array $loaders)
47+
{
48+
$file = sys_get_temp_dir().'/cache-serializer.php';
49+
@unlink($file);
50+
51+
$warmer = new SerializerCacheWarmer($loaders, basename($file));
52+
$warmer->warmUp(\dirname($file));
53+
54+
$this->assertFileDoesNotExist($file);
55+
56+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
57+
58+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
59+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
60+
}
61+
4362
public static function loaderProvider(): array
4463
{
4564
return [
@@ -65,8 +84,8 @@ public function testWarmUpWithoutLoader()
6584
$file = sys_get_temp_dir().'/cache-serializer-without-loader.php';
6685
@unlink($file);
6786

68-
$warmer = new SerializerCacheWarmer([], $file);
69-
$warmer->warmUp(\dirname($file));
87+
$warmer = new SerializerCacheWarmer([], basename($file));
88+
$warmer->warmUp(\dirname($file), \dirname($file));
7089

7190
$this->assertFileExists($file);
7291
}
@@ -79,15 +98,19 @@ public function testClassAutoloadException()
7998
{
8099
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));
81100

82-
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__));
101+
$file = tempnam(sys_get_temp_dir(), __FUNCTION__);
102+
@unlink($file);
103+
104+
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], basename($file));
83105

84106
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
85107
if ($class === $mappedClass) {
86108
throw new \DomainException('This exception should be caught by the warmer.');
87109
}
88110
}, true, true);
89111

90-
$warmer->warmUp('foo');
112+
$warmer->warmUp(\dirname($file), \dirname($file));
113+
$this->assertFileExists($file);
91114

92115
spl_autoload_unregister($classLoader);
93116
}
@@ -103,7 +126,10 @@ public function testClassAutoloadExceptionWithUnrelatedException()
103126

104127
$this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false));
105128

106-
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__));
129+
$file = tempnam(sys_get_temp_dir(), __FUNCTION__);
130+
@unlink($file);
131+
132+
$warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], basename($file));
107133

108134
spl_autoload_register($classLoader = function ($class) use ($mappedClass) {
109135
if ($class === $mappedClass) {
@@ -112,8 +138,12 @@ public function testClassAutoloadExceptionWithUnrelatedException()
112138
}
113139
}, true, true);
114140

115-
$warmer->warmUp('foo');
141+
try {
142+
$warmer->warmUp(\dirname($file), \dirname($file));
143+
} finally {
144+
$this->assertFileDoesNotExist($file);
116145

117-
spl_autoload_unregister($classLoader);
146+
spl_autoload_unregister($classLoader);
147+
}
118148
}
119149
}

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