Skip to content

Commit

Permalink
Merge pull request #119 from FriendsOfSymfony/fix-105
Browse files Browse the repository at this point in the history
Fix #105
  • Loading branch information
dbu committed Jul 31, 2014
2 parents 51bee50 + 7452746 commit c166470
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 38 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.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testIsCached()
{
$client = static::createClient();

$client->request('GET', '/cached');
$client->request('GET', '/cached/42');
$response = $client->getResponse();
$this->assertEquals('public', $response->headers->get('Cache-Control'));
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/Functional/EventListener/TagSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function testConfigurationTagsAreSet()
{
$client = static::createClient();

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

public function testConfigurationTagsAreInvalidated()
Expand All @@ -76,11 +76,11 @@ public function testConfigurationTagsAreInvalidated()
'fos_http_cache.cache_manager',
'\FOS\HttpCacheBundle\CacheManager'
)
->shouldReceive('invalidateTags')->once()->with(array('area'))
->shouldReceive('invalidateTags')->once()->with(array('area', 'area-51'))
->shouldReceive('flush')->once()
;

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

protected function tearDown()
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Fixtures/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public function errorAction()
return new Response('Forbidden', 403);
}

public function contentAction()
public function contentAction($id = null)
{
return new Response('content');
return new Response('content ' . $id);
}
}
1 change: 1 addition & 0 deletions Tests/Functional/Fixtures/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fos_http_cache:
match:
path: ^/cached
tags: [area]
tag_expressions: ["'area-'~id"]
user_context:
user_identifier_headers:
- Cookie
Expand Down
19 changes: 10 additions & 9 deletions Tests/Functional/Fixtures/app/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
test_list:
pattern: /test/list
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::listAction }
pattern: /test/list
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::listAction }
methods: [GET]

test_error:
pattern: /test/error
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::errorAction }
pattern: /test/error
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::errorAction }

test_one:
pattern: /test/{id}
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::itemAction }
pattern: /test/{id}
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::itemAction }
methods: [GET,POST]

test_cached:
pattern: /cached
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }
pattern: /cached/{id}
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }
methods: [GET,PUT]

test_noncached:
pattern: /noncached
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }

test_logout:
pattern: /secured_area/logout
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 c166470

Please sign in to comment.