Skip to content

[FrameworkBundle] Make ValidatorCacheWarmer and SerializeCacheWarmer use kernel.build_dir instead of kernel.cache_dir #52981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ 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`
* Make `SerializeCacheWarmer` use `kernel.build_dir` instead of `cache_dir`

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}
Expand All @@ -79,15 +120,19 @@ 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) {
throw new \DomainException('This exception should be caught by the warmer.');
}
}, true, true);

$warmer->warmUp('foo');
$warmer->warmUp(\dirname($file), \dirname($file));
$this->assertFileExists($file);

spl_autoload_unregister($classLoader);
}
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
Expand All @@ -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);

Expand All @@ -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);
}
Expand All @@ -85,17 +132,22 @@ 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) {
throw new \DomainException('This exception should be caught by the warmer.');
}
}, true, true);

$warmer->warmUp('foo');
$warmer->warmUp(\dirname($file), \dirname($file));

$this->assertFileExists($file);

spl_autoload_unregister($classloader);
}
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
Loading