Skip to content

cleanup usage of event dispatcher with CacheInvalidator #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

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

1.2.0
-----

Expand Down
2 changes: 1 addition & 1 deletion doc/cache-invalidator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Then add the logger as a subscriber to the cache invalidator::
use FOS\HttpCache\EventListener\LogSubscriber;

$subscriber = new LogSubscriber($monolog);
$cacheInvalidator->addSubscriber($subscriber);
$cacheInvalidator->getEventDispatcher()->addSubscriber($subscriber);

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

Expand Down
7 changes: 7 additions & 0 deletions src/CacheInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public function supports($operation)
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
if ($this->eventDispatcher) {
// if you want to set a custom event dispatcher, do so right after instantiating
// the invalidator.
throw new \Exception('You may not change the event dispatcher once it is set.');
}
$this->eventDispatcher = $eventDispatcher;
}

Expand All @@ -141,6 +146,8 @@ public function getEventDispatcher()
* @param EventSubscriberInterface $subscriber
*
* @return $this
*
* @deprecated Use getEventDispatcher()->addSubscriber($subscriber) instead.
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/CacheInvalidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function testProxyClientExceptionsAreLogged()
->with('critical', '403 error response "Forbidden" from caching proxy at http://127.0.0.1', array('exception' => $responseException))
->getMock();

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

$cacheInvalidator
->flush()
Expand Down