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

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