From d8079d9336d0c29db85bac8eed10857965d5e991 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Tue, 19 Aug 2014 10:55:42 +0200 Subject: [PATCH] also provide request itself in expressions --- EventListener/InvalidationSubscriber.php | 7 ++++++- EventListener/TagSubscriber.php | 9 +++++---- Resources/doc/reference/annotations.rst | 3 +++ Resources/doc/reference/configuration/tags.rst | 3 +++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/EventListener/InvalidationSubscriber.php b/EventListener/InvalidationSubscriber.php index e43fdeeb..5f411986 100644 --- a/EventListener/InvalidationSubscriber.php +++ b/EventListener/InvalidationSubscriber.php @@ -202,6 +202,11 @@ 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(); @@ -209,7 +214,7 @@ private function invalidateRoutes(array $routes, Request $request) // 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; diff --git a/EventListener/TagSubscriber.php b/EventListener/TagSubscriber.php index eec2b273..b8f80926 100644 --- a/EventListener/TagSubscriber.php +++ b/EventListener/TagSubscriber.php @@ -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); } /** diff --git a/Resources/doc/reference/annotations.rst b/Resources/doc/reference/annotations.rst index 614fd74c..c5e9d9e8 100644 --- a/Resources/doc/reference/annotations.rst +++ b/Resources/doc/reference/annotations.rst @@ -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: diff --git a/Resources/doc/reference/configuration/tags.rst b/Resources/doc/reference/configuration/tags.rst index 2b4a70e0..f1e8e4e0 100644 --- a/Resources/doc/reference/configuration/tags.rst +++ b/Resources/doc/reference/configuration/tags.rst @@ -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.