浏览代码

[AsseticBundle] fixed router and controller

Kris Wallsmith 14 年之前
父节点
当前提交
57dd6aef86

+ 16 - 10
src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php

@@ -12,6 +12,7 @@
 namespace Symfony\Bundle\AsseticBundle\Controller;
 
 use Assetic\Asset\AssetCache;
+use Assetic\Asset\AssetInterface;
 use Assetic\Factory\LazyAssetManager;
 use Assetic\Cache\CacheInterface;
 use Symfony\Component\HttpFoundation\Request;
@@ -42,13 +43,9 @@ class AsseticController
             throw new NotFoundHttpException(sprintf('The "%s" asset could not be found.', $name));
         }
 
-        $asset = $this->getAsset($name);
-        if (null !== $pos) {
-            $leaves = array_values(iterator_to_array($asset));
-            if (!isset($leaves[$pos])) {
-                throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos));
-            }
-            $asset = $leaves[$pos];
+        $asset = $this->am->get($name);
+        if (null !== $pos && !$asset = $this->findAssetLeaf($asset, $pos)) {
+            throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos));
         }
 
         $response = $this->createResponse();
@@ -71,7 +68,7 @@ class AsseticController
             return $response;
         }
 
-        $response->setContent($asset->dump());
+        $response->setContent($this->cachifyAsset($asset)->dump());
 
         return $response;
     }
@@ -81,8 +78,17 @@ class AsseticController
         return new Response();
     }
 
-    protected function getAsset($name)
+    protected function cachifyAsset(AssetInterface $asset)
     {
-        return new AssetCache($this->am->get($name), $this->cache);
+        return new AssetCache($asset, $this->cache);
+    }
+
+    private function findAssetLeaf(AssetInterface $asset, $pos)
+    {
+        $leaves = array_values(iterator_to_array($asset));
+
+        if (isset($leaves[$pos])) {
+            return $leaves[$pos];
+        }
     }
 }

+ 7 - 3
src/Symfony/Bundle/AsseticBundle/Routing/AsseticLoader.php

@@ -71,8 +71,7 @@ class AsseticLoader extends Loader
             if (isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug()) {
                 $i = 0;
                 foreach ($asset as $leaf) {
-                    $pos = $i++;
-                    $this->loadRouteForAsset($routes, $leaf, $name.'_'.$pos, $pos);
+                    $this->loadRouteForAsset($routes, $leaf, $name, $i++);
                 }
             }
         }
@@ -106,7 +105,12 @@ class AsseticLoader extends Loader
             $defaults['_format'] = $format;
         }
 
-        $routes->add('_assetic_'.$name, new Route($pattern, $defaults));
+        $route = '_assetic_'.$name;
+        if (null !== $pos) {
+            $route .= '_'.$pos;
+        }
+
+        $routes->add($route, new Route($pattern, $defaults));
     }
 
     public function supports($resource, $type = null)