Skip to content

Commit

Permalink
Fix #105
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ddeboer committed Jul 30, 2014
1 parent 51bee50 commit 166316c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 22 deletions.
35 changes: 24 additions & 11 deletions EventListener/TagSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use FOS\HttpCacheBundle\Configuration\Tag;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
Expand Down Expand Up @@ -78,10 +77,7 @@ public function onKernelResponse(FilterResponseEvent $event)
$tags[] = $tag;
}
foreach ($configuredTags['expressions'] as $expression) {
$tags[] = $this->expressionLanguage->evaluate($expression, array(
'request' => $request,
'response' => $response,
));
$tags[] = $this->evaluateTag($expression, $request);
}
}

Expand All @@ -98,6 +94,16 @@ public function onKernelResponse(FilterResponseEvent $event)
}
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => 'onKernelResponse'
);
}

/**
* Get the tags from the annotations on the controller that was used in the
* request.
Expand All @@ -118,9 +124,9 @@ private function getAnnotationTags(Request $request)
$tags = array();
foreach ($tagConfigurations as $tagConfiguration) {
if (null !== $tagConfiguration->getExpression()) {
$tags[] = $this->expressionLanguage->evaluate(
$tags[] = $this->evaluateTag(
$tagConfiguration->getExpression(),
$request->attributes->all()
$request
);
} else {
$tags = array_merge($tags, $tagConfiguration->getTags());
Expand All @@ -131,12 +137,19 @@ private function getAnnotationTags(Request $request)
}

/**
* {@inheritdoc}
* Evaluate a tag that contains expressions
*
* @param string $expression
* @param Request $request
*
* @return string Evaluaated tag
*/
public static function getSubscribedEvents()
private function evaluateTag($expression, Request $request)
{
return array(
KernelEvents::RESPONSE => 'onKernelResponse'
return $this->expressionLanguage->evaluate(
$expression,
$request->attributes->all()
);
}

}
2 changes: 1 addition & 1 deletion Resources/doc/reference/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ response::
*/
public function showAction($id)
{
// Assume $id equals 123
// Assume request parameter $id equals 123
}

Or, using a `param converter`_::
Expand Down
38 changes: 30 additions & 8 deletions Resources/doc/reference/configuration/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ tags
====

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

.. include:: /includes/enabled.rst

Expand Down Expand Up @@ -40,6 +41,14 @@ invalidated instead.

.. include:: /includes/match.rst

tags
^^^^

**type**: ``array``

Tags that should be set on responses to safe requests; or invalidated for
unsafe requests.

.. code-block:: yaml
# app/config/config.yml
Expand All @@ -51,14 +60,27 @@ invalidated instead.
path: ^/news
tags: [news-section]
.. note::

See further the :doc:`tagging feature description </features/tagging>`.
tags
^^^^
tag_expressions
~~~~~~~~~~~~~~~

**type**: ``array``

Tags that should be set on responses to safe requests; or invalidated for
unsafe requests.
You can dynamically refer to request attributes using
:ref:`expressions <expression language requirement>`. Assume a route
``/articles/{id}``. A request to path ``/articles/123`` will set/invalidate
tag ``articles-123`` with the following configuration:

.. code-block:: yaml
# app/config/config.yml
fos_http_cache:
tags:
rules:
-
match:
path: ^/articles
tags: [articles]
tag_expressions: ["article-"~id]
You can combine ``tags`` and ``tag_expression`` in one rule.
4 changes: 2 additions & 2 deletions Tests/Unit/EventListener/TagSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testOnKernelResponseGet()
;
$this->listener->addRule($mockMatcher, array(
'tags' => array('configured-tag'),
'expressions' => array('"item-" ~ request.attributes.get("id")'),
'expressions' => array('"item-" ~ id'),
));
$this->listener->onKernelResponse($event);

Expand Down Expand Up @@ -123,7 +123,7 @@ public function testOnKernelResponsePost()
;
$this->listener->addRule($mockMatcher, array(
'tags' => array('configured-tag'),
'expressions' => array('"item-" ~ request.attributes.get("id")'),
'expressions' => array('"item-" ~ id'),
));
$this->listener->onKernelResponse($event);
}
Expand Down

0 comments on commit 166316c

Please sign in to comment.