Skip to content

Commit

Permalink
Move Cache-Control logic to a Util class + Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Oct 4, 2013
1 parent 0581437 commit 052ab23
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 40 deletions.
46 changes: 8 additions & 38 deletions Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface;
use FOS\JsRoutingBundle\Response\RoutesResponse;
use FOS\JsRoutingBundle\Util\CacheControlConfig;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -32,14 +33,14 @@ class Controller
protected $serializer;

/**
* @var \FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractorInterface
* @var ExposedRoutesExtractorInterface
*/
protected $exposedRoutesExtractor;

/**
* @var array
* @var CacheControlConfig
*/
protected $cacheControl;
protected $cacheControlConfig;

/**
* @var boolean
Expand All @@ -56,10 +57,10 @@ class Controller
*/
public function __construct($serializer, ExposedRoutesExtractorInterface $exposedRoutesExtractor, array $cacheControl = array(), $debug = false)
{
$this->serializer = $serializer;
$this->serializer = $serializer;
$this->exposedRoutesExtractor = $exposedRoutesExtractor;
$this->cacheControl = $cacheControl;
$this->debug = $debug;
$this->cacheControlConfig = new CacheControlConfig($cacheControl);
$this->debug = $debug;
}

/**
Expand Down Expand Up @@ -104,38 +105,7 @@ public function indexAction(Request $request, $_format)
}

$response = new Response($content, 200, array('Content-Type' => $request->getMimeType($_format)));

return $this->setCacheHeaders($response);
}

/**
* @param Response $response
*
* @return Response
*/
protected function setCacheHeaders(Response $response)
{
if (empty($this->cacheControl['enabled'])) {
return $response;
}

$this->cacheControl['public'] ? $response->setPublic() : $response->setPrivate();

if (is_integer($this->cacheControl['maxage'])) {
$response->setMaxAge($this->cacheControl['maxage']);
}

if (is_integer($this->cacheControl['smaxage'])) {
$response->setSharedMaxAge($this->cacheControl['smaxage']);
}

if ($this->cacheControl['expires'] !== null) {
$response->setExpires(new \DateTime($this->cacheControl['expires']));
}

if (!empty($this->cacheControl['vary'])) {
$response->setVary($this->cacheControl['vary']);
}
$this->cacheControlConfig->apply($response);

return $response;
}
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/FOSJsRoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function load(array $configs, ContainerBuilder $container)
} else {
$config['cache_control'] = array('enabled' => false);
}

$container->setParameter('fos_js_routing.cache_control', $config['cache_control']);
}
}
2 changes: 0 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

<parameters>
<parameter key="fos_js_routing.extractor.class">FOS\JsRoutingBundle\Extractor\ExposedRoutesExtractor</parameter>
<parameter key="fos_js_routing.cache_control.class">FOS\JsRoutingBundle\CacheControl\CacheControl</parameter>
</parameters>

<services>
Expand All @@ -16,5 +15,4 @@
<argument>%kernel.bundles%</argument>
</service>
</services>

</container>
30 changes: 30 additions & 0 deletions Tests/Controller/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ public function testIndexActionWithoutRoutes()
$this->assertEquals('{"base_url":"","routes":[],"prefix":"","host":"","scheme":""}', $response->getContent());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->headers->get('Content-Type'));

$this->assertFalse($response->headers->hasCacheControlDirective('public'));
$this->assertNull($response->getExpires());
$this->assertFalse($response->headers->hasCacheControlDirective('max-age'));
$this->assertFalse($response->headers->hasCacheControlDirective('s-maxage'));
}

public function testCacheControl()
{
$cacheControlConfig = array(
'enabled' => true,
'public' => true,
'expires' => '2013-10-04 23:59:59 UTC',
'maxage' => 123,
'smaxage' => 456,
'vary' => array(),
);

$controller = new Controller($this->getSerializer(), $this->getExtractor(), $cacheControlConfig, sys_get_temp_dir());
$response = $controller->indexAction($this->getRequest('/'), 'json');

$this->assertTrue($response->headers->hasCacheControlDirective('public'));

$this->assertEquals('2013-10-04 23:59:59', $response->getExpires()->format('Y-m-d H:i:s'));

$this->assertTrue($response->headers->hasCacheControlDirective('max-age'));
$this->assertEquals(123, $response->headers->getCacheControlDirective('max-age'));

$this->assertTrue($response->headers->hasCacheControlDirective('s-maxage'));
$this->assertEquals(456, $response->headers->getCacheControlDirective('s-maxage'));
}

private function getExtractor(array $exposedRoutes = array(), $baseUrl = '')
Expand Down
55 changes: 55 additions & 0 deletions Util/CacheControlConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/*
* This file is part of the FOSJsRoutingBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\JsRoutingBundle\Util;

use Symfony\Component\HttpFoundation\Response;

class CacheControlConfig
{
/**
* @var array
*/
private $parameters;

public function __construct(array $parameters = array())
{
$this->parameters = $parameters;
}

/**
* @param Response $response
*/
public function apply(Response $response)
{
if (empty($this->parameters['enabled'])) {
return;
}

$this->parameters['public'] ? $response->setPublic() : $response->setPrivate();

if (is_integer($this->parameters['maxage'])) {
$response->setMaxAge($this->parameters['maxage']);
}

if (is_integer($this->parameters['smaxage'])) {
$response->setSharedMaxAge($this->parameters['smaxage']);
}

if ($this->parameters['expires'] !== null) {
$response->setExpires(new \DateTime($this->parameters['expires']));
}

if (!empty($this->parameters['vary'])) {
$response->setVary($this->parameters['vary']);
}
}
}

0 comments on commit 052ab23

Please sign in to comment.