Skip to content

Commit

Permalink
[AsseticBundle] fixed various bugs in PHP templating, added new "comb…
Browse files Browse the repository at this point in the history
…ine" option
  • Loading branch information
kriswallsmith committed May 19, 2011
1 parent 8268675 commit 7eeb945
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
Expand Up @@ -15,14 +15,12 @@
<tag name="assetic.templating.php" />
<argument type="service" id="templating.helper.router" />
<argument type="service" id="assetic.asset_factory" />
<argument>%assetic.debug%</argument>
</service>

<service id="assetic.helper.static" class="%assetic.helper.static.class%">
<tag name="assetic.templating.php" />
<argument type="service" id="templating.helper.assets" />
<argument type="service" id="assetic.asset_factory" />
<argument>%assetic.debug%</argument>
</service>

<service id="assetic.php_formula_loader" class="%assetic.cached_formula_loader.class%" public="false">
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Bundle/AsseticBundle/Routing/AsseticLoader.php
Expand Up @@ -67,8 +67,11 @@ public function load($routingResource, $type = null)

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

$debug = isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug();
$combine = isset($formula[2]['combine']) ? $formula[2]['combine'] : !$debug;

// add a route for each "leaf" in debug mode
if (isset($formula[2]['debug']) ? $formula[2]['debug'] : $this->am->isDebug()) {
if (!$combine) {
$i = 0;
foreach ($asset as $leaf) {
$this->loadRouteForAsset($routes, $leaf, $name, $i++);
Expand Down
44 changes: 24 additions & 20 deletions src/Symfony/Bundle/AsseticBundle/Templating/AsseticHelper.php
Expand Up @@ -13,6 +13,7 @@

use Assetic\Asset\AssetInterface;
use Assetic\Factory\AssetFactory;
use Assetic\Util\TraversableString;
use Symfony\Component\Templating\Helper\Helper;

/**
Expand All @@ -23,18 +24,15 @@
abstract class AsseticHelper extends Helper
{
protected $factory;
protected $debug;

/**
* Constructor.
*
* @param AssetFactory $factory The asset factory
* @param Boolean $debug The debug mode
*/
public function __construct(AssetFactory $factory, $debug = false)
public function __construct(AssetFactory $factory)
{
$this->factory = $factory;
$this->debug = $debug;
}

/**
Expand All @@ -43,7 +41,7 @@ public function __construct(AssetFactory $factory, $debug = false)
public function javascripts($inputs = array(), $filters = array(), array $options = array())
{
if (!isset($options['output'])) {
$options['output'] = 'js/*';
$options['output'] = 'js/*.js';
}

return $this->getAssetUrls($inputs, $filters, $options);
Expand All @@ -55,7 +53,7 @@ public function javascripts($inputs = array(), $filters = array(), array $option
public function stylesheets($inputs = array(), $filters = array(), array $options = array())
{
if (!isset($options['output'])) {
$options['output'] = 'css/*';
$options['output'] = 'css/*.css';
}

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

if (!isset($options['debug'])) {
$options['debug'] = $this->debug;
$options['debug'] = $this->factory->isDebug();
}

if (!isset($options['combine'])) {
$options['combine'] = !$options['debug'];
}

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

if (!isset($options['name'])) {
$options['name'] = $this->factory->generateAssetName($inputs, $filters);
}

$coll = $this->factory->createAsset($inputs, $filters, $options);

if (!$options['debug']) {
return array($this->getAssetUrl($coll, $options));
$options['name'] = $this->factory->generateAssetName($inputs, $filters, $options);
}

$urls = array();
foreach ($coll as $leaf) {
$urls[] = $this->getAssetUrl($leaf, array_replace($options, array(
'name' => $options['name'].'_'.count($urls),
)));
$asset = $this->factory->createAsset($inputs, $filters, $options);

$one = $this->getAssetUrl($asset, $options);
$many = array();
if ($options['combine']) {
$many[] = $one;
} else {
$i = 0;
foreach ($asset as $leaf) {
$many[] = $this->getAssetUrl($leaf, array_replace($options, array(
'name' => $options['name'].'_'.$i++,
)));
}
}

return $urls;
return new TraversableString($one, $many);
}

/**
Expand Down
Expand Up @@ -29,13 +29,12 @@ class DynamicAsseticHelper extends AsseticHelper
*
* @param RouterHelper $routerHelper The router helper
* @param AssetFactory $factory The asset factory
* @param Boolean $debug The debug mode
*/
public function __construct(RouterHelper $routerHelper, AssetFactory $factory, $debug = false)
public function __construct(RouterHelper $routerHelper, AssetFactory $factory)
{
$this->routerHelper = $routerHelper;

parent::__construct($factory, $debug);
parent::__construct($factory);
}

protected function getAssetUrl(AssetInterface $asset, $options = array())
Expand Down
Expand Up @@ -29,13 +29,12 @@ class StaticAsseticHelper extends AsseticHelper
*
* @param AssetsHelper $assetsHelper The assets helper
* @param AssetFactory $factory The asset factory
* @param Boolean $debug The debug mode
*/
public function __construct(AssetsHelper $assetsHelper, AssetFactory $factory, $debug = false)
public function __construct(AssetsHelper $assetsHelper, AssetFactory $factory)
{
$this->assetsHelper = $assetsHelper;

parent::__construct($factory, $debug);
parent::__construct($factory);
}

protected function getAssetUrl(AssetInterface $asset, $options = array())
Expand Down
Expand Up @@ -34,7 +34,7 @@ public function testUrls($debug, $count, $message)
$helper = new AsseticHelperForTest(new AssetFactory('/foo', $debug), $debug);
$urls = $helper->javascripts(array('js/jquery.js', 'js/jquery.plugin.js'));

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

Expand Down

0 comments on commit 7eeb945

Please sign in to comment.