1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the HttpCachePass class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\MVC\Symfony\Cache\PurgeClientInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* HttpCache related compiler pass. |
19
|
|
|
* |
20
|
|
|
* Ensures Varnish proxy client is correctly configured. |
21
|
|
|
*/ |
22
|
|
|
class HttpCachePass implements CompilerPassInterface |
23
|
|
|
{ |
24
|
|
|
public function process(ContainerBuilder $container) |
25
|
|
|
{ |
26
|
|
|
$this->processCacheManager($container); |
27
|
|
|
$this->processPurgeClient($container); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function processCacheManager(ContainerBuilder $container) |
31
|
|
|
{ |
32
|
|
|
if (!$container->hasDefinition('ezpublish.http_cache.cache_manager')) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!$container->hasDefinition('fos_http_cache.proxy_client.varnish')) { |
37
|
|
|
throw new InvalidArgumentException('Varnish proxy client must be enabled in FOSHttpCacheBundle'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$varnishClientDef = $container->findDefinition('fos_http_cache.proxy_client.varnish'); |
41
|
|
|
$varnishClientDef->setFactory( |
42
|
|
|
[ |
43
|
|
|
new Reference('ezpublish.http_cache.proxy_client.varnish.factory'), |
44
|
|
|
'buildProxyClient', |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
// Set it lazy as it can be loaded during cache warming and factory depends on ConfigResolver while cache warming |
48
|
|
|
// occurs before SA matching. |
49
|
|
|
$varnishClientDef->setLazy(true); |
50
|
|
|
|
51
|
|
|
// Forcing cache manager to use Varnish proxy client, for BAN support. |
52
|
|
|
$cacheManagerDef = $container->findDefinition('ezpublish.http_cache.cache_manager'); |
53
|
|
|
$cacheManagerDef->replaceArgument(0, new Reference('fos_http_cache.proxy_client.varnish')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function processPurgeClient(ContainerBuilder $container) |
57
|
|
|
{ |
58
|
|
|
// Check that alias exists (if not it has been removed by another bundle) |
59
|
|
|
if (!$container->has('ezpublish.http_cache.purge_client')) { |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$purgeType = $container->getParameter('ezpublish.http_cache.purge_type'); |
64
|
|
|
switch ($purgeType) { |
65
|
|
|
case 'local': |
66
|
|
|
$purgeService = 'ezpublish.http_cache.purge_client.local'; |
67
|
|
|
break; |
68
|
|
|
case 'http': |
69
|
|
|
$purgeService = 'ezpublish.http_cache.purge_client.fos'; |
70
|
|
|
break; |
71
|
|
|
default: |
72
|
|
|
if (!$container->has($purgeType)) { |
73
|
|
|
throw new InvalidArgumentException("Invalid ezpublish.http_cache.purge_type. Can be 'local', 'http' or a valid service identifier implementing PurgeClientInterface."); |
74
|
|
|
} elseif (!$container->get($purgeType) instanceof PurgeClientInterface) { |
75
|
|
|
throw new InvalidArgumentException('Invalid ezpublish.http_cache.purge_type, it needs to implement PurgeClientInterface.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$purgeService = $purgeType; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$container->setAlias('ezpublish.http_cache.purge_client', $purgeService); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|