diff --git a/src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php b/src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php index 136ef133d6..6ffd6fb95e 100644 --- a/src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php +++ b/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]; + } } } diff --git a/src/Symfony/Bundle/AsseticBundle/Routing/AsseticLoader.php b/src/Symfony/Bundle/AsseticBundle/Routing/AsseticLoader.php index 97435441ee..9525fc8ff2 100644 --- a/src/Symfony/Bundle/AsseticBundle/Routing/AsseticLoader.php +++ b/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)