Skip to content

Commit 5a11934

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 4b96b08 commit 5a11934

File tree

4 files changed

+85
-12
lines changed

4 files changed

+85
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Deprecate the `router.cache_dir` config option
1212
* Remove `AbstractPhpFileCacheWarmer`'s constructor argument, override new `getPhpArrayFile` method instead
1313
* Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
14+
* Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir`
1415
* Add `rate_limiter` tags to rate limiter services
1516
* Add `secrets:reveal` command
1617
* Add `rate_limiter` option to `http_client.default_options` and `http_client.scoped_clients`

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

Lines changed: 19 additions & 2 deletions
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;
@@ -34,13 +35,16 @@ class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
3435
*/
3536
public function __construct(
3637
private array $loaders,
37-
string $phpArrayFile,
38+
private string $phpArrayFile,
3839
) {
39-
parent::__construct($phpArrayFile);
40+
parent::__construct();
4041
}
4142

4243
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool
4344
{
45+
if (!$buildDir) {
46+
return false;
47+
}
4448
if (!$this->loaders) {
4549
return true;
4650
}
@@ -60,6 +64,19 @@ protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?strin
6064
return true;
6165
}
6266

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

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)
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