Skip to content

Commit c166470

Browse files
committed
Merge pull request #119 from FriendsOfSymfony/fix-105
Fix #105
2 parents 51bee50 + 7452746 commit c166470

File tree

9 files changed

+75
-38
lines changed

9 files changed

+75
-38
lines changed

EventListener/TagSubscriber.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use FOS\HttpCacheBundle\Configuration\Tag;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1717
use Symfony\Component\HttpFoundation\Request;
18-
use Symfony\Component\HttpFoundation\Response;
1918
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
2019
use Symfony\Component\HttpKernel\KernelEvents;
2120
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
@@ -78,10 +77,7 @@ public function onKernelResponse(FilterResponseEvent $event)
7877
$tags[] = $tag;
7978
}
8079
foreach ($configuredTags['expressions'] as $expression) {
81-
$tags[] = $this->expressionLanguage->evaluate($expression, array(
82-
'request' => $request,
83-
'response' => $response,
84-
));
80+
$tags[] = $this->evaluateTag($expression, $request);
8581
}
8682
}
8783

@@ -98,6 +94,16 @@ public function onKernelResponse(FilterResponseEvent $event)
9894
}
9995
}
10096

97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public static function getSubscribedEvents()
101+
{
102+
return array(
103+
KernelEvents::RESPONSE => 'onKernelResponse'
104+
);
105+
}
106+
101107
/**
102108
* Get the tags from the annotations on the controller that was used in the
103109
* request.
@@ -118,9 +124,9 @@ private function getAnnotationTags(Request $request)
118124
$tags = array();
119125
foreach ($tagConfigurations as $tagConfiguration) {
120126
if (null !== $tagConfiguration->getExpression()) {
121-
$tags[] = $this->expressionLanguage->evaluate(
127+
$tags[] = $this->evaluateTag(
122128
$tagConfiguration->getExpression(),
123-
$request->attributes->all()
129+
$request
124130
);
125131
} else {
126132
$tags = array_merge($tags, $tagConfiguration->getTags());
@@ -131,12 +137,19 @@ private function getAnnotationTags(Request $request)
131137
}
132138

133139
/**
134-
* {@inheritdoc}
140+
* Evaluate a tag that contains expressions
141+
*
142+
* @param string $expression
143+
* @param Request $request
144+
*
145+
* @return string Evaluaated tag
135146
*/
136-
public static function getSubscribedEvents()
147+
private function evaluateTag($expression, Request $request)
137148
{
138-
return array(
139-
KernelEvents::RESPONSE => 'onKernelResponse'
149+
return $this->expressionLanguage->evaluate(
150+
$expression,
151+
$request->attributes->all()
140152
);
141153
}
154+
142155
}

Resources/doc/reference/annotations.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ response::
114114
*/
115115
public function showAction($id)
116116
{
117-
// Assume $id equals 123
117+
// Assume request parameter $id equals 123
118118
}
119119

120120
Or, using a `param converter`_::

Resources/doc/reference/configuration/tags.rst

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ tags
22
====
33

44
Create tag rules in your application configuration to set tags on responses
5-
and invalidate them.
5+
and invalidate them. See the :doc:`tagging feature chapter </features/tagging>`
6+
for an introduction.
67

78
.. include:: /includes/enabled.rst
89

@@ -40,6 +41,14 @@ invalidated instead.
4041

4142
.. include:: /includes/match.rst
4243

44+
tags
45+
^^^^
46+
47+
**type**: ``array``
48+
49+
Tags that should be set on responses to safe requests; or invalidated for
50+
unsafe requests.
51+
4352
.. code-block:: yaml
4453
4554
# app/config/config.yml
@@ -51,14 +60,27 @@ invalidated instead.
5160
path: ^/news
5261
tags: [news-section]
5362
54-
.. note::
55-
56-
See further the :doc:`tagging feature description </features/tagging>`.
5763
58-
tags
59-
^^^^
64+
tag_expressions
65+
~~~~~~~~~~~~~~~
6066

6167
**type**: ``array``
6268

63-
Tags that should be set on responses to safe requests; or invalidated for
64-
unsafe requests.
69+
You can dynamically refer to request attributes using
70+
:ref:`expressions <expression language requirement>`. Assume a route
71+
``/articles/{id}``. A request to path ``/articles/123`` will set/invalidate
72+
tag ``articles-123`` with the following configuration:
73+
74+
.. code-block:: yaml
75+
76+
# app/config/config.yml
77+
fos_http_cache:
78+
tags:
79+
rules:
80+
-
81+
match:
82+
path: ^/articles
83+
tags: [articles]
84+
tag_expressions: ["'article-'~id"]
85+
86+
You can combine ``tags`` and ``tag_expression`` in one rule.

Tests/Functional/EventListener/CacheControlSubscriberTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testIsCached()
1919
{
2020
$client = static::createClient();
2121

22-
$client->request('GET', '/cached');
22+
$client->request('GET', '/cached/42');
2323
$response = $client->getResponse();
2424
$this->assertEquals('public', $response->headers->get('Cache-Control'));
2525
}

Tests/Functional/EventListener/TagSubscriberTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function testConfigurationTagsAreSet()
6363
{
6464
$client = static::createClient();
6565

66-
$client->request('GET', '/cached');
66+
$client->request('GET', '/cached/51');
6767
$response = $client->getResponse();
68-
$this->assertEquals('area', $response->headers->get('X-Cache-Tags'));
68+
$this->assertEquals('area,area-51', $response->headers->get('X-Cache-Tags'));
6969
}
7070

7171
public function testConfigurationTagsAreInvalidated()
@@ -76,11 +76,11 @@ public function testConfigurationTagsAreInvalidated()
7676
'fos_http_cache.cache_manager',
7777
'\FOS\HttpCacheBundle\CacheManager'
7878
)
79-
->shouldReceive('invalidateTags')->once()->with(array('area'))
79+
->shouldReceive('invalidateTags')->once()->with(array('area', 'area-51'))
8080
->shouldReceive('flush')->once()
8181
;
8282

83-
$client->request('POST', '/cached');
83+
$client->request('PUT', '/cached/51');
8484
}
8585

8686
protected function tearDown()

Tests/Functional/Fixtures/Controller/TestController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public function errorAction()
4747
return new Response('Forbidden', 403);
4848
}
4949

50-
public function contentAction()
50+
public function contentAction($id = null)
5151
{
52-
return new Response('content');
52+
return new Response('content ' . $id);
5353
}
5454
}

Tests/Functional/Fixtures/app/config/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fos_http_cache:
2323
match:
2424
path: ^/cached
2525
tags: [area]
26+
tag_expressions: ["'area-'~id"]
2627
user_context:
2728
user_identifier_headers:
2829
- Cookie
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
test_list:
2-
pattern: /test/list
3-
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::listAction }
2+
pattern: /test/list
3+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::listAction }
44
methods: [GET]
55

66
test_error:
7-
pattern: /test/error
8-
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::errorAction }
7+
pattern: /test/error
8+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::errorAction }
99

1010
test_one:
11-
pattern: /test/{id}
12-
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::itemAction }
11+
pattern: /test/{id}
12+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::itemAction }
1313
methods: [GET,POST]
1414

1515
test_cached:
16-
pattern: /cached
17-
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }
16+
pattern: /cached/{id}
17+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }
18+
methods: [GET,PUT]
1819

1920
test_noncached:
2021
pattern: /noncached
21-
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }
22+
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }
2223

2324
test_logout:
2425
pattern: /secured_area/logout

Tests/Unit/EventListener/TagSubscriberTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testOnKernelResponseGet()
6868
;
6969
$this->listener->addRule($mockMatcher, array(
7070
'tags' => array('configured-tag'),
71-
'expressions' => array('"item-" ~ request.attributes.get("id")'),
71+
'expressions' => array('"item-" ~ id'),
7272
));
7373
$this->listener->onKernelResponse($event);
7474

@@ -123,7 +123,7 @@ public function testOnKernelResponsePost()
123123
;
124124
$this->listener->addRule($mockMatcher, array(
125125
'tags' => array('configured-tag'),
126-
'expressions' => array('"item-" ~ request.attributes.get("id")'),
126+
'expressions' => array('"item-" ~ id'),
127127
));
128128
$this->listener->onKernelResponse($event);
129129
}

0 commit comments

Comments
 (0)