Skip to content

Commit 052ab23

Browse files
committed
Move Cache-Control logic to a Util class + Add tests
1 parent 0581437 commit 052ab23

File tree

5 files changed

+94
-40
lines changed

5 files changed

+94
-40
lines changed

Controller/Controller.php

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
1515
use FOS\JsRoutingBundle\Response\RoutesResponse;
16+
use FOS\JsRoutingBundle\Util\CacheControlConfig;
1617
use Symfony\Component\Config\ConfigCache;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpFoundation\Response;
@@ -32,14 +33,14 @@ class Controller
3233
protected $serializer;
3334

3435
/**
35-
* @var \FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface
36+
* @var ExposedRoutesExtractorInterface
3637
*/
3738
protected $exposedRoutesExtractor;
3839

3940
/**
40-
* @var array
41+
* @var CacheControlConfig
4142
*/
42-
protected $cacheControl;
43+
protected $cacheControlConfig;
4344

4445
/**
4546
* @var boolean
@@ -56,10 +57,10 @@ class Controller
5657
*/
5758
public function __construct($serializer, ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = array(), $debug = false)
5859
{
59-
$this->serializer = $serializer;
60+
$this->serializer = $serializer;
6061
$this->exposedRoutesExtractor = $exposedRoutesExtractor;
61-
$this->cacheControl = $cacheControl;
62-
$this->debug = $debug;
62+
$this->cacheControlConfig = new CacheControlConfig($cacheControl);
63+
$this->debug = $debug;
6364
}
6465

6566
/**
@@ -104,38 +105,7 @@ public function indexAction(Request $request, $_format)
104105
}
105106

106107
$response = new Response($content, 200, array('Content-Type' => $request->getMimeType($_format)));
107-
108-
return $this->setCacheHeaders($response);
109-
}
110-
111-
/**
112-
* @param Response $response
113-
*
114-
* @return Response
115-
*/
116-
protected function setCacheHeaders(Response $response)
117-
{
118-
if (empty($this->cacheControl['enabled'])) {
119-
return $response;
120-
}
121-
122-
$this->cacheControl['public'] ? $response->setPublic() : $response->setPrivate();
123-
124-
if (is_integer($this->cacheControl['maxage'])) {
125-
$response->setMaxAge($this->cacheControl['maxage']);
126-
}
127-
128-
if (is_integer($this->cacheControl['smaxage'])) {
129-
$response->setSharedMaxAge($this->cacheControl['smaxage']);
130-
}
131-
132-
if ($this->cacheControl['expires'] !== null) {
133-
$response->setExpires(new \DateTime($this->cacheControl['expires']));
134-
}
135-
136-
if (!empty($this->cacheControl['vary'])) {
137-
$response->setVary($this->cacheControl['vary']);
138-
}
108+
$this->cacheControlConfig->apply($response);
139109

140110
return $response;
141111
}

DependencyInjection/FOSJsRoutingExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function load(array $configs, ContainerBuilder $container)
5858
} else {
5959
$config['cache_control'] = array('enabled' => false);
6060
}
61+
6162
$container->setParameter('fos_js_routing.cache_control', $config['cache_control']);
6263
}
6364
}

Resources/config/services.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
<parameters>
77
<parameter key="fos_js_routing.extractor.class">FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor</parameter>
8-
<parameter key="fos_js_routing.cache_control.class">FOS\JsRoutingBundle\CacheControl\CacheControl</parameter>
98
</parameters>
109

1110
<services>
@@ -16,5 +15,4 @@
1615
<argument>%kernel.bundles%</argument>
1716
</service>
1817
</services>
19-
2018
</container>

Tests/Controller/ControllerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,36 @@ public function testIndexActionWithoutRoutes()
5555
$this->assertEquals('{"base_url":"","routes":[],"prefix":"","host":"","scheme":""}', $response->getContent());
5656
$this->assertEquals(200, $response->getStatusCode());
5757
$this->assertEquals('application/json', $response->headers->get('Content-Type'));
58+
59+
$this->assertFalse($response->headers->hasCacheControlDirective('public'));
60+
$this->assertNull($response->getExpires());
61+
$this->assertFalse($response->headers->hasCacheControlDirective('max-age'));
62+
$this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
63+
}
64+
65+
public function testCacheControl()
66+
{
67+
$cacheControlConfig = array(
68+
'enabled' => true,
69+
'public' => true,
70+
'expires' => '2013-10-04 23:59:59 UTC',
71+
'maxage' => 123,
72+
'smaxage' => 456,
73+
'vary' => array(),
74+
);
75+
76+
$controller = new Controller($this->getSerializer(), $this->getExtractor(), $cacheControlConfig, sys_get_temp_dir());
77+
$response = $controller->indexAction($this->getRequest('/'), 'json');
78+
79+
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
80+
81+
$this->assertEquals('2013-10-04 23:59:59', $response->getExpires()->format('Y-m-d H:i:s'));
82+
83+
$this->assertTrue($response->headers->hasCacheControlDirective('max-age'));
84+
$this->assertEquals(123, $response->headers->getCacheControlDirective('max-age'));
85+
86+
$this->assertTrue($response->headers->hasCacheControlDirective('s-maxage'));
87+
$this->assertEquals(456, $response->headers->getCacheControlDirective('s-maxage'));
5888
}
5989

6090
private function getExtractor(array $exposedRoutes = array(), $baseUrl = '')

Util/CacheControlConfig.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSJsRoutingBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\JsRoutingBundle\Util;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
class CacheControlConfig
17+
{
18+
/**
19+
* @var array
20+
*/
21+
private $parameters;
22+
23+
public function __construct(array $parameters = array())
24+
{
25+
$this->parameters = $parameters;
26+
}
27+
28+
/**
29+
* @param Response $response
30+
*/
31+
public function apply(Response $response)
32+
{
33+
if (empty($this->parameters['enabled'])) {
34+
return;
35+
}
36+
37+
$this->parameters['public'] ? $response->setPublic() : $response->setPrivate();
38+
39+
if (is_integer($this->parameters['maxage'])) {
40+
$response->setMaxAge($this->parameters['maxage']);
41+
}
42+
43+
if (is_integer($this->parameters['smaxage'])) {
44+
$response->setSharedMaxAge($this->parameters['smaxage']);
45+
}
46+
47+
if ($this->parameters['expires'] !== null) {
48+
$response->setExpires(new \DateTime($this->parameters['expires']));
49+
}
50+
51+
if (!empty($this->parameters['vary'])) {
52+
$response->setVary($this->parameters['vary']);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)