Skip to content

Commit 78be609

Browse files
committed
[AsseticBundle] fixed various bugs in PHP templating, added new "combine" option
1 parent 4236896 commit 78be609

File tree

6 files changed

+33
-30
lines changed

6 files changed

+33
-30
lines changed

Resources/config/templating_php.xml

-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
<tag name="assetic.templating.php" />
1616
<argument type="service" id="templating.helper.router" />
1717
<argument type="service" id="assetic.asset_factory" />
18-
<argument>%assetic.debug%</argument>
1918
</service>
2019

2120
<service id="assetic.helper.static" class="%assetic.helper.static.class%">
2221
<tag name="assetic.templating.php" />
2322
<argument type="service" id="templating.helper.assets" />
2423
<argument type="service" id="assetic.asset_factory" />
25-
<argument>%assetic.debug%</argument>
2624
</service>
2725

2826
<service id="assetic.php_formula_loader" class="%assetic.cached_formula_loader.class%" public="false">

Routing/AsseticLoader.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ public function load($routingResource, $type = null)
6767

6868
$this->loadRouteForAsset($routes, $asset, $name);
6969

70+
$debug = isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug();
71+
$combine = isset($formula[2]['combine']) ? $formula[2]['combine'] : !$debug;
72+
7073
// add a route for each "leaf" in debug mode
71-
if (isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug()) {
74+
if (!$combine) {
7275
$i = 0;
7376
foreach ($asset as $leaf) {
7477
$this->loadRouteForAsset($routes, $leaf, $name, $i++);

Templating/AsseticHelper.php

+24-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Assetic\Asset\AssetInterface;
1515
use Assetic\Factory\AssetFactory;
16+
use Assetic\Util\TraversableString;
1617
use Symfony\Component\Templating\Helper\Helper;
1718

1819
/**
@@ -23,18 +24,15 @@
2324
abstract class AsseticHelper extends Helper
2425
{
2526
protected $factory;
26-
protected $debug;
2727

2828
/**
2929
* Constructor.
3030
*
3131
* @param AssetFactory $factory The asset factory
32-
* @param Boolean $debug The debug mode
3332
*/
34-
public function __construct(AssetFactory $factory, $debug = false)
33+
public function __construct(AssetFactory $factory)
3534
{
3635
$this->factory = $factory;
37-
$this->debug = $debug;
3836
}
3937

4038
/**
@@ -43,7 +41,7 @@ public function __construct(AssetFactory $factory, $debug = false)
4341
public function javascripts($inputs = array(), $filters = array(), array $options = array())
4442
{
4543
if (!isset($options['output'])) {
46-
$options['output'] = 'js/*';
44+
$options['output'] = 'js/*.js';
4745
}
4846

4947
return $this->getAssetUrls($inputs, $filters, $options);
@@ -55,7 +53,7 @@ public function javascripts($inputs = array(), $filters = array(), array $option
5553
public function stylesheets($inputs = array(), $filters = array(), array $options = array())
5654
{
5755
if (!isset($options['output'])) {
58-
$options['output'] = 'css/*';
56+
$options['output'] = 'css/*.css';
5957
}
6058

6159
return $this->getAssetUrls($inputs, $filters, $options);
@@ -109,31 +107,37 @@ private function getAssetUrls($inputs = array(), $filters = array(), array $opti
109107
}
110108

111109
if (!isset($options['debug'])) {
112-
$options['debug'] = $this->debug;
110+
$options['debug'] = $this->factory->isDebug();
111+
}
112+
113+
if (!isset($options['combine'])) {
114+
$options['combine'] = !$options['debug'];
113115
}
114116

115117
if (isset($options['single']) && $options['single'] && 1 < count($inputs)) {
116118
$inputs = array_slice($inputs, -1);
117119
}
118120

119121
if (!isset($options['name'])) {
120-
$options['name'] = $this->factory->generateAssetName($inputs, $filters);
121-
}
122-
123-
$coll = $this->factory->createAsset($inputs, $filters, $options);
124-
125-
if (!$options['debug']) {
126-
return array($this->getAssetUrl($coll, $options));
122+
$options['name'] = $this->factory->generateAssetName($inputs, $filters, $options);
127123
}
128124

129-
$urls = array();
130-
foreach ($coll as $leaf) {
131-
$urls[] = $this->getAssetUrl($leaf, array_replace($options, array(
132-
'name' => $options['name'].'_'.count($urls),
133-
)));
125+
$asset = $this->factory->createAsset($inputs, $filters, $options);
126+
127+
$one = $this->getAssetUrl($asset, $options);
128+
$many = array();
129+
if ($options['combine']) {
130+
$many[] = $one;
131+
} else {
132+
$i = 0;
133+
foreach ($asset as $leaf) {
134+
$many[] = $this->getAssetUrl($leaf, array_replace($options, array(
135+
'name' => $options['name'].'_'.$i++,
136+
)));
137+
}
134138
}
135139

136-
return $urls;
140+
return new TraversableString($one, $many);
137141
}
138142

139143
/**

Templating/DynamicAsseticHelper.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ class DynamicAsseticHelper extends AsseticHelper
2929
*
3030
* @param RouterHelper $routerHelper The router helper
3131
* @param AssetFactory $factory The asset factory
32-
* @param Boolean $debug The debug mode
3332
*/
34-
public function __construct(RouterHelper $routerHelper, AssetFactory $factory, $debug = false)
33+
public function __construct(RouterHelper $routerHelper, AssetFactory $factory)
3534
{
3635
$this->routerHelper = $routerHelper;
3736

38-
parent::__construct($factory, $debug);
37+
parent::__construct($factory);
3938
}
4039

4140
protected function getAssetUrl(AssetInterface $asset, $options = array())

Templating/StaticAsseticHelper.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ class StaticAsseticHelper extends AsseticHelper
2929
*
3030
* @param AssetsHelper $assetsHelper The assets helper
3131
* @param AssetFactory $factory The asset factory
32-
* @param Boolean $debug The debug mode
3332
*/
34-
public function __construct(AssetsHelper $assetsHelper, AssetFactory $factory, $debug = false)
33+
public function __construct(AssetsHelper $assetsHelper, AssetFactory $factory)
3534
{
3635
$this->assetsHelper = $assetsHelper;
3736

38-
parent::__construct($factory, $debug);
37+
parent::__construct($factory);
3938
}
4039

4140
protected function getAssetUrl(AssetInterface $asset, $options = array())

Tests/Templating/AsseticHelperTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testUrls($debug, $count, $message)
3434
$helper = new AsseticHelperForTest(new AssetFactory('/foo', $debug), $debug);
3535
$urls = $helper->javascripts(array('js/jquery.js', 'js/jquery.plugin.js'));
3636

37-
$this->assertInternalType('array', $urls, '->javascripts() returns an array');
37+
$this->assertInstanceOf('Traversable', $urls, '->javascripts() returns an array');
3838
$this->assertEquals($count, count($urls), $message);
3939
}
4040

0 commit comments

Comments
 (0)