Skip to content

Fix #105 #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused.

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'
);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just moved this to have public methods above private ones.


/**
* 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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
public static function getSubscribedEvents()
private function evaluateTag($expression, Request $request)
{
return array(
KernelEvents::RESPONSE => 'onKernelResponse'
return $this->expressionLanguage->evaluate(
$expression,
$request->attributes->all()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this does not have the Request itself anymore

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it also did not have the request previously, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depends. On line 81 it was passed. On line 122 it was not (and this call looks invalid by passing a Request in an argument expecting an array)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof What do you mean exactly? evaluateTag(string $expression, Request $request) wraps a call to $expressionLanguage->evaluate(string $expression, array $values). As far as I can see evaluateTag is called twice, both times with a Request object:

https://github.com/FriendsOfSymfony/FOSHttpCacheBundle/blob/master/EventListener/TagSubscriber.php#L80
https://github.com/FriendsOfSymfony/FOSHttpCacheBundle/blob/master/EventListener/TagSubscriber.php#L127

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ddeboer the comment to which I replied was refering to the previous code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ddeboer well, SensioFRameworkExtraBundle is inconsistent. For @Security, you get more stuff, like the request.

@dbu the behavior for @Security is that known params always win. this way, you always know that request is the Request object. If you have a request attribute with the same name, you can always get it from the Request object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we just add the request for advanced users, mention it in the doc but do not have any examples using the request, to keep them simple? the strategy to overwrite a possible request attribute with the Request means that there would be a BC break if we would add this later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof Good point, and your use case makes sense. So let’s just give access to Request, too (while keeping easy access to request attributes; after all, that is what this PR was for). @dbu Can you create a PR for adding Request? To me this is no blocker for 1.0, as it won’t break BC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbu Ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #144

);
}

}
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