Skip to content

Commit

Permalink
Deprecate "metadata_cache_driver" configuration key
Browse files Browse the repository at this point in the history
Since metadata is not supposed to be discovered at runtime, the fallback
cache, which is the one configured through this key, is never supposed
to be used. There is no point in configuring it.
  • Loading branch information
ostrolucky authored and greg0ire committed Nov 4, 2020
1 parent 728a3b8 commit 3e2946e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
4 changes: 1 addition & 3 deletions CacheWarmer/DoctrineMetadataCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ public function __construct(EntityManagerInterface $entityManager, string $phpAr

/**
* @param string $cacheDir
*
* @return bool false if there is nothing to warm-up
*/
protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter): bool
{
$metadataFactory = new ClassMetadataFactory();
$metadataFactory->setEntityManager($this->entityManager);
Expand Down
7 changes: 7 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,13 @@ private function getOrmCacheDriverNode(string $name): ArrayNodeDefinition
->scalarNode('pool')->end()
->end();

if ($name === 'metadata_cache_driver') {
$node->setDeprecated(...$this->getDeprecationMsg(
'The "metadata_cache_driver" configuration key is deprecated. PHP Array cache is now automatically registered when %kernel.debug% is false.',
'2.2'
));
}

return $node;
}

Expand Down
54 changes: 22 additions & 32 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,11 @@ protected function loadOrmCacheDrivers(array $entityManager, ContainerBuilder $c
$this->loadCacheDriver('result_cache', $entityManager['name'], $entityManager['result_cache_driver'], $container);
$this->loadCacheDriver('query_cache', $entityManager['name'], $entityManager['query_cache_driver'], $container);

if (! $container->getParameter('kernel.debug')) {
$this->registerMetadataPhpArrayCacheWarmer($entityManager['name'], $container);
$this->registerMetadataPhpArrayCache($entityManager['name'], $container);
if ($container->getParameter('kernel.debug')) {
return;
}

$this->registerMetadataPhpArrayCaching($entityManager['name'], $container);
}

/**
Expand Down Expand Up @@ -936,35 +937,24 @@ private function createArrayAdapterCachePool(ContainerBuilder $container, string
return $id;
}

private function registerMetadataPhpArrayCacheWarmer(string $entityManagerName, ContainerBuilder $container): void
{
$cacheWarmerDefinition = $container->register(sprintf('doctrine.orm.%s_metadata_cache.php_array_warmer', $entityManagerName), DoctrineMetadataCacheWarmer::class);
$cacheWarmerDefinition->setArguments([
new Reference(sprintf('doctrine.orm.%s_entity_manager', $entityManagerName)),
$this->getPhpArrayFile($entityManagerName),
]);
$cacheWarmerDefinition->addTag('kernel.cache_warmer');
}

private function registerMetadataPhpArrayCache(string $entityManagerName, ContainerBuilder $container): void
{
$aliasId = sprintf('doctrine.orm.%s_metadata_cache', $entityManagerName);
$serviceId = (string) $container->getAlias($aliasId);

$phpArrayAdapterDefinition = new Definition(PhpArrayAdapter::class, [
$this->getPhpArrayFile($entityManagerName),
new Definition(DoctrineAdapter::class, [new Reference($serviceId)]),
]);

$serviceId .= '.php_array';

$doctrineCacheDefinition = $container->register($serviceId, DoctrineProvider::class);
$doctrineCacheDefinition->addArgument($phpArrayAdapterDefinition);
$container->setAlias($aliasId, $serviceId);
}

private function getPhpArrayFile(string $entityManagerName): string
private function registerMetadataPhpArrayCaching(string $entityManagerName, ContainerBuilder $container): void
{
return '%kernel.cache_dir%' . sprintf('/doctrine/orm/%s_metadata.php', $entityManagerName);
$metadataCacheAlias = $this->getObjectManagerElementName($entityManagerName . '_metadata_cache');
$decoratedMetadataCacheServiceId = (string) $container->getAlias($metadataCacheAlias);
$phpArrayCacheDecoratorServiceId = $decoratedMetadataCacheServiceId . '.php_array';
$phpArrayFile = '%kernel.cache_dir%' . sprintf('/doctrine/orm/%s_metadata.php', $entityManagerName);

$container->register(DoctrineMetadataCacheWarmer::class)
->setArguments([new Reference(sprintf('doctrine.orm.%s_entity_manager', $entityManagerName)), $phpArrayFile])
->addTag('kernel.cache_warmer');

$container->setAlias($metadataCacheAlias, $phpArrayCacheDecoratorServiceId);
$container->register($phpArrayCacheDecoratorServiceId, DoctrineProvider::class)
->addArgument(
new Definition(PhpArrayAdapter::class, [
$phpArrayFile,
new Definition(DoctrineAdapter::class, [new Reference($decoratedMetadataCacheServiceId)]),
])
);
}
}
46 changes: 31 additions & 15 deletions Tests/DependencyInjection/DoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,9 @@ public function testMessengerIntegration(): void
}
}

/**
* @group legacy
*/
public function testInvalidCacheConfiguration(): void
{
if (! interface_exists(EntityManagerInterface::class)) {
Expand Down Expand Up @@ -812,7 +815,16 @@ public function testCacheConfiguration(string $expectedAliasName, string $expect
$this->assertEquals($expectedAliasTarget, (string) $alias);
}

public static function cacheConfigurationProvider(): array
/**
* @dataProvider legacyCacheConfigurationProvider
* @group legacy
*/
public function testLegacyCacheConfiguration(string $expectedAliasName, string $expectedAliasTarget, string $cacheName, array $cacheConfig): void
{
$this->testCacheConfiguration($expectedAliasName, $expectedAliasTarget, $cacheName, $cacheConfig);
}

public static function legacyCacheConfigurationProvider(): array
{
return [
'metadata_cache_default' => [
Expand All @@ -821,6 +833,24 @@ public static function cacheConfigurationProvider(): array
'cacheName' => 'metadata_cache_driver',
'cacheConfig' => ['type' => null],
],
'metadata_cache_pool' => [
'expectedAliasName' => 'doctrine.orm.default_metadata_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.provider.metadata_cache_pool.php_array',
'cacheName' => 'metadata_cache_driver',
'cacheConfig' => ['type' => 'pool', 'pool' => 'metadata_cache_pool'],
],
'metadata_cache_service' => [
'expectedAliasName' => 'doctrine.orm.default_metadata_cache',
'expectedAliasTarget' => 'service_target_metadata.php_array',
'cacheName' => 'metadata_cache_driver',
'cacheConfig' => ['type' => 'service', 'id' => 'service_target_metadata'],
],
];
}

public static function cacheConfigurationProvider(): array
{
return [
'query_cache_default' => [
'expectedAliasName' => 'doctrine.orm.default_query_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.provider.cache.doctrine.orm.default.query',
Expand All @@ -833,13 +863,6 @@ public static function cacheConfigurationProvider(): array
'cacheName' => 'result_cache_driver',
'cacheConfig' => ['type' => null],
],

'metadata_cache_pool' => [
'expectedAliasName' => 'doctrine.orm.default_metadata_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.provider.metadata_cache_pool.php_array',
'cacheName' => 'metadata_cache_driver',
'cacheConfig' => ['type' => 'pool', 'pool' => 'metadata_cache_pool'],
],
'query_cache_pool' => [
'expectedAliasName' => 'doctrine.orm.default_query_cache',
'expectedAliasTarget' => 'doctrine.orm.cache.provider.query_cache_pool',
Expand All @@ -852,13 +875,6 @@ public static function cacheConfigurationProvider(): array
'cacheName' => 'result_cache_driver',
'cacheConfig' => ['type' => 'pool', 'pool' => 'result_cache_pool'],
],

'metadata_cache_service' => [
'expectedAliasName' => 'doctrine.orm.default_metadata_cache',
'expectedAliasTarget' => 'service_target_metadata.php_array',
'cacheName' => 'metadata_cache_driver',
'cacheConfig' => ['type' => 'service', 'id' => 'service_target_metadata'],
],
'query_cache_service' => [
'expectedAliasName' => 'doctrine.orm.default_query_cache',
'expectedAliasTarget' => 'service_target_query',
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-2.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Configuration
* `doctrine.dbal.keep_slave`. Use `doctrine.dbal.keep_replica`

Similarly, if you use XML configuration, please replace `<slave>` with `<replica>`.
* The `metadata_cache_driver` configuration key has been deprecated. PHP Array cache is now automatically registered when `%kernel.debug%` is false.

0 comments on commit 3e2946e

Please sign in to comment.