From e35386897e93c76dab23e6ed0d02755d83d3b1a0 Mon Sep 17 00:00:00 2001 From: Quentin Devos <4972091+Okhoshi@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:15:58 +0100 Subject: [PATCH 1/2] [FrameworkBundle] Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir` Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com> --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../CacheWarmer/ValidatorCacheWarmer.php | 4 + .../Resources/config/validator.php | 2 +- .../CacheWarmer/ValidatorCacheWarmerTest.php | 81 ++++++++++++++++--- 4 files changed, 77 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 8e70fb98e42fe..e7afbbe241e4d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -25,6 +25,7 @@ CHANGELOG * Set `framework.rate_limiter.limiters.*.lock_factory` to `auto` by default * Deprecate `RateLimiterFactory` autowiring aliases, use `RateLimiterFactoryInterface` instead * Allow configuring compound rate limiters + * Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir` 7.2 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php index 6ecaa4bd14d01..9c313f80a8662 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php @@ -41,6 +41,10 @@ public function __construct( protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool { + if (!$buildDir) { + return false; + } + $loaders = $this->validatorBuilder->getLoaders(); $metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), $arrayAdapter); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.php index adde2de238e05..535b42edc1bc3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/validator.php @@ -28,7 +28,7 @@ return static function (ContainerConfigurator $container) { $container->parameters() - ->set('validator.mapping.cache.file', param('kernel.cache_dir').'/validation.php'); + ->set('validator.mapping.cache.file', '%kernel.build_dir%/validation.php'); $validatorsDir = \dirname((new \ReflectionClass(EmailValidator::class))->getFileName()); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php index cc471e43fc685..af0bb1b50d3dd 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php @@ -32,7 +32,7 @@ public function testWarmUp() @unlink($file); $warmer = new ValidatorCacheWarmer($validatorBuilder, $file); - $warmer->warmUp(\dirname($file)); + $warmer->warmUp(\dirname($file), \dirname($file)); $this->assertFileExists($file); @@ -42,6 +42,53 @@ public function testWarmUp() $this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit()); } + public function testWarmUpAbsoluteFilePath() + { + $validatorBuilder = new ValidatorBuilder(); + $validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml'); + $validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml'); + $validatorBuilder->addMethodMapping('loadValidatorMetadata'); + $validatorBuilder->enableAttributeMapping(); + + $file = sys_get_temp_dir().'/0/cache-validator.php'; + @unlink($file); + + $cacheDir = sys_get_temp_dir().'/1'; + + $warmer = new ValidatorCacheWarmer($validatorBuilder, $file); + $warmer->warmUp($cacheDir, $cacheDir); + + $this->assertFileExists($file); + $this->assertFileDoesNotExist($cacheDir.'/cache-validator.php'); + + $arrayPool = new PhpArrayAdapter($file, new NullAdapter()); + + $this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit()); + $this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit()); + } + + public function testWarmUpWithoutBuilDir() + { + $validatorBuilder = new ValidatorBuilder(); + $validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml'); + $validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml'); + $validatorBuilder->addMethodMapping('loadValidatorMetadata'); + $validatorBuilder->enableAttributeMapping(); + + $file = sys_get_temp_dir().'/cache-validator.php'; + @unlink($file); + + $warmer = new ValidatorCacheWarmer($validatorBuilder, $file); + $warmer->warmUp(\dirname($file)); + + $this->assertFileDoesNotExist($file); + + $arrayPool = new PhpArrayAdapter($file, new NullAdapter()); + + $this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Person')->isHit()); + $this->assertTrue($arrayPool->getItem('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author')->isHit()); + } + public function testWarmUpWithAnnotations() { $validatorBuilder = new ValidatorBuilder(); @@ -52,7 +99,7 @@ public function testWarmUpWithAnnotations() @unlink($file); $warmer = new ValidatorCacheWarmer($validatorBuilder, $file); - $warmer->warmUp(\dirname($file)); + $warmer->warmUp(\dirname($file), \dirname($file)); $this->assertFileExists($file); @@ -72,7 +119,7 @@ public function testWarmUpWithoutLoader() @unlink($file); $warmer = new ValidatorCacheWarmer($validatorBuilder, $file); - $warmer->warmUp(\dirname($file)); + $warmer->warmUp(\dirname($file), \dirname($file)); $this->assertFileExists($file); } @@ -85,9 +132,12 @@ public function testClassAutoloadException() { $this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest', false)); + $file = tempnam(sys_get_temp_dir(), __FUNCTION__); + @unlink($file); + $validatorBuilder = new ValidatorBuilder(); $validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/does_not_exist.yaml'); - $warmer = new ValidatorCacheWarmer($validatorBuilder, tempnam(sys_get_temp_dir(), __FUNCTION__)); + $warmer = new ValidatorCacheWarmer($validatorBuilder, $file); spl_autoload_register($classloader = function ($class) use ($mappedClass) { if ($class === $mappedClass) { @@ -95,7 +145,9 @@ public function testClassAutoloadException() } }, true, true); - $warmer->warmUp('foo'); + $warmer->warmUp(\dirname($file), \dirname($file)); + + $this->assertFileExists($file); spl_autoload_unregister($classloader); } @@ -106,14 +158,14 @@ public function testClassAutoloadException() */ public function testClassAutoloadExceptionWithUnrelatedException() { - $this->expectException(\DomainException::class); - $this->expectExceptionMessage('This exception should not be caught by the warmer.'); + $file = tempnam(sys_get_temp_dir(), __FUNCTION__); + @unlink($file); $this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_ValidatorCacheWarmerTest', false)); $validatorBuilder = new ValidatorBuilder(); $validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/does_not_exist.yaml'); - $warmer = new ValidatorCacheWarmer($validatorBuilder, tempnam(sys_get_temp_dir(), __FUNCTION__)); + $warmer = new ValidatorCacheWarmer($validatorBuilder, basename($file)); spl_autoload_register($classLoader = function ($class) use ($mappedClass) { if ($class === $mappedClass) { @@ -122,8 +174,17 @@ public function testClassAutoloadExceptionWithUnrelatedException() } }, true, true); - $warmer->warmUp('foo'); + $this->expectException(\DomainException::class); + $this->expectExceptionMessage('This exception should not be caught by the warmer.'); + + try { + $warmer->warmUp(\dirname($file), \dirname($file)); + } catch (\DomainException $e) { + $this->assertFileDoesNotExist($file); - spl_autoload_unregister($classLoader); + throw $e; + } finally { + spl_autoload_unregister($classLoader); + } } } From ae6f4e0ed2cf449ad47a0093af5bd1c90b919e0a Mon Sep 17 00:00:00 2001 From: Quentin Devos <4972091+Okhoshi@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:16:17 +0100 Subject: [PATCH 2/2] [FrameworkBundle] Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir` Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com> --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../CacheWarmer/SerializerCacheWarmer.php | 3 + .../Resources/config/serializer.php | 7 +- .../CacheWarmer/SerializerCacheWarmerTest.php | 74 ++++++++++++++++--- 4 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index e7afbbe241e4d..ec0d88fcea3f6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -26,6 +26,7 @@ CHANGELOG * Deprecate `RateLimiterFactory` autowiring aliases, use `RateLimiterFactoryInterface` instead * Allow configuring compound rate limiters * Make `ValidatorCacheWarmer` use `kernel.build_dir` instead of `cache_dir` + * Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir` 7.2 --- diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php index 46da4daaab4d1..fbf7083b70b28 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php @@ -41,6 +41,9 @@ public function __construct( protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter, ?string $buildDir = null): bool { + if (!$buildDir) { + return false; + } if (!$this->loaders) { return true; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php index 535b95a399248..5578924541096 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.php @@ -56,7 +56,7 @@ return static function (ContainerConfigurator $container) { $container->parameters() - ->set('serializer.mapping.cache.file', '%kernel.cache_dir%/serialization.php') + ->set('serializer.mapping.cache.file', '%kernel.build_dir%/serialization.php') ; $container->services() @@ -164,7 +164,10 @@ ->set('serializer.mapping.cache.symfony', CacheItemPoolInterface::class) ->factory([PhpArrayAdapter::class, 'create']) - ->args([param('serializer.mapping.cache.file'), service('cache.serializer')]) + ->args([ + param('serializer.mapping.cache.file'), + service('cache.serializer'), + ]) ->set('serializer.mapping.cache_class_metadata_factory', CacheClassMetadataFactory::class) ->decorate('serializer.mapping.class_metadata_factory') diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php index 5feb0c8ec1bd7..9b765c36a18e6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/SerializerCacheWarmerTest.php @@ -30,9 +30,50 @@ public function testWarmUp(array $loaders) @unlink($file); $warmer = new SerializerCacheWarmer($loaders, $file); - $warmer->warmUp(\dirname($file)); + $warmer->warmUp(\dirname($file), \dirname($file)); + + $this->assertFileExists($file); + + $arrayPool = new PhpArrayAdapter($file, new NullAdapter()); + + $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit()); + $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit()); + } + + /** + * @dataProvider loaderProvider + */ + public function testWarmUpAbsoluteFilePath(array $loaders) + { + $file = sys_get_temp_dir().'/0/cache-serializer.php'; + @unlink($file); + + $cacheDir = sys_get_temp_dir().'/1'; + + $warmer = new SerializerCacheWarmer($loaders, $file); + $warmer->warmUp($cacheDir, $cacheDir); $this->assertFileExists($file); + $this->assertFileDoesNotExist($cacheDir.'/cache-serializer.php'); + + $arrayPool = new PhpArrayAdapter($file, new NullAdapter()); + + $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Person')->isHit()); + $this->assertTrue($arrayPool->getItem('Symfony_Bundle_FrameworkBundle_Tests_Fixtures_Serialization_Author')->isHit()); + } + + /** + * @dataProvider loaderProvider + */ + public function testWarmUpWithoutBuildDir(array $loaders) + { + $file = sys_get_temp_dir().'/cache-serializer.php'; + @unlink($file); + + $warmer = new SerializerCacheWarmer($loaders, $file); + $warmer->warmUp(\dirname($file)); + + $this->assertFileDoesNotExist($file); $arrayPool = new PhpArrayAdapter($file, new NullAdapter()); @@ -66,7 +107,7 @@ public function testWarmUpWithoutLoader() @unlink($file); $warmer = new SerializerCacheWarmer([], $file); - $warmer->warmUp(\dirname($file)); + $warmer->warmUp(\dirname($file), \dirname($file)); $this->assertFileExists($file); } @@ -79,7 +120,10 @@ public function testClassAutoloadException() { $this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false)); - $warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__)); + $file = tempnam(sys_get_temp_dir(), __FUNCTION__); + @unlink($file); + + $warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], $file); spl_autoload_register($classLoader = function ($class) use ($mappedClass) { if ($class === $mappedClass) { @@ -87,7 +131,8 @@ public function testClassAutoloadException() } }, true, true); - $warmer->warmUp('foo'); + $warmer->warmUp(\dirname($file), \dirname($file)); + $this->assertFileExists($file); spl_autoload_unregister($classLoader); } @@ -98,12 +143,12 @@ public function testClassAutoloadException() */ public function testClassAutoloadExceptionWithUnrelatedException() { - $this->expectException(\DomainException::class); - $this->expectExceptionMessage('This exception should not be caught by the warmer.'); - $this->assertFalse(class_exists($mappedClass = 'AClassThatDoesNotExist_FWB_CacheWarmer_SerializerCacheWarmerTest', false)); - $warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], tempnam(sys_get_temp_dir(), __FUNCTION__)); + $file = tempnam(sys_get_temp_dir(), __FUNCTION__); + @unlink($file); + + $warmer = new SerializerCacheWarmer([new YamlFileLoader(__DIR__.'/../Fixtures/Serialization/Resources/does_not_exist.yaml')], basename($file)); spl_autoload_register($classLoader = function ($class) use ($mappedClass) { if ($class === $mappedClass) { @@ -112,8 +157,17 @@ public function testClassAutoloadExceptionWithUnrelatedException() } }, true, true); - $warmer->warmUp('foo'); + $this->expectException(\DomainException::class); + $this->expectExceptionMessage('This exception should not be caught by the warmer.'); + + try { + $warmer->warmUp(\dirname($file), \dirname($file)); + } catch (\DomainException $e) { + $this->assertFileDoesNotExist($file); - spl_autoload_unregister($classLoader); + throw $e; + } finally { + spl_autoload_unregister($classLoader); + } } }