Browse Source

[AsseticBundle] added local caching to the controller

Kris Wallsmith 14 years ago
parent
commit
cd5b60359d

+ 11 - 2
src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php

@@ -11,7 +11,9 @@
 
 namespace Symfony\Bundle\AsseticBundle\Controller;
 
+use Assetic\Asset\AssetCache;
 use Assetic\AssetManager;
+use Assetic\Cache\CacheInterface;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -26,12 +28,14 @@ class AsseticController
     protected $request;
     protected $response;
     protected $am;
+    protected $cache;
 
-    public function __construct(Request $request, Response $response, AssetManager $am)
+    public function __construct(Request $request, Response $response, AssetManager $am, CacheInterface $cache)
     {
         $this->request = $request;
         $this->response = $response;
         $this->am = $am;
+        $this->cache = $cache;
     }
 
     public function render($name)
@@ -40,7 +44,7 @@ class AsseticController
             throw new NotFoundHttpException('Asset Not Found');
         }
 
-        $asset = $this->am->get($name);
+        $asset = $this->getAsset($name);
 
         // validate if-modified-since
         if (null !== $lastModified = $asset->getLastModified()) {
@@ -57,4 +61,9 @@ class AsseticController
 
         return $this->response;
     }
+
+    protected function getAsset($name)
+    {
+        return new AssetCache($this->am->get($name), $this->cache);
+    }
 }

+ 6 - 0
src/Symfony/Bundle/AsseticBundle/Resources/config/controller.xml

@@ -7,6 +7,8 @@
     <parameters>
         <parameter key="assetic.controller.class">Symfony\Bundle\AsseticBundle\Controller\AsseticController</parameter>
         <parameter key="assetic.routing_loader.class">Symfony\Bundle\AsseticBundle\Routing\AsseticLoader</parameter>
+        <parameter key="assetic.cache.class">Assetic\Cache\FilesystemCache</parameter>
+        <parameter key="assetic.cache_dir">%kernel.cache_dir%/assetic</parameter>
     </parameters>
 
     <services>
@@ -18,6 +20,10 @@
             <argument type="service" id="request" />
             <argument type="service" id="response" />
             <argument type="service" id="assetic.asset_manager" />
+            <argument type="service" id="assetic.cache" />
+        </service>
+        <service id="assetic.cache" class="%assetic.cache.class%" public="false">
+            <argument>%assetic.cache_dir%</argument>
         </service>
     </services>
 </container>

+ 1 - 0
src/Symfony/Bundle/AsseticBundle/Tests/DependencyInjection/AsseticExtensionTest.php

@@ -39,6 +39,7 @@ class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
         $this->container->register('twig', 'Twig_Environment');
         $this->container->setParameter('kernel.debug', false);
         $this->container->setParameter('kernel.root_dir', __DIR__);
+        $this->container->setParameter('kernel.cache_dir', __DIR__);
         $this->container->setParameter('kernel.bundles', array());
     }