Skip to content

Commit 54c859e

Browse files
authored
Merge pull request #4 from ezsystems/ezp26982-fix_kernel_integration
Fix kernel integration
2 parents 34e43e7 + 94cf5c2 commit 54c859e

File tree

5 files changed

+110
-9
lines changed

5 files changed

+110
-9
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ Add the package to `app/AppKernel.php`, *before* the EzPublishCoreBundle declara
2424

2525
The package will replace the services from the kernel, thus enabling the new features, such as multi-tagging.
2626

27+
The application cache class needs to be customized. If you haven't changed the `AppCache` class, you can do so
28+
by setting the `SYMFONY_HTTP_CACHE_CLASS` environment variable for your PHP or web server user.
29+
If you use your own `AppCache` class, you will have to make it to extend from this class instead
30+
of from the CoreBundle's.
31+
32+
For PHP's internal server:
33+
34+
export SYMFONY_HTTP_CACHE_CLASS='EzSystems\PlatformHttpCacheBundle\AppCache'
35+
36+
For Apache, with the default eZ Platform virtual host definition, uncomment the `SetEnv` lines for the two
37+
variables above in your virtualhost, and set the values accordingly:
38+
39+
SetEnv SYMFONY_HTTP_CACHE_CLASS='EzSystems\PlatformHttpCacheBundle\AppCache'
40+
41+
For Nginx, set the variables using `fastcgi_param`:
42+
43+
fastcgi_param SYMFONY_HTTP_CACHE "1";
44+
45+
Do not forget to restart your web server.
46+
2747
## Features
2848

2949
### `xkey` header on ContentView responses
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace spec\EzSystems\PlatformHttpCacheBundle\DependencyInjection\Compiler;
4+
5+
use EzSystems\PlatformHttpCacheBundle\DependencyInjection\Compiler\KernelPass;
6+
use PhpSpec\ObjectBehavior;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Definition;
9+
10+
class KernelPassSpec extends ObjectBehavior
11+
{
12+
function let(ContainerBuilder $container)
13+
{
14+
$container->getDefinitions()->willReturn([]);
15+
$container->getAlias('ezpublish.http_cache.purge_client')->willReturn('some_random_id');
16+
}
17+
18+
function it_is_initializable()
19+
{
20+
$this->shouldHaveType(KernelPass::class);
21+
}
22+
23+
function it_disables_the_kernels_httpcache_slots(ContainerBuilder $container)
24+
{
25+
$container->getDefinitions()->willReturn([
26+
'ezpublish.http_cache.witness_service' => new Definition(),
27+
'ezpublish.http_cache.signalslot.some_slot' => new Definition(),
28+
'ezpublish.http_cache.signalslot.some_other_slot' => new Definition(),
29+
'witness_service' => new Definition(),
30+
]);
31+
$container->removeDefinition('ezpublish.http_cache.signalslot.some_slot')->shouldBeCalled();
32+
$container->removeDefinition('ezpublish.http_cache.signalslot.some_other_slot')->shouldBeCalled();
33+
34+
$this->process($container);
35+
}
36+
37+
function it_disables_the_kernels_smartcache_event_listeners(ContainerBuilder $container)
38+
{
39+
$container->getDefinitions()->willReturn([
40+
'ezpublish.cache_clear.content.some_listener' => new Definition(),
41+
'witness_service' => new Definition(),
42+
]);
43+
$container->removeDefinition('ezpublish.cache_clear.content.some_listener')->shouldBeCalled();
44+
45+
$this->process($container);
46+
}
47+
48+
function it_disables_the_kernels_view_cache_response_listener(ContainerBuilder $container)
49+
{
50+
$container->getDefinitions()->willReturn([
51+
'ezpublish.view.cache_response_listener' => new Definition(),
52+
'witness_service' => new Definition(),
53+
]);
54+
$container->removeDefinition('ezpublish.view.cache_response_listener')->shouldBeCalled();
55+
56+
$this->process($container);
57+
}
58+
}

src/AppCache.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
4+
* @license For full copyright and license information view LICENSE file distributed with this source code.
5+
*/
6+
7+
namespace EzSystems\PlatformHttpCacheBundle;
8+
9+
use eZ\Bundle\EzPublishCoreBundle\HttpCache;
10+
use EzSystems\PlatformHttpCacheBundle\Proxy\TagAwareStore;
11+
12+
/**
13+
* Custom AppCache.
14+
*
15+
* Enable by setting SYMFONY_HTTP_CACHE_CLASS to 'EzSystems\PlatformHttpCacheBundle\AppCache'
16+
*/
17+
class AppCache extends HttpCache
18+
{
19+
protected function createStore()
20+
{
21+
return new TagAwareStore($this->cacheDir ?: $this->kernel->getCacheDir() . '/http_cache');
22+
}
23+
}

src/DependencyInjection/Compiler/KernelPass.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010
use Symfony\Component\DependencyInjection\ContainerBuilder;
1111

1212
/**
13-
*
13+
* Disables some of the http-cache services declared by the kernel so that
14+
* they can be replaced with this bundle's.
1415
*/
1516
class KernelPass implements CompilerPassInterface
1617
{
1718
public function process(ContainerBuilder $container)
1819
{
19-
// slots
2020
foreach ($container->getDefinitions() as $id => $definition) {
2121
if ($this->isSignalSlot($id) ||
2222
$this->isSmartCacheListener($id) ||
23-
$this->isCacheTagListener($id)
23+
$this->isResponseCacheListener($id)
2424
) {
2525
$container->removeDefinition($id);
2626
}
@@ -44,15 +44,15 @@ protected function isSignalSlot($id)
4444
*/
4545
protected function isSmartCacheListener($id)
4646
{
47-
return preg_match('/ezpublish.cache_clear.content.[a-z_]]_listener/', $id);
47+
return preg_match('/^ezpublish\.cache_clear\.content.[a-z_]+_listener/', $id);
4848
}
4949

5050
/**
5151
* @param string $id
5252
*
5353
* @return bool
5454
*/
55-
protected function isCacheTagListener($id)
55+
protected function isResponseCacheListener($id)
5656
{
5757
return $id === 'ezpublish.view.cache_response_listener';
5858
}

src/Proxy/TagAwareStore.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private function saveTag($tag, $digest)
123123

124124
/**
125125
* Purges data from $request.
126-
* If xkey or X-Location-Id (deprecated) header is present, the store will purge cache for given locationId or group of locationIds.
126+
* If key or X-Location-Id (deprecated) header is present, the store will purge cache for given locationId or group of locationIds.
127127
* If not, regular purge by URI will occur.
128128
*
129129
* @param \Symfony\Component\HttpFoundation\Request $request
@@ -132,7 +132,7 @@ private function saveTag($tag, $digest)
132132
*/
133133
public function purgeByRequest(Request $request)
134134
{
135-
if (!$request->headers->has('X-Location-Id') && !$request->headers->has('xkey')) {
135+
if (!$request->headers->has('X-Location-Id') && !$request->headers->has('key')) {
136136
return $this->purge($request->getUri());
137137
}
138138

@@ -142,8 +142,8 @@ public function purgeByRequest(Request $request)
142142
return $this->purgeAllContent();
143143
}
144144

145-
if ($request->headers->has('xkey')) {
146-
$tags = explode(' ', $request->headers->get('xkey'));
145+
if ($request->headers->has('key')) {
146+
$tags = explode(' ', $request->headers->get('key'));
147147
} elseif ($locationId[0] === '(' && substr($locationId, -1) === ')') {
148148
// Deprecated: (123|456|789) => Purge for #123, #456 and #789 location IDs.
149149
$tags = array_map(

0 commit comments

Comments
 (0)