Skip to content

Commit d5cccee

Browse files
authored
Merge pull request #338 from j0k3r/symfony-3.2-for-1.3
Add support for Symfony 3.*
2 parents 23c4dc2 + cf0687a commit d5cccee

File tree

7 files changed

+40
-20
lines changed

7 files changed

+40
-20
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ matrix:
6464
env:
6565
- SYMFONY_VERSION='3.0.*'
6666
- FRAMEWORK_EXTRA_VERSION='~3.0'
67+
- php: 5.6
68+
env:
69+
- SYMFONY_VERSION='3.1.*'
70+
- FRAMEWORK_EXTRA_VERSION='~3.0'
71+
- php: 5.6
72+
env:
73+
- SYMFONY_VERSION='3.2.*'
74+
- FRAMEWORK_EXTRA_VERSION='~3.0'
6775

6876
install:
6977
- pip install -qr Resources/doc/requirements.txt --user

EventListener/AbstractRuleSubscriber.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,16 @@ protected function matchRule(Request $request, Response $response)
5454

5555
return false;
5656
}
57+
58+
/**
59+
* Decide whether to even look for matching rules with the current request.
60+
*
61+
* @param Request $request
62+
*
63+
* @return bool True if the request is safe and headers can be set
64+
*/
65+
protected function isRequestCacheable(Request $request)
66+
{
67+
return method_exists($request, 'isMethodCacheable') ? $request->isMethodCacheable() : $request->isMethodSafe();
68+
}
5769
}

EventListener/CacheControlSubscriber.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function onKernelResponse(FilterResponseEvent $event)
102102
}
103103

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

@@ -158,7 +158,7 @@ private function setCache(Response $response, array $directives, $overwrite)
158158
return;
159159
}
160160

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

@@ -210,16 +210,4 @@ private function setExtraCacheDirectives(Response $response, array $controls, $o
210210
}
211211
}
212212
}
213-
214-
/**
215-
* Decide whether to even look for matching rules with the current request.
216-
*
217-
* @param Request $request
218-
*
219-
* @return bool True if the request is safe and headers can be set
220-
*/
221-
protected function isRequestSafe(Request $request)
222-
{
223-
return $request->isMethodSafe();
224-
}
225213
}

EventListener/TagSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function onKernelResponse(FilterResponseEvent $event)
7979
}
8080
}
8181

82-
if ($request->isMethodSafe()) {
82+
if ($this->isRequestCacheable($request)) {
8383
$this->tagHandler->addTags($tags);
8484
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
8585
// For safe requests (GET and HEAD), set cache tags on response

Tests/Functional/EventListener/CacheControlSubscriberTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public function testNotCached()
3030

3131
$client->request('GET', '/noncached');
3232
$response = $client->getResponse();
33-
$this->assertEquals('no-cache', $response->headers->get('Cache-Control'));
33+
$this->assertContains('no-cache', $response->headers->get('Cache-Control'));
3434
}
3535
}

Tests/Functional/Fixtures/Controller/TagController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818

1919
class TagController extends Controller
2020
{
21+
/**
22+
* Workaround to be compliant with all Symfony version.
23+
*
24+
* @param Request $request
25+
*
26+
* @return bool True if the request is cacheable
27+
*/
28+
private function isRequestCacheable(Request $request)
29+
{
30+
return method_exists($request, 'isMethodCacheable') ? $request->isMethodCacheable() : $request->isMethodSafe();
31+
}
32+
2133
/**
2234
* @Tag("all-items")
2335
* @Tag("item-123")
@@ -32,7 +44,7 @@ public function listAction()
3244
*/
3345
public function itemAction(Request $request, $id)
3446
{
35-
if (!$request->isMethodSafe()) {
47+
if (!$this->isRequestCacheable($request)) {
3648
$this->container->get('fos_http_cache.handler.tag_handler')->invalidateTags(array('all-items'));
3749
}
3850

Tests/Unit/EventListener/CacheControlSubscriberTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function testSetOnlyNoCacheHeader()
224224
$subscriber->onKernelResponse($event);
225225
$newHeaders = $event->getResponse()->headers->all();
226226

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

230230
public function testSkip()
@@ -242,7 +242,7 @@ public function testSkip()
242242
$subscriber->onKernelResponse($event);
243243
$newHeaders = $event->getResponse()->headers->all();
244244

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

248248
public function testVary()
@@ -332,7 +332,7 @@ public function testMatchRule()
332332

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

338338
/**

0 commit comments

Comments
 (0)