Skip to content

Commit ee23d44

Browse files
authored
Merge pull request #328 from FriendsOfSymfony/add-listener-method
also expose the addListener method of the event dispatcher to the HttpCache trait
2 parents da5dbb7 + 1109bc2 commit ee23d44

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpC
4747
* BC BREAK: Renamed all event listeners to XxListener instead of XxSubscriber.
4848
* BC BREAK: Constructors for PurgeListener and RefreshListener now use an
4949
options array for customization.
50+
* Provide a trait for the event dispatching kernel, instead of a base class.
51+
The trait offers both the addSubscriber and the addListener methods.
5052

5153
### Testing
5254

doc/symfony-cache-configuration.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ trait ``FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache``::
5050
}
5151
}
5252

53-
The trait is adding events before and/or after kernel methods to let the
54-
listeners interfere. If you need to overwrite core ``HttpCache`` functionality
55-
in your kernel, one option is to provide your own event listeners. If you need
56-
to implement functionality directly on the methods, be careful to always call
57-
the trait methods rather than going directly to the parent, or events will not
58-
be triggered anymore. You might also need to copy a method from the trait and
59-
add your own logic between the events to not be too early or too late for the
60-
event.
53+
The trait adds the ``addSubscriber`` and ``addListener`` methods as defined in
54+
the ``EventDispatcherInterface`` to your cache kernel. In addition, it triggers
55+
events before and/or after kernel methods to let the listeners interact. If you
56+
need to overwrite core ``HttpCache`` functionality in your kernel, you can
57+
provide your own event listeners. If you need to implement functionality
58+
directly on the methods, be careful to always call the trait methods rather
59+
than going directly to the parent, or events will not be triggered anymore. You
60+
might also need to copy a method from the trait and add your own logic between
61+
the events to not be too early or too late for the event.
6162

6263
When starting to extend your ``AppCache``, it is recommended to use the
6364
``EventDispatchingHttpCacheTestCase`` to run tests with your kernel to be sure

src/SymfonyCache/EventDispatchingHttpCache.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,23 @@ public function getEventDispatcher()
6464
/**
6565
* Add an event subscriber.
6666
*
67-
* @param EventSubscriberInterface $subscriber
67+
* @see EventDispatcherInterface::addSubscriber
6868
*/
6969
public function addSubscriber(EventSubscriberInterface $subscriber)
7070
{
7171
$this->getEventDispatcher()->addSubscriber($subscriber);
7272
}
7373

74+
/**
75+
* Add an event listener to this HttpCache.
76+
*
77+
* @see EventDispatcherInterface::addListener
78+
*/
79+
public function addListener($eventName, $listener, $priority = 0)
80+
{
81+
$this->getEventDispatcher()->addListener($eventName, $listener, $priority);
82+
}
83+
7484
/**
7585
* {@inheritdoc}
7686
*

src/Test/EventDispatchingHttpCacheTestCase.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,26 @@ public function testPreInvalidateReturnEarly()
297297
$this->assertSame($response, $method->invokeArgs($httpCache, [$request, $catch]));
298298
$this->assertEquals(1, $testListener->preInvalidateCalls);
299299
}
300+
301+
public function testAddListener()
302+
{
303+
$request = Request::create('/foo', 'GET');
304+
$response = new Response();
305+
306+
$httpCache = $this->getHttpCachePartialMock(['lookup']);
307+
$simpleListener = new SimpleListener($this, $httpCache, $request);
308+
$httpCache->addListener(Events::PRE_HANDLE, [$simpleListener, 'callback']);
309+
310+
$httpCache
311+
->expects($this->any())
312+
->method('lookup')
313+
->with($request)
314+
->will($this->returnValue($response))
315+
;
316+
317+
$this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST));
318+
$this->assertEquals(1, $simpleListener->calls);
319+
}
300320
}
301321

302322
class TestListener implements EventSubscriberInterface
@@ -416,3 +436,40 @@ public function preInvalidate(CacheEvent $event)
416436
++$this->preInvalidateCalls;
417437
}
418438
}
439+
440+
class SimpleListener
441+
{
442+
public $calls = 0;
443+
444+
/**
445+
* @var EventDispatchingHttpCacheTestCase To do assertions
446+
*/
447+
private $test;
448+
449+
/**
450+
* @var CacheInvalidationInterface The kernel to ensure the event carries the correct kernel
451+
*/
452+
private $kernel;
453+
454+
/**
455+
* @var Request The request to ensure the event carries the correct request
456+
*/
457+
private $request;
458+
459+
public function __construct(
460+
EventDispatchingHttpCacheTestCase $test,
461+
CacheInvalidationInterface $kernel,
462+
Request $request
463+
) {
464+
$this->test = $test;
465+
$this->kernel = $kernel;
466+
$this->request = $request;
467+
}
468+
469+
public function callback(CacheEvent $event)
470+
{
471+
$this->test->assertSame($this->kernel, $event->getKernel());
472+
$this->test->assertSame($this->request, $event->getRequest());
473+
++$this->calls;
474+
}
475+
}

0 commit comments

Comments
 (0)