Skip to content

Commit d269c3f

Browse files
dbuddeboer
authored andcommitted
load purge and refresh handlers by default
1 parent 227ac2d commit d269c3f

File tree

6 files changed

+71
-7
lines changed

6 files changed

+71
-7
lines changed

Resources/doc/features/invalidation.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Invalidation
44
**Works with**:
55

66
* :ref:`Varnish <foshttpcache:varnish configuration>`
7-
* :ref:`Nginx <foshttpcache:nginx configuration>`
7+
* :ref:`Nginx <foshttpcache:nginx configuration>` (except regular expressions)
8+
* :doc:`symfony-http-cache` (except regular expressions)
89

910
**Preparation**:
1011

Resources/doc/features/symfony-http-cache.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ concept is to use event subscribers on the HttpCache class.
1515

1616
Symfony HttpCache support is currently limited to following features:
1717

18+
* Purge
19+
* Refresh
1820
* User context
1921

2022
Extending the correct HttpCache

Resources/doc/features/tagging.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Tagging
22
=======
33

4-
**Works with**: :ref:`Varnish <foshttpcache:varnish_tagging>`
4+
**Works with**:
5+
6+
* :ref:`Varnish <foshttpcache:varnish_tagging>`
57

68
If your application has many intricate relationships between cached items,
79
which makes it complex to invalidate them by route, cache tagging will be

Resources/doc/features/user-context.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ User Context
33

44
**Works with**:
55

6-
- :ref:`Varnish <foshttpcache:varnish user context>`
7-
- :doc:`symfony-http-cache`
6+
* :ref:`Varnish <foshttpcache:varnish user context>`
7+
* :doc:`symfony-http-cache`
88

99
If your application serves different content depending on the user's group
1010
or context (guest, editor, admin), you can cache that content per user context.

SymfonyCache/EventDispatchingHttpCache.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use FOS\HttpCache\SymfonyCache\CacheEvent;
1515
use FOS\HttpCache\SymfonyCache\Events;
16+
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
17+
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
1618
use FOS\HttpCache\SymfonyCache\UserContextSubscriber;
1719
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
1820
use Symfony\Component\EventDispatcher\EventDispatcher;
@@ -37,7 +39,12 @@
3739
* - `self::SUBSCRIBER_NONE` (**no** native subscriber).
3840
* - `self::SUBSCRIBER_ALL | ~self::SUBSCRIBER_USER_CONTEXT` (**all** native ones **except** the user context one).
3941
*
42+
* Note: This class looks very similar to the FOSHttpCache library event
43+
* dispatching kernel, but extends the FrameworkBundle HttpCache instead of the
44+
* one from the HttpKernel component.
45+
*
4046
* @author Jérôme Vieilledent <lolautruche@gmail.com> (courtesy of eZ Systems AS)
47+
* @author David Buchmann <mail@davidbu.ch>
4148
*
4249
* {@inheritdoc}
4350
*/
@@ -64,6 +71,20 @@ abstract class EventDispatchingHttpCache extends HttpCache
6471
*/
6572
const SUBSCRIBER_USER_CONTEXT = 1;
6673

74+
/**
75+
* Option for the "fos_default_subscribers" that enables the purge subscriber.
76+
*
77+
* See class phpdoc for examples.
78+
*/
79+
const SUBSCRIBER_PURGE = 2;
80+
81+
/**
82+
* Option for the "fos_default_subscribers" that enables the refresh subscriber.
83+
*
84+
* See class phpdoc for examples.
85+
*/
86+
const SUBSCRIBER_REFRESH = 4;
87+
6788
/**
6889
* @var EventDispatcherInterface
6990
*/
@@ -102,7 +123,8 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
102123
}
103124

104125
/**
105-
* Add subscriber
126+
* Add a cache kernel event subscriber that listens to events listed in
127+
* FOS\HttpCache\SymfonyCache\Event
106128
*
107129
* @param EventSubscriberInterface $subscriber
108130
*/
@@ -111,6 +133,34 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
111133
$this->getEventDispatcher()->addSubscriber($subscriber);
112134
}
113135

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+
146+
/**
147+
* {@inheritDoc}
148+
*
149+
* Adding the Events::PRE_INVALIDATE event.
150+
*/
151+
protected function invalidate(Request $request, $catch = false)
152+
{
153+
if ($this->getEventDispatcher()->hasListeners(Events::PRE_INVALIDATE)) {
154+
$event = new CacheEvent($this, $request);
155+
$this->getEventDispatcher()->dispatch(Events::PRE_INVALIDATE, $event);
156+
if ($event->getResponse()) {
157+
return $event->getResponse();
158+
}
159+
}
160+
161+
return parent::invalidate($request, $catch);
162+
}
163+
114164
/**
115165
* Return the subscribers to be added to the event dispatcher, according to the
116166
* fos_default_subscribers option in `getOptions()`.
@@ -127,6 +177,12 @@ protected function getDefaultSubscribers()
127177
if ($defaultSubscribersOption & self::SUBSCRIBER_USER_CONTEXT) {
128178
$subscribers[] = new UserContextSubscriber();
129179
}
180+
if ($defaultSubscribersOption & self::SUBSCRIBER_PURGE) {
181+
$subscribers[] = new PurgeSubscriber();
182+
}
183+
if ($defaultSubscribersOption & self::SUBSCRIBER_REFRESH) {
184+
$subscribers[] = new RefreshSubscriber();
185+
}
130186

131187
return $subscribers;
132188
}

Tests/Unit/SymfonyCache/EventDispatchingHttpCacheTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace FOS\HttpCacheBundle\Tests\Unit\SymfonyCache;
1313

14+
use FOS\HttpCache\SymfonyCache\PurgeSubscriber;
15+
use FOS\HttpCache\SymfonyCache\RefreshSubscriber;
1416
use FOS\HttpCacheBundle\SymfonyCache\EventDispatchingHttpCache;
1517
use FOS\HttpCache\SymfonyCache\CacheEvent;
1618
use FOS\HttpCache\SymfonyCache\Events;
@@ -119,13 +121,14 @@ public function testConfiguredSubscribers(array $options, array $expectedSubscri
119121

120122
public function configuredSubscribersProvider()
121123
{
124+
$all = array(new UserContextSubscriber(), new PurgeSubscriber(), new RefreshSubscriber());
122125
return array(
123-
array(array(), array(new UserContextSubscriber())),
126+
array(array(), $all),
124127
array(
125128
array(
126129
'fos_default_subscribers' => EventDispatchingHttpCache::SUBSCRIBER_ALL,
127130
),
128-
array(new UserContextSubscriber())
131+
$all
129132
),
130133
array(
131134
array(

0 commit comments

Comments
 (0)