Skip to content

Commit

Permalink
Merge pull request #338 from j0k3r/symfony-3.2-for-1.3
Browse files Browse the repository at this point in the history
Add support for Symfony 3.*
  • Loading branch information
dbu authored Jan 6, 2017
2 parents 23c4dc2 + cf0687a commit d5cccee
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ matrix:
env:
- SYMFONY_VERSION='3.0.*'
- FRAMEWORK_EXTRA_VERSION='~3.0'
- php: 5.6
env:
- SYMFONY_VERSION='3.1.*'
- FRAMEWORK_EXTRA_VERSION='~3.0'
- php: 5.6
env:
- SYMFONY_VERSION='3.2.*'
- FRAMEWORK_EXTRA_VERSION='~3.0'

install:
- pip install -qr Resources/doc/requirements.txt --user
Expand Down
12 changes: 12 additions & 0 deletions EventListener/AbstractRuleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ protected function matchRule(Request $request, Response $response)

return false;
}

/**
* Decide whether to even look for matching rules with the current request.
*
* @param Request $request
*
* @return bool True if the request is safe and headers can be set
*/
protected function isRequestCacheable(Request $request)
{
return method_exists($request, 'isMethodCacheable') ? $request->isMethodCacheable() : $request->isMethodSafe();
}
}
16 changes: 2 additions & 14 deletions EventListener/CacheControlSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function onKernelResponse(FilterResponseEvent $event)
}

// do not change cache directives on unsafe requests.
if ($this->skip || !$this->isRequestSafe($request)) {
if ($this->skip || !$this->isRequestCacheable($request)) {
return;
}

Expand Down Expand Up @@ -158,7 +158,7 @@ private function setCache(Response $response, array $directives, $overwrite)
return;
}

if ('no-cache' === $response->headers->get('cache-control')) {
if (false !== strpos($response->headers->get('Cache-Control'), 'no-cache')) {
// this single header is set by default. if its the only thing, we override it.
$response->setCache($directives);

Expand Down Expand Up @@ -210,16 +210,4 @@ private function setExtraCacheDirectives(Response $response, array $controls, $o
}
}
}

/**
* Decide whether to even look for matching rules with the current request.
*
* @param Request $request
*
* @return bool True if the request is safe and headers can be set
*/
protected function isRequestSafe(Request $request)
{
return $request->isMethodSafe();
}
}
2 changes: 1 addition & 1 deletion EventListener/TagSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function onKernelResponse(FilterResponseEvent $event)
}
}

if ($request->isMethodSafe()) {
if ($this->isRequestCacheable($request)) {
$this->tagHandler->addTags($tags);
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
// For safe requests (GET and HEAD), set cache tags on response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function testNotCached()

$client->request('GET', '/noncached');
$response = $client->getResponse();
$this->assertEquals('no-cache', $response->headers->get('Cache-Control'));
$this->assertContains('no-cache', $response->headers->get('Cache-Control'));
}
}
14 changes: 13 additions & 1 deletion Tests/Functional/Fixtures/Controller/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@

class TagController extends Controller
{
/**
* Workaround to be compliant with all Symfony version.
*
* @param Request $request
*
* @return bool True if the request is cacheable
*/
private function isRequestCacheable(Request $request)
{
return method_exists($request, 'isMethodCacheable') ? $request->isMethodCacheable() : $request->isMethodSafe();
}

/**
* @Tag("all-items")
* @Tag("item-123")
Expand All @@ -32,7 +44,7 @@ public function listAction()
*/
public function itemAction(Request $request, $id)
{
if (!$request->isMethodSafe()) {
if (!$this->isRequestCacheable($request)) {
$this->container->get('fos_http_cache.handler.tag_handler')->invalidateTags(array('all-items'));
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/Unit/EventListener/CacheControlSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function testSetOnlyNoCacheHeader()
$subscriber->onKernelResponse($event);
$newHeaders = $event->getResponse()->headers->all();

$this->assertEquals('no-cache', $newHeaders['cache-control'][0]);
$this->assertContains('no-cache', $newHeaders['cache-control'][0]);
}

public function testSkip()
Expand All @@ -242,7 +242,7 @@ public function testSkip()
$subscriber->onKernelResponse($event);
$newHeaders = $event->getResponse()->headers->all();

$this->assertEquals('no-cache', $newHeaders['cache-control'][0]);
$this->assertContains('no-cache', $newHeaders['cache-control'][0]);
}

public function testVary()
Expand Down Expand Up @@ -332,7 +332,7 @@ public function testMatchRule()

$subscriber->onKernelResponse($event2);
$newHeaders = $response2->headers->all();
$this->assertEquals('no-cache', $newHeaders['cache-control'][0]);
$this->assertContains('no-cache', $newHeaders['cache-control'][0]);
}

/**
Expand Down

0 comments on commit d5cccee

Please sign in to comment.