Browse Source

Merge remote branch 'kriswallsmith/assetic/etags'

* kriswallsmith/assetic/etags:
  [AsseticBundle] added etags to controller so changes to filters etc trigger invalidation
Fabien Potencier 14 năm trước cách đây
mục cha
commit
ad80b09a4f

+ 19 - 8
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 @@ class AsseticController
         }
 
         $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 @@ class AsseticController
         return $response;
     }
 
+    protected function createResponse()
+    {
+        return new Response();
+    }
+
     protected function getAsset($name)
     {
         return new AssetCache($this->am->get($name), $this->cache);