vendor/doctrine/doctrine-bundle/DependencyInjection/Compiler/CacheCompatibilityPass.php line 116

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
  3. use Doctrine\Common\Cache\Psr6\CacheAdapter;
  4. use Psr\Cache\CacheItemPoolInterface;
  5. use Symfony\Component\DependencyInjection\ChildDefinition;
  6. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  7. use Symfony\Component\DependencyInjection\ContainerBuilder;
  8. use Symfony\Component\DependencyInjection\Definition;
  9. use Symfony\Component\DependencyInjection\Reference;
  10. use function array_keys;
  11. use function assert;
  12. use function in_array;
  13. use function is_a;
  14. use function trigger_deprecation;
  15. /** @internal  */
  16. final class CacheCompatibilityPass implements CompilerPassInterface
  17. {
  18.     private const CONFIGURATION_TAG          'doctrine.orm.configuration';
  19.     private const CACHE_METHODS_PSR6_SUPPORT = [
  20.         'setMetadataCache',
  21.         'setQueryCache',
  22.         'setResultCache',
  23.     ];
  24.     public function process(ContainerBuilder $container): void
  25.     {
  26.         foreach (array_keys($container->findTaggedServiceIds(self::CONFIGURATION_TAG)) as $id) {
  27.             foreach ($container->getDefinition($id)->getMethodCalls() as $methodCall) {
  28.                 if ($methodCall[0] === 'setSecondLevelCacheConfiguration') {
  29.                     $this->updateSecondLevelCache($container$methodCall[1][0]);
  30.                     continue;
  31.                 }
  32.                 if (! in_array($methodCall[0], self::CACHE_METHODS_PSR6_SUPPORTtrue)) {
  33.                     continue;
  34.                 }
  35.                 $aliasId      = (string) $methodCall[1][0];
  36.                 $definitionId = (string) $container->getAlias($aliasId);
  37.                 $this->wrapIfNecessary($container$aliasId$definitionId);
  38.             }
  39.         }
  40.     }
  41.     private function updateSecondLevelCache(ContainerBuilder $containerDefinition $slcConfigDefinition): void
  42.     {
  43.         foreach ($slcConfigDefinition->getMethodCalls() as $methodCall) {
  44.             if ($methodCall[0] !== 'setCacheFactory') {
  45.                 continue;
  46.             }
  47.             $factoryDefinition $methodCall[1][0];
  48.             assert($factoryDefinition instanceof Definition);
  49.             $aliasId = (string) $factoryDefinition->getArgument(1);
  50.             $this->wrapIfNecessary($container$aliasId, (string) $container->getAlias($aliasId));
  51.             foreach ($factoryDefinition->getMethodCalls() as $factoryMethodCall) {
  52.                 if ($factoryMethodCall[0] !== 'setRegion') {
  53.                     continue;
  54.                 }
  55.                 $regionDefinition $container->getDefinition($factoryMethodCall[1][0]);
  56.                 // Get inner service for FileLock
  57.                 if ($regionDefinition->getClass() === '%doctrine.orm.second_level_cache.filelock_region.class%') {
  58.                     $regionDefinition $container->getDefinition($regionDefinition->getArgument(0));
  59.                 }
  60.                 // We don't know how to adjust custom region classes
  61.                 if ($regionDefinition->getClass() !== '%doctrine.orm.second_level_cache.default_region.class%') {
  62.                     continue;
  63.                 }
  64.                 $driverId = (string) $regionDefinition->getArgument(1);
  65.                 if (! $container->hasAlias($driverId)) {
  66.                     continue;
  67.                 }
  68.                 $this->wrapIfNecessary($container$driverId, (string) $container->getAlias($driverId));
  69.             }
  70.             break;
  71.         }
  72.     }
  73.     private function createCompatibilityLayerDefinition(ContainerBuilder $containerstring $definitionId): ?Definition
  74.     {
  75.         $definition $container->getDefinition($definitionId);
  76.         while (! $definition->getClass() && $definition instanceof ChildDefinition) {
  77.             $definition $container->findDefinition($definition->getParent());
  78.         }
  79.         if (is_a($definition->getClass(), CacheItemPoolInterface::class, true)) {
  80.             return null;
  81.         }
  82.         trigger_deprecation(
  83.             'doctrine/doctrine-bundle',
  84.             '2.4',
  85.             'Configuring doctrine/cache is deprecated. Please update the cache service "%s" to use a PSR-6 cache.',
  86.             $definitionId
  87.         );
  88.         return (new Definition(CacheItemPoolInterface::class))
  89.             ->setFactory([CacheAdapter::class, 'wrap'])
  90.             ->addArgument(new Reference($definitionId));
  91.     }
  92.     private function wrapIfNecessary(ContainerBuilder $containerstring $aliasIdstring $definitionId): void
  93.     {
  94.         $compatibilityLayer $this->createCompatibilityLayerDefinition($container$definitionId);
  95.         if ($compatibilityLayer === null) {
  96.             return;
  97.         }
  98.         $compatibilityLayerId $definitionId '.compatibility_layer';
  99.         $container->setAlias($aliasId$compatibilityLayerId);
  100.         $container->setDefinition($compatibilityLayerId$compatibilityLayer);
  101.     }
  102. }