Skip to content

Commit

Permalink
Add PhpArrayAdapter to cache metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
ossinkine committed Aug 20, 2020
1 parent b74e72c commit 7813b6a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
39 changes: 39 additions & 0 deletions CacheWarmer/DoctrineMetadataCacheWarmer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\CacheWarmer;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\AbstractPhpFileCacheWarmer;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\DoctrineProvider;

class DoctrineMetadataCacheWarmer extends AbstractPhpFileCacheWarmer
{
/**
* @var EntityManagerInterface
*/
private $entityManager;

public function __construct(EntityManagerInterface $entityManager, string $phpArrayFile)
{
$this->entityManager = $entityManager;

parent::__construct($phpArrayFile);
}

/**
* @param string $cacheDir
*
* @return bool false if there is nothing to warm-up
*/
protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter)
{
$metadataFactory = new ClassMetadataFactory();
$metadataFactory->setEntityManager($this->entityManager);
$metadataFactory->setCacheDriver(new DoctrineProvider($arrayAdapter));
$metadataFactory->getAllMetadata();

return true;
}
}
37 changes: 37 additions & 0 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\CacheWarmer\DoctrineMetadataCacheWarmer;
use Doctrine\Bundle\DoctrineBundle\Dbal\RegexSchemaAssetFilter;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
Expand All @@ -17,6 +18,8 @@
use Symfony\Bridge\Doctrine\SchemaListener\PdoCacheAdapterDoctrineSchemaSubscriber;
use Symfony\Bridge\Doctrine\Validator\DoctrineLoader;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
Expand Down Expand Up @@ -778,6 +781,11 @@ protected function loadOrmCacheDrivers(array $entityManager, ContainerBuilder $c
$this->loadCacheDriver('metadata_cache', $entityManager['name'], $entityManager['metadata_cache_driver'], $container);
$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);
}
}

/**
Expand Down Expand Up @@ -898,4 +906,33 @@ 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 = $aliasId.'.php_array';
$doctrineCacheDefinition = $container->register($serviceId, DoctrineProvider::class);
$doctrineCacheDefinition->addArgument($phpArrayAdapterDefinition);
$container->setAlias($aliasId, $serviceId);
}

private function getPhpArrayFile(string $entityManagerName): string
{
return '%kernel.cache_dir%'.sprintf('/doctrine/orm/%s_metadata.php', $entityManagerName);
}
}

0 comments on commit 7813b6a

Please sign in to comment.