Skip to content

Commit 0581437

Browse files
Craig Menningwilldurand
authored andcommitted
Cache-Control Feature
* Added ability to set Cache-Control headers * Moved cache-control header settings into controller * Removed extraneous "use" statement * Updated test to pass empty array for cacheConfig
1 parent 73fd999 commit 0581437

File tree

7 files changed

+79
-3
lines changed

7 files changed

+79
-3
lines changed

Controller/Controller.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class Controller
3636
*/
3737
protected $exposedRoutesExtractor;
3838

39+
/**
40+
* @var array
41+
*/
42+
protected $cacheControl;
43+
3944
/**
4045
* @var boolean
4146
*/
@@ -46,12 +51,14 @@ class Controller
4651
*
4752
* @param mixed $serializer Any object with a serialize($data, $format) method
4853
* @param ExposedRoutesExtractorInterface $exposedRoutesExtractor The extractor service.
54+
* @param array $cacheControl
4955
* @param boolean $debug
5056
*/
51-
public function __construct($serializer, ExposedRoutesExtractorInterface $exposedRoutesExtractor, $debug = false)
57+
public function __construct($serializer, ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = array(), $debug = false)
5258
{
5359
$this->serializer = $serializer;
5460
$this->exposedRoutesExtractor = $exposedRoutesExtractor;
61+
$this->cacheControl = $cacheControl;
5562
$this->debug = $debug;
5663
}
5764

@@ -96,6 +103,40 @@ public function indexAction(Request $request, $_format)
96103
$content = $callback.'('.$content.');';
97104
}
98105

99-
return new Response($content, 200, array('Content-Type' => $request->getMimeType($_format)));
106+
$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+
}
139+
140+
return $response;
100141
}
101142
}

DependencyInjection/Configuration.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ public function getConfigTreeBuilder()
4141
->prototype('scalar')->end()
4242
->end()
4343
->scalarNode('request_context_base_url')->defaultNull()->end()
44+
->arrayNode('cache_control')
45+
->children()
46+
->booleanNode('public')->defaultFalse()->end()
47+
->scalarNode('expires')->defaultNull()->end()
48+
->scalarNode('maxage')->defaultNull()->end()
49+
->scalarNode('smaxage')->defaultNull()->end()
50+
->arrayNode('vary')
51+
->beforeNormalization()
52+
->ifTrue(function($v) { return !is_array($v); })
53+
->then(function($v) { return array($v); })
54+
->end()
55+
->prototype('scalar')->end()
56+
->end()
57+
->end()
58+
->end()
4459
->end();
4560

4661
return $builder;

DependencyInjection/FOSJsRoutingExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,12 @@ public function load(array $configs, ContainerBuilder $container)
5252
if (isset($config['request_context_base_url'])) {
5353
$container->setParameter('fos_js_routing.request_context_base_url', $config['request_context_base_url']);
5454
}
55+
56+
if (isset($config['cache_control'])) {
57+
$config['cache_control']['enabled'] = true;
58+
} else {
59+
$config['cache_control'] = array('enabled' => false);
60+
}
61+
$container->setParameter('fos_js_routing.cache_control', $config['cache_control']);
5562
}
5663
}

Resources/config/controllers.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<service id="fos_js_routing.controller" class="%fos_js_routing.controller.class%">
1010
<argument type="service" id="fos_js_routing.serializer" />
1111
<argument type="service" id="fos_js_routing.extractor" />
12+
<argument>%fos_js_routing.cache_control%</argument>
1213
<argument>%kernel.debug%</argument>
1314
</service>
1415
</services>

Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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>
89
</parameters>
910

1011
<services>

Resources/doc/README.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ You can prevent to expose a route by configuring it as below:
154154
options:
155155
expose: false
156156

157+
You can enable HTTP caching as below:
158+
159+
# app/config/config.yml
160+
fos_js_routing:
161+
cache_control:
162+
# All are optional, defaults shown
163+
public: false # can be true (public) or false (private)
164+
maxage: null # integer value, e.g. 300
165+
smaxage: null # integer value, e.g. 300
166+
expires: null # anything that can be fed to "new \DateTime($expires)", e.g. "5 minutes"
167+
vary: [] # string or array, e.g. "Cookie" or [ Cookie, Accept ]
157168

158169
Commands
159170
--------

Tests/Controller/ControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testGenerateWithCallback()
4949

5050
public function testIndexActionWithoutRoutes()
5151
{
52-
$controller = new Controller($this->getSerializer(), $this->getExtractor(), sys_get_temp_dir(), array());
52+
$controller = new Controller($this->getSerializer(), $this->getExtractor(), array(), sys_get_temp_dir());
5353
$response = $controller->indexAction($this->getRequest('/'), 'json');
5454

5555
$this->assertEquals('{"base_url":"","routes":[],"prefix":"","host":"","scheme":""}', $response->getContent());

0 commit comments

Comments
 (0)