Skip to content

Commit

Permalink
Deprecate "metadata_cache_driver" configuration key
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky committed Oct 31, 2020
1 parent bb74383 commit c925eed
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 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
8 changes: 7 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,12 @@ 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.'
));
}

return $node;
}

Expand Down Expand Up @@ -735,7 +741,7 @@ private function getDeprecationMsg(string $message): array
if (method_exists(BaseNode::class, 'getDeprecation')) {
return [
'doctrine/doctrine-bundle',
'2.0',
'2.2',
$message,
];
}
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)]),
])
);
}
}
4 changes: 4 additions & 0 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 All @@ -790,6 +793,7 @@ public function testInvalidCacheConfiguration(): void
* @param array|string $cacheConfig
*
* @dataProvider cacheConfigurationProvider
* @group legacy
*/
public function testCacheConfiguration(string $expectedAliasName, string $expectedAliasTarget, string $cacheName, $cacheConfig): void
{
Expand Down

0 comments on commit c925eed

Please sign in to comment.