Skip to content

Commit 12e4467

Browse files
committed
Have a method to add tags to the TagSubscriber directly, for when the request does not exist yet
1 parent 9e8bae3 commit 12e4467

File tree

4 files changed

+95
-7
lines changed

4 files changed

+95
-7
lines changed

EventListener/TagSubscriber.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ class TagSubscriber extends AbstractRuleSubscriber implements EventSubscriberInt
3636
*/
3737
private $expressionLanguage;
3838

39+
/**
40+
* List of tags to add to response.
41+
*
42+
* @var string[]
43+
*/
44+
private $tags = array();
45+
3946
/**
4047
* Constructor
4148
*
@@ -50,6 +57,22 @@ public function __construct(
5057
$this->expressionLanguage = $expressionLanguage;
5158
}
5259

60+
/**
61+
* Add tags to set on the main response.
62+
*
63+
* Contrary to CacheManager::tagResponse, this method can be called before
64+
* the response object exists.
65+
*
66+
* To invalidate tags, use CacheManager::invalidateTags directly.
67+
* Invalidation is only executed on the kernel.terminate event.
68+
*
69+
* @param array $tags List of tags to add.
70+
*/
71+
public function addTags(array $tags)
72+
{
73+
$this->tags = array_merge($this->tags, $tags);
74+
}
75+
5376
/**
5477
* Process the _tags request attribute, which is set when using the Tag
5578
* annotation
@@ -63,12 +86,16 @@ public function onKernelResponse(FilterResponseEvent $event)
6386
{
6487
$request = $event->getRequest();
6588
$response = $event->getResponse();
66-
67-
$tags = array();
89+
if ($request->isMethodSafe()) {
90+
$tags = $this->tags;
91+
$this->tags = array();
92+
} else {
93+
$tags = array();
94+
}
6895

6996
// Only set cache tags or invalidate them if response is successful
7097
if ($response->isSuccessful()) {
71-
$tags = $this->getAnnotationTags($request);
98+
$tags = array_merge($tags, $this->getAnnotationTags($request));
7299
}
73100

74101
$configuredTags = $this->matchRule($request, $response);

Resources/doc/features/tagging.rst

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ Setting and Invalidating Tags
3737
You can tag responses in three ways: with the cache manager, configuration and
3838
annotations.
3939

40-
Cache Manager
41-
~~~~~~~~~~~~~
40+
Tagging from code
41+
~~~~~~~~~~~~~~~~~
4242

43-
Use ``tagResponse($response, $tags)`` to set tags on a response::
43+
Inject the ``CacheManager`` (service ``fos_http_cache.cache_manager``) and
44+
use ``tagResponse($response, $tags)`` to set tags on a response::
4445

4546
use Symfony\Component\HttpFoundation\Response;
47+
use FOS\HttpCacheBundle\CacheManager;
4648

4749
class NewsController
4850
{
51+
/**
52+
* @var CacheManager
53+
*/
54+
private $cacheManager;
55+
4956
public function articleAction($id)
5057
{
5158
$response = new Response('Some news article');
@@ -55,7 +62,28 @@ Use ``tagResponse($response, $tags)`` to set tags on a response::
5562
}
5663
}
5764

58-
Then call ``invalidateTags($tags)`` to invalidate the tags you set::
65+
Or inject the ``TagSubscriber`` (service ``fos_http_cache.event_listener.tag``)
66+
and call ``addTags($tags)`` to set tags that will be added to the response once
67+
it exists::
68+
69+
use FOS\HttpCacheBundle\EventListener\TagSubscriber;
70+
71+
class Service
72+
{
73+
/**
74+
* @var TagSubscriber
75+
*/
76+
private $tagSubscriber;
77+
78+
public function doStuff($thing)
79+
{
80+
$this->tagSubscriber->addTags(array('news', 'news-' . $thing->getId()));
81+
82+
// ...
83+
}
84+
}
85+
86+
To invalidate tags, call ``CacheManager::invalidateTags($tags)``::
5987

6088
class NewsController
6189
{

Resources/doc/reference/cache-manager.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ option to true::
9595

9696
$cacheManager->tagResponse($response, array('different'), true);
9797

98+
.. tip::
99+
100+
If you do not yet have the response available, you can use the
101+
``TagSubscriber::addTags($tags)`` method to have your tags added once the
102+
response is being sent.
103+
98104
invalidateTags()
99105
----------------
100106

Tests/Unit/EventListener/TagSubscriberTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,33 @@ public function testOnKernelResponsePost()
128128
$this->listener->onKernelResponse($event);
129129
}
130130

131+
public function testAddTagsGet()
132+
{
133+
$request = new Request();
134+
$request->setMethod('GET');
135+
136+
$this->listener->addTags(array('a', 'b'));
137+
$event = $this->getEvent($request);
138+
$this->listener->onKernelResponse($event);
139+
140+
$this->assertEquals(
141+
'a,b',
142+
$event->getResponse()->headers->get($this->cacheManager->getTagsHeader())
143+
);
144+
}
145+
146+
public function testAddTagsPost()
147+
{
148+
$request = new Request();
149+
$request->setMethod('POST');
150+
151+
$this->listener->addTags(array('a', 'b'));
152+
$event = $this->getEvent($request);
153+
$this->listener->onKernelResponse($event);
154+
155+
$this->assertFalse($event->getResponse()->headers->has($this->cacheManager->getTagsHeader()));
156+
}
157+
131158
protected function getEvent(Request $request, Response $response = null)
132159
{
133160
return new FilterResponseEvent(

0 commit comments

Comments
 (0)