Skip to content

Commit

Permalink
Merge pull request #144 from FriendsOfSymfony/expression-request
Browse files Browse the repository at this point in the history
also provide request itself in expressions
  • Loading branch information
ddeboer committed Aug 19, 2014
2 parents 3b69fca + d8079d9 commit 6eb7222
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
7 changes: 6 additions & 1 deletion EventListener/InvalidationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,19 @@ private function invalidatePaths(array $pathConfigurations)
*/
private function invalidateRoutes(array $routes, Request $request)
{
$values = $request->attributes->all();
// if there is an attribute called "request", it needs to be accessed through the request.
$values['request'] = $request;
$expressionLanguage = $this->getExpressionLanguage();

foreach ($routes as $route) {
$params = array();

if (null !== $route->getParams()) {
// Iterate over route params and try to evaluate their values
foreach ($route->getParams() as $key => $value) {
if (is_array($value)) {
$value = $this->getExpressionLanguage()->evaluate($value['expression'], $request->attributes->all());
$value = $expressionLanguage->evaluate($value['expression'], $values);
}

$params[$key] = $value;
Expand Down
9 changes: 5 additions & 4 deletions EventListener/TagSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ private function getAnnotationTags(Request $request)
*/
private function evaluateTag($expression, Request $request)
{
return $this->getExpressionLanguage()->evaluate(
$expression,
$request->attributes->all()
);
$values = $request->attributes->all();
// if there is an attribute called "request", it needs to be accessed through the request.
$values['request'] = $request;

return $this->getExpressionLanguage()->evaluate($expression, $values);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Resources/doc/reference/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ route ``articles`` with the ``number`` parameter set to ``123``, do::
// Assume $request->attributes->get('id') returns 123
}

The expression has access to all request attributes and the request itself
under the name ``request``.

See :doc:`/features/invalidation` for more information.

.. _tag:
Expand Down
3 changes: 3 additions & 0 deletions Resources/doc/reference/configuration/tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,7 @@ tag ``articles-123`` with the following configuration:
tags: [articles]
tag_expressions: ["'article-'~id"]
The expression has access to all request attributes and the request itself
under the name ``request``.

You can combine ``tags`` and ``tag_expression`` in one rule.

0 comments on commit 6eb7222

Please sign in to comment.