10000 [FrameworkBundle] Make `SerializeCacheWarmer` use `kernel.build_dir` … · symfony/symfony@60296eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 60296eb

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 7008f22 commit 60296eb

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Deprecate the `router.cache_dir` config option
99
* Remove `AbstractPhpFileCacheWarmer`'s constructor argument, override new `getPhpArrayFile` method instead
1010
* Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
11+
* Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
1112

1213
7.0
1314
---

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
1313

1414
use Symfony\Component\Cache\Adapter\ArrayAdapter;
15+
use Symfony\Component\Filesystem\Path;
1516
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
1617
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
1718
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
@@ -28,18 +29,24 @@ class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
2829
{
2930
private array $loaders;
3031

32+
private string $phpArrayFile;
33+
3134
/**
3235
* @param LoaderInterface[] $loaders The serializer metadata loaders
3336
* @param string $phpArrayFile The PHP file where metadata are cached
3437
*/
3538
public function __construct(array $loaders, string $phpArrayFile)
3639
{
37-
parent::__construct($phpArrayFile);
40+
parent::__construct();
3841
$this->loaders = $loaders;
42+
$this->phpArrayFile = $phpArrayFile;
3943
}
4044

4145
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, string $buildDir = null): bool
4246
{
47+
if (!$buildDir) {
48+
return false;
49+
}
4350
if (!$this->loaders) {
4451
return true;
4552
}
@@ -59,6 +66,18 @@ protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, string
5966
return true;
6067
}
6168

69+
protected function getPhpArrayFile(string $cacheDir, string $buildDir = null): ?string
70+
{
71+
if (!$buildDir) {
72+
return null;
73+
}
74+
75+
if (Path::isRelative($this->phpArrayFile)) {
76+
return Path::join($buildDir, $this->phpArrayFile);
77+
}
78+
return $this->phpArrayFile;
79+
}
80+
6281
/**
6382
* @param LoaderInterface[] $loaders
6483
*

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

32+
$warmer = new SerializerCacheWarmer($loaders, basename($file));
33+
$warmer->warmUp(\dirname($file), \dirname($file));
34+
35+
$this->assertFileExists($file);
36+
37+
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
38+
39+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit());
40+
$this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit());
41+
}
42+
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+
3253
$warmer = new SerializerCacheWarmer($loaders, $file);
33-
$warmer->warmUp(\dirname($file));
54+
$warmer->warmUp($cacheDir, $cacheDir);
3455

3556
$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, basename($file));
74+
$warmer->warmUp(\dirname($file));
75+
76+
$this->assertFileDoesNotExist($file);
3677

3778
$arrayPool = new PhpArrayAdapter($file, new NullAdapter());
3879

@@ -65,8 +106,8 @@ public function testWarmUpWithoutLoader()
65106
$file = sys_get_temp_dir().'/cache-serializer-without-loader.php';
66107
@unlink($file);
67108

68-
$warmer = new SerializerCacheWarmer([], $file);
69-
$warmer->warmUp(\dirname($file));
109+
$warmer = new SerializerCacheWarmer([], basename($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')], basename($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)
0