Skip to content

Commit f9f98e4

Browse files
committed
Merge pull request #169 from FriendsOfSymfony/cleanup-invalidator-events
cleanup usage of event dispatcher with CacheInvalidator
2 parents cdf7c49 + e2414fc commit f9f98e4

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
* **2015-02-11** It is no longer possible to change the event dispatcher of the
5+
CacheInvalidator once its instantiated. If you need a custom dispatcher, set it
6+
right after creating the invalidator instance. Deprecated CacheInvalidator::addSubscriber
7+
in favor of either using the event dispatcher instance you inject or do
8+
getEventDispatcher()->addSubscriber($subscriber)
9+
410
1.2.0
511
-----
612

doc/cache-invalidator.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Then add the logger as a subscriber to the cache invalidator::
197197
use FOS\HttpCache\EventListener\LogSubscriber;
198198

199199
$subscriber = new LogSubscriber($monolog);
200-
$cacheInvalidator->addSubscriber($subscriber);
200+
$cacheInvalidator->getEventDispatcher()->addSubscriber($subscriber);
201201

202202
Now, if you flush the invalidator, errors will be logged::
203203

src/CacheInvalidator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public function supports($operation)
118118
*/
119119
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
120120
{
121+
if ($this->eventDispatcher) {
122+
// if you want to set a custom event dispatcher, do so right after instantiating
123+
// the invalidator.
124+
throw new \Exception('You may not change the event dispatcher once it is set.');
125+
}
121126
$this->eventDispatcher = $eventDispatcher;
122127
}
123128

@@ -141,6 +146,8 @@ public function getEventDispatcher()
141146
* @param EventSubscriberInterface $subscriber
142147
*
143148
* @return $this
149+
*
150+
* @deprecated Use getEventDispatcher()->addSubscriber($subscriber) instead.
144151
*/
145152
public function addSubscriber(EventSubscriberInterface $subscriber)
146153
{

tests/Unit/CacheInvalidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public function testProxyClientExceptionsAreLogged()
209209
->with('critical', '403 error response "Forbidden" from caching proxy at http://127.0.0.1', array('exception' => $responseException))
210210
->getMock();
211211

212-
$cacheInvalidator->addSubscriber(new LogSubscriber($logger));
212+
$cacheInvalidator->getEventDispatcher()->addSubscriber(new LogSubscriber($logger));
213213

214214
$cacheInvalidator
215215
->flush()

0 commit comments

Comments
 (0)