Skip to content

Commit 7f7ea42

Browse files
committed
[AsseticBundle] added setting asset root when using bundle notation
1 parent b173884 commit 7f7ea42

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/Symfony/Bundle/AsseticBundle/Factory/AssetFactory.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,23 @@ public function __construct(KernelInterface $kernel, ContainerInterface $contain
4242
}
4343

4444
/**
45-
* Adds support for bundle notation and globs.
45+
* Adds support for bundle notation file and glob assets.
4646
*
47-
* Please note this is a naive implementation of globs in that it doesn't
47+
* FIXME: This is a naive implementation of globs in that it doesn't
4848
* attempt to support bundle inheritance within the glob pattern itself.
4949
*/
5050
protected function parseInput($input, array $options = array())
5151
{
5252
// expand bundle notation
5353
if ('@' == $input[0] && false !== strpos($input, '/')) {
54+
// use the bundle path as this asset's root
55+
$bundle = substr($input, 1);
56+
if (false !== $pos = strpos($bundle, '/')) {
57+
$bundle = substr($bundle, 0, $pos);
58+
}
59+
$options['root'] = array($this->kernel->getBundle($bundle)->getPath());
60+
61+
// canonicalize the input
5462
if (false !== $pos = strpos($input, '*')) {
5563
// locateResource() does not support globs so we provide a naive implementation here
5664
list($before, $after) = explode('*', $input, 2);

src/Symfony/Bundle/AsseticBundle/Tests/Factory/AssetFactoryTest.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,51 @@ protected function setUp()
3333
public function testBundleNotation()
3434
{
3535
$input = '@MyBundle/Resources/css/main.css';
36+
$bundle = $this->getMock('Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface');
3637

38+
$this->kernel->expects($this->once())
39+
->method('getBundle')
40+
->with('MyBundle')
41+
->will($this->returnValue($bundle));
3742
$this->kernel->expects($this->once())
3843
->method('locateResource')
3944
->with($input)
40-
->will($this->returnValue('/path/to/bundle/Resources/css/main.css'));
45+
->will($this->returnValue('/path/to/MyBundle/Resources/css/main.css'));
46+
$bundle->expects($this->once())
47+
->method('getPath')
48+
->will($this->returnValue('/path/to/MyBundle'));
49+
50+
$coll = $this->factory->createAsset($input)->all();
51+
$asset = $coll[0];
4152

42-
$this->factory->createAsset($input);
53+
$this->assertEquals('/path/to/MyBundle', $asset->getSourceRoot(), '->createAsset() sets the asset root');
54+
$this->assertEquals('Resources/css/main.css', $asset->getSourcePath(), '->createAsset() sets the asset path');
4355
}
4456

4557
/**
4658
* @dataProvider getGlobs
4759
*/
4860
public function testBundleGlobNotation($input)
4961
{
62+
$bundle = $this->getMock('Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface');
63+
64+
$this->kernel->expects($this->once())
65+
->method('getBundle')
66+
->with('MyBundle')
67+
->will($this->returnValue($bundle));
5068
$this->kernel->expects($this->once())
5169
->method('locateResource')
5270
->with('@MyBundle/Resources/css/')
53-
->will($this->returnValue('/path/to/bundle/Resources/css/'));
71+
->will($this->returnValue('/path/to/MyBundle/Resources/css/'));
72+
$bundle->expects($this->once())
73+
->method('getPath')
74+
->will($this->returnValue('/path/to/MyBundle'));
75+
76+
$coll = $this->factory->createAsset($input)->all();
77+
$asset = $coll[0];
5478

55-
$this->factory->createAsset($input);
79+
$this->assertEquals('/path/to/MyBundle', $asset->getSourceRoot(), '->createAsset() sets the asset root');
80+
$this->assertNull($asset->getSourcePath(), '->createAsset() sets the asset path to null');
5681
}
5782

5883
public function getGlobs()

0 commit comments

Comments
 (0)