Преглед изворни кода

[AsseticBundle] added setting asset root when using bundle notation

Kris Wallsmith пре 14 година
родитељ
комит
7f7ea42870

+ 10 - 2
src/Symfony/Bundle/AsseticBundle/Factory/AssetFactory.php

@@ -42,15 +42,23 @@ class AssetFactory extends BaseAssetFactory
     }
 
     /**
-     * Adds support for bundle notation and globs.
+     * Adds support for bundle notation file and glob assets.
      *
-     * Please note this is a naive implementation of globs in that it doesn't
+     * FIXME: This is a naive implementation of globs in that it doesn't
      * attempt to support bundle inheritance within the glob pattern itself.
      */
     protected function parseInput($input, array $options = array())
     {
         // expand bundle notation
         if ('@' == $input[0] && false !== strpos($input, '/')) {
+            // use the bundle path as this asset's root
+            $bundle = substr($input, 1);
+            if (false !== $pos = strpos($bundle, '/')) {
+                $bundle = substr($bundle, 0, $pos);
+            }
+            $options['root'] = array($this->kernel->getBundle($bundle)->getPath());
+
+            // canonicalize the input
             if (false !== $pos = strpos($input, '*')) {
                 // locateResource() does not support globs so we provide a naive implementation here
                 list($before, $after) = explode('*', $input, 2);

+ 29 - 4
src/Symfony/Bundle/AsseticBundle/Tests/Factory/AssetFactoryTest.php

@@ -33,13 +33,25 @@ class AssetFactoryTest extends \PHPUnit_Framework_TestCase
     public function testBundleNotation()
     {
         $input = '@MyBundle/Resources/css/main.css';
+        $bundle = $this->getMock('Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface');
 
+        $this->kernel->expects($this->once())
+            ->method('getBundle')
+            ->with('MyBundle')
+            ->will($this->returnValue($bundle));
         $this->kernel->expects($this->once())
             ->method('locateResource')
             ->with($input)
-            ->will($this->returnValue('/path/to/bundle/Resources/css/main.css'));
+            ->will($this->returnValue('/path/to/MyBundle/Resources/css/main.css'));
+        $bundle->expects($this->once())
+            ->method('getPath')
+            ->will($this->returnValue('/path/to/MyBundle'));
+
+        $coll = $this->factory->createAsset($input)->all();
+        $asset = $coll[0];
 
-        $this->factory->createAsset($input);
+        $this->assertEquals('/path/to/MyBundle', $asset->getSourceRoot(), '->createAsset() sets the asset root');
+        $this->assertEquals('Resources/css/main.css', $asset->getSourcePath(), '->createAsset() sets the asset path');
     }
 
     /**
@@ -47,12 +59,25 @@ class AssetFactoryTest extends \PHPUnit_Framework_TestCase
      */
     public function testBundleGlobNotation($input)
     {
+        $bundle = $this->getMock('Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface');
+
+        $this->kernel->expects($this->once())
+            ->method('getBundle')
+            ->with('MyBundle')
+            ->will($this->returnValue($bundle));
         $this->kernel->expects($this->once())
             ->method('locateResource')
             ->with('@MyBundle/Resources/css/')
-            ->will($this->returnValue('/path/to/bundle/Resources/css/'));
+            ->will($this->returnValue('/path/to/MyBundle/Resources/css/'));
+        $bundle->expects($this->once())
+            ->method('getPath')
+            ->will($this->returnValue('/path/to/MyBundle'));
+
+        $coll = $this->factory->createAsset($input)->all();
+        $asset = $coll[0];
 
-        $this->factory->createAsset($input);
+        $this->assertEquals('/path/to/MyBundle', $asset->getSourceRoot(), '->createAsset() sets the asset root');
+        $this->assertNull($asset->getSourcePath(), '->createAsset() sets the asset path to null');
     }
 
     public function getGlobs()