Skip to content

Commit 211c59b

Browse files
committed
cleanup cache setup
1 parent b56f545 commit 211c59b

File tree

9 files changed

+82
-29
lines changed

9 files changed

+82
-29
lines changed

doc/symfony-cache-configuration.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ Instead of extending ``Symfony\Component\HttpKernel\HttpCache\HttpCache``, your
3232

3333
If your class already needs to extend a different class, simply copy the
3434
event handling code from the EventDispatchingHttpCache into your
35-
``AppCache`` class. The drawback is that you need to manually check whether
36-
you need to adjust your ``AppCache`` each time you update the FOSHttpCache
37-
library.
35+
``AppCache`` class and make it implement ``CacheInvalidationInterface``.
36+
The drawback is that you need to manually check whether you need to adjust
37+
your ``AppCache`` each time you update the FOSHttpCache library.
3838

3939
Now that you have an event dispatching kernel, you can make it register the
4040
subscribers you need. While you could do that from your bootstrap code, this is
@@ -168,7 +168,7 @@ options through the constructor:
168168

169169
With Apache, you can do this for example in a ``.htaccess`` file::
170170

171-
RewriteEngine On
171+
RewriteEngine On
172172
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
173173

174174
Cleaning the Cookie Header

src/SymfonyCache/CacheEvent.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class CacheEvent extends Event
2525
{
2626
/**
27-
* @var HttpCache
27+
* @var CacheInvalidationInterface
2828
*/
2929
private $kernel;
3030

@@ -39,19 +39,25 @@ class CacheEvent extends Event
3939
private $response;
4040

4141
/**
42-
* @param HttpCache $kernel The kernel raising with this event.
43-
* @param Request $request The request being processed.
42+
* Make sure your $kernel implements CacheInvalidationInterface. Creating the event with other
43+
* HttpCache classes is deprecated and will no longer be supported in version 2 of this library.
44+
*
45+
* @param CacheInvalidationInterface|HttpCache $kernel The kernel raising with this event.
46+
* @param Request $request The request being processed.
4447
*/
45-
public function __construct(HttpCache $kernel, Request $request)
48+
public function __construct($kernel, Request $request)
4649
{
50+
if (!($kernel instanceof CacheInvalidationInterface || $kernel instanceof HttpCache)) {
51+
throw new \InvalidArgumentException('Expected a CacheInvalidationInterface or HttpCache');
52+
}
4753
$this->kernel = $kernel;
4854
$this->request = $request;
4955
}
5056

5157
/**
5258
* Get the cache kernel that raised this event.
5359
*
54-
* @return HttpCache
60+
* @return CacheInvalidationInterface
5561
*/
5662
public function getKernel()
5763
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\SymfonyCache;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
19+
/**
20+
* Interface for a HttpCache that supports active cache invalidation.
21+
*/
22+
interface CacheInvalidationInterface extends HttpKernelInterface
23+
{
24+
/**
25+
* Forwards the Request to the backend and determines whether the response should be stored.
26+
*
27+
* This methods is triggered when the cache missed or a reload is required.
28+
*
29+
* This method is present on HttpCache but must be public to allow event subscribers to do
30+
* refresh operations.
31+
*
32+
* @param Request $request A Request instance
33+
* @param bool $catch Whether to process exceptions
34+
*
35+
* @return Response A Response instance
36+
*/
37+
public function fetch(Request $request, $catch = false);
38+
39+
/**
40+
* Gets the store for cached responses.
41+
*
42+
* @return StoreInterface $store The store used by the HttpCache
43+
*/
44+
public function getStore();
45+
}

src/SymfonyCache/EventDispatchingHttpCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* {@inheritdoc}
3232
*/
33-
abstract class EventDispatchingHttpCache extends HttpCache
33+
abstract class EventDispatchingHttpCache extends HttpCache implements CacheInvalidationInterface
3434
{
3535
/**
3636
* @var EventDispatcherInterface

tests/Functional/CacheInvalidatorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\HttpCache\Tests\Functional;
1313

1414
use FOS\HttpCache\CacheInvalidator;
15+
use FOS\HttpCache\Handler\TagHandler;
1516
use FOS\HttpCache\Test\VarnishTestCase;
1617

1718
/**
@@ -20,6 +21,23 @@
2021
class CacheInvalidatorTest extends VarnishTestCase
2122
{
2223
public function testInvalidateTags()
24+
{
25+
$cacheInvalidator = new CacheInvalidator($this->getProxyClient());
26+
$tagHandler = new TagHandler($cacheInvalidator);
27+
28+
$this->assertMiss($this->getResponse('/tags.php'));
29+
$this->assertHit($this->getResponse('/tags.php'));
30+
31+
$tagHandler->invalidateTags(array('tag1'));
32+
$cacheInvalidator->flush();
33+
34+
$this->assertMiss($this->getResponse('/tags.php'));
35+
}
36+
37+
/**
38+
* Test the deprecated CacheInvalidator::invalidateTags method.
39+
*/
40+
public function testInvalidateTagsBC()
2341
{
2442
$cacheInvalidator = new CacheInvalidator($this->getProxyClient());
2543

File renamed without changes.

tests/Unit/SymfonyCache/PurgeSubscriberTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class PurgeSubscriberTest extends \PHPUnit_Framework_TestCase
2727
public function setUp()
2828
{
2929
$this->kernel = $this
30-
->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')
30+
->getMockBuilder('FOS\HttpCache\SymfonyCache\CacheInvalidationInterface')
3131
->disableOriginalConstructor()
3232
->getMock()
3333
;

tests/Unit/SymfonyCache/RefreshSubscriberTest.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RefreshSubscriberTest extends \PHPUnit_Framework_TestCase
2727

2828
public function setUp()
2929
{
30-
$this->kernel = $this->getMock('FOS\HttpCache\Tests\Unit\SymfonyCache\TestHttpCache');
30+
$this->kernel = $this->getMock('FOS\HttpCache\SymfonyCache\CacheInvalidationInterface');
3131
}
3232

3333
public function testRefreshAllowed()
@@ -127,19 +127,3 @@ public function testInvalidConfiguration()
127127
new RefreshSubscriber(array('purge_client_ip' => '1.2.3.4'));
128128
}
129129
}
130-
131-
class TestHttpCache extends HttpCache
132-
{
133-
public function __construct()
134-
{}
135-
136-
/**
137-
* Made public to allow event subscribers to do refresh operations.
138-
*
139-
* {@inheritDoc}
140-
*/
141-
public function fetch(Request $request, $catch = false)
142-
{
143-
return parent::fetch($request, $catch);
144-
}
145-
}

tests/Unit/SymfonyCache/UserContextSubscriberTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class UserContextSubscriberTest extends \PHPUnit_Framework_TestCase
2727
public function setUp()
2828
{
2929
$this->kernel = $this
30-
->getMockBuilder('Symfony\Component\HttpKernel\HttpCache\HttpCache')
30+
->getMockBuilder('FOS\HttpCache\SymfonyCache\CacheInvalidationInterface')
3131
->disableOriginalConstructor()
3232
->getMock()
3333
;

0 commit comments

Comments
 (0)