|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EzSystems\PlatformHttpCacheBundle\DependencyInjection\Compiler; |
| 4 | + |
| 5 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 6 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 7 | +use Symfony\Component\DependencyInjection\Reference; |
| 8 | +use EzSystems\PlatformHttpCacheBundle\Handler\TagHandler; |
| 9 | + |
| 10 | +class DriverPass implements CompilerPassInterface |
| 11 | +{ |
| 12 | + public function process(ContainerBuilder $container) |
| 13 | + { |
| 14 | + $purgeType = $container->getParameter('ezpublish.http_cache.purge_type'); |
| 15 | + $configuredPurgeClientServiceId = static::getTaggedService($container, 'ezplatform.http_cache.purge_client'); |
| 16 | + if ($configuredPurgeClientServiceId === null) { |
| 17 | + throw new \InvalidArgumentException("No driver found being able to handle purge_type '$purgeType'."); |
| 18 | + } |
| 19 | + $container->setAlias('ezplatform.http_cache.purge_client', $configuredPurgeClientServiceId); |
| 20 | + |
| 21 | + // TagHandler is responsible for setting correct tags on legacy responses with X-Location-Id headers |
| 22 | + $configuredTagHandlerServiceId = static::getTaggedService($container, 'ezplatform.http_cache.tag_handler'); |
| 23 | + if ($configuredTagHandlerServiceId !== null) { |
| 24 | + $container->setAlias('ezplatform.http_cache.tag_handler', $configuredTagHandlerServiceId); |
| 25 | + } |
| 26 | + |
| 27 | + // FOS TagHandler is making sure running "php app/console fos:httpcache:invalidate:tag <tag>" works |
| 28 | + $configuredFosTagHandlerServiceId = static::getTaggedService($container, 'ezplatform.http_cache.fos_tag_handler'); |
| 29 | + if ($configuredFosTagHandlerServiceId === null) { |
| 30 | + // We default to xkey handler. This one should anyway work for most drivers as it just passes a purge request |
| 31 | + // on to the purge client |
| 32 | + $configuredFosTagHandlerServiceId = 'ezplatform.http_cache.tag_handler.xkey'; |
| 33 | + } |
| 34 | + $fosTagHandlerDefinition = $container->getDefinition($configuredFosTagHandlerServiceId); |
| 35 | + $definition = $container->getDefinition('fos_http_cache.handler.tag_handler'); |
| 36 | + $definition->setClass($fosTagHandlerDefinition->getClass()); |
| 37 | + $definition->addArgument(new Reference('ezplatform.http_cache.purge_client')); |
| 38 | + } |
| 39 | + |
| 40 | + public static function getTaggedService(ContainerBuilder $container, $tag) |
| 41 | + { |
| 42 | + $purgeType = $container->getParameter('ezpublish.http_cache.purge_type'); |
| 43 | + $configuredTagHandlerServiceId = null; |
| 44 | + |
| 45 | + $tagHandlerServiceIds = $container->findTaggedServiceIds($tag); |
| 46 | + foreach ($tagHandlerServiceIds as $tagHandlerServiceId => $attributes) { |
| 47 | + $currentPurgeTypeId = null; |
| 48 | + $currentTagHandlerServiceId = null; |
| 49 | + foreach ($attributes as $attribute) { |
| 50 | + if (array_key_exists('purge_type', $attribute)) { |
| 51 | + $currentPurgeTypeId = $attribute['purge_type']; |
| 52 | + } |
| 53 | + if ($currentPurgeTypeId !== null) { |
| 54 | + if ($purgeType === $attribute['purge_type']) { |
| 55 | + $configuredTagHandlerServiceId = $tagHandlerServiceId; |
| 56 | + break 2; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + if ($currentPurgeTypeId === null) { |
| 61 | + throw new \InvalidArgumentException("Missing attribute 'purge_type' in tagged service '$tagHandlerServiceId'."); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return $configuredTagHandlerServiceId; |
| 66 | + } |
| 67 | +} |
0 commit comments