Skip to content

Commit 166316c

Browse files
committed
Fix #105
* configured tags can access request.attributes with simple expression vars (id) * response is no longer available as a value for expressions; but there’s no use case for that anyway * update docs
1 parent 51bee50 commit 166316c

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

EventListener/TagSubscriber.php

+24-11
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

+1-1
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

+30-8
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/Unit/EventListener/TagSubscriberTest.php

+2-2
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)