From 968c870c9455c363ec14fed44e17313d6e4ec368 Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Thu, 24 Feb 2011 21:48:51 -0800 Subject: [PATCH] [AsseticBundle] added etags to controller so changes to filters etc trigger invalidation --- .../Controller/AsseticController.php | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php b/src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php index 3655fde742154..642907c127b8f 100644 --- a/src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php +++ b/src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php @@ -12,7 +12,7 @@ namespace Symfony\Bundle\AsseticBundle\Controller; use Assetic\Asset\AssetCache; -use Assetic\AssetManager; +use Assetic\Factory\LazyAssetManager; use Assetic\Cache\CacheInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -29,7 +29,7 @@ class AsseticController protected $am; protected $cache; - public function __construct(Request $request, AssetManager $am, CacheInterface $cache) + public function __construct(Request $request, LazyAssetManager $am, CacheInterface $cache) { $this->request = $request; $this->am = $am; @@ -43,18 +43,24 @@ public function render($name) } $asset = $this->getAsset($name); + $response = $this->createResponse(); - $response = new Response(); - - // validate if-modified-since + // last-modified if (null !== $lastModified = $asset->getLastModified()) { $date = new \DateTime(); $date->setTimestamp($lastModified); $response->setLastModified($date); + } + + // etag + if ($this->am->hasFormula($name)) { + $formula = $this->am->getFormula($name); + $formula['last_modified'] = $lastModified; + $response->setETag(md5(serialize($formula))); + } - if ($response->isNotModified($this->request)) { - return $response; - } + if ($response->isNotModified($this->request)) { + return $response; } $response->setContent($asset->dump()); @@ -62,6 +68,11 @@ public function render($name) return $response; } + protected function createResponse() + { + return new Response(); + } + protected function getAsset($name) { return new AssetCache($this->am->get($name), $this->cache);