Skip to content

Commit a3e83d7

Browse files
committed
Added plugin support for TagHandler
1 parent ca6728e commit a3e83d7

File tree

6 files changed

+80
-52
lines changed

6 files changed

+80
-52
lines changed

spec/DependencyInjection/Compiler/KernelPassSpec.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ function it_disables_the_kernels_httpcache_services(ContainerBuilder $container,
5252
]
5353
])->shouldBeCalled();
5454

55-
$container->getParameter('purge_type')->shouldBeCalled();
56-
5755
$this->process($container);
5856
}
5957
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

src/DependencyInjection/Compiler/KernelPass.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
99
use Symfony\Component\DependencyInjection\ContainerBuilder;
10-
use Symfony\Component\DependencyInjection\Reference;
11-
use EzSystems\PlatformHttpCacheBundle\Handler\TagHandler;
1210

1311
/**
1412
* Disables some of the http-cache services declared by the kernel so that
@@ -43,14 +41,6 @@ public function process(ContainerBuilder $container)
4341
return true;
4442
}));
4543
$container->getDefinition('cache_clearer')->setArguments($arguments);
46-
47-
$purgeType = $container->getParameter('purge_type');
48-
if ($purgeType === 'http') {
49-
// Injecting our own Tag handler
50-
$definition = $container->getDefinition('fos_http_cache.handler.tag_handler');
51-
$definition->setClass(TagHandler::class);
52-
$definition->addArgument(new Reference('ezplatform.http_cache.purge_client.fos'));
53-
}
5444
}
5545

5646
/**

src/DependencyInjection/Compiler/PurgeTypeSettingsPass.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/EzSystemsPlatformHttpCacheBundle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use EzSystems\PlatformHttpCacheBundle\DependencyInjection\Compiler\ResponseTaggersPass;
66
use EzSystems\PlatformHttpCacheBundle\DependencyInjection\Compiler\KernelPass;
7-
use EzSystems\PlatformHttpCacheBundle\DependencyInjection\Compiler\PurgeTypeSettingsPass;
7+
use EzSystems\PlatformHttpCacheBundle\DependencyInjection\Compiler\DriverPass;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use Symfony\Component\HttpKernel\Bundle\Bundle;
1010

@@ -16,6 +16,6 @@ public function build(ContainerBuilder $container)
1616

1717
$container->addCompilerPass(new ResponseTaggersPass());
1818
$container->addCompilerPass(new KernelPass());
19-
$container->addCompilerPass(new PurgeTypeSettingsPass());
19+
$container->addCompilerPass(new DriverPass());
2020
}
2121
}

src/Resources/config/services.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ services:
1515
arguments: ["@ezplatform.http_cache.cache_manager"]
1616
tags:
1717
- {name: ezplatform.http_cache.purge_client, purge_type: http}
18+
- {name: ezplatform.http_cache.purge_client, purge_type: varnish}
1819

1920
ezplatform.http_cache.purge_client.local:
2021
class: EzSystems\PlatformHttpCacheBundle\PurgeClient\LocalPurgeClient
@@ -30,9 +31,19 @@ services:
3031
arguments: ["%ezplatform.http_cache.store.root%"]
3132

3233
ezplatform.http_cache.tag_handler:
34+
alias: ezplatform.http_cache.tag_handler.xkey
35+
36+
ezplatform.http_cache.tag_handler.xkey:
3337
class: EzSystems\PlatformHttpCacheBundle\Handler\TagHandler
3438
arguments:
3539
- '@ezplatform.http_cache.cache_manager'
3640
- '%ezplatform.http_cache.tags.header%'
3741
- '@ezplatform.http_cache.purge_client'
42+
tags:
43+
# - {name: ezplatform.http_cache.tag_handler, purge_type: local}
44+
# - {name: ezplatform.http_cache.tag_handler, purge_type: http}
45+
# - {name: ezplatform.http_cache.tag_handler, purge_type: varnish}
46+
# - {name: ezplatform.http_cache.fos_tag_handler, purge_type: http}
47+
# - {name: ezplatform.http_cache.fos_tag_handler, purge_type: varnish}
48+
# - {name: ezplatform.http_cache.fos_tag_handler, purge_type: fastly}
3849

0 commit comments

Comments
 (0)