Merge remote branch 'kriswallsmith/assetic/combine'

* kriswallsmith/assetic/combine:
  [AsseticBundle] added missing filter to test
  [AsseticBundle] added config for cssimport filter
  [AsseticBundle] added config for packager filter
  [AsseticBundle] removed problematic test
  [AsseticBundle] fixed various bugs in PHP templating, added new "combine" option
This commit is contained in:
Fabien Potencier 2011-05-19 15:47:35 +02:00
commit 75e7a7a76f
10 changed files with 71 additions and 65 deletions

View File

@ -0,0 +1,16 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="assetic.filter.cssimport.class">Assetic\Filter\CssImportFilter</parameter>
</parameters>
<services>
<service id="assetic.filter.cssimport" class="%assetic.filter.cssimport.class%">
<tag name="assetic.filter" alias="cssimport" />
</service>
</services>
</container>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="assetic.filter.packager.class">Assetic\Filter\PackagerFilter</parameter>
<parameter key="assetic.filter.packager.packages" type="collection" />
</parameters>
<services>
<service id="assetic.filter.packager" class="%assetic.filter.packager.class%">
<tag name="assetic.filter" alias="packager" />
<argument>%assetic.filter.packager.packages%</argument>
</service>
</services>
</container>

View File

@ -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">

View File

@ -67,8 +67,11 @@ class AsseticLoader extends Loader
$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++);

View File

@ -13,6 +13,7 @@ namespace Symfony\Bundle\AsseticBundle\Templating;
use Assetic\Asset\AssetInterface;
use Assetic\Factory\AssetFactory;
use Assetic\Util\TraversableString;
use Symfony\Component\Templating\Helper\Helper;
/**
@ -23,18 +24,15 @@ use Symfony\Component\Templating\Helper\Helper;
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;
}
/**
@ -43,7 +41,7 @@ abstract class AsseticHelper extends Helper
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);
@ -55,7 +53,7 @@ abstract class AsseticHelper extends Helper
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);
@ -109,7 +107,11 @@ abstract class AsseticHelper extends Helper
}
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)) {
@ -117,23 +119,25 @@ abstract class AsseticHelper extends Helper
}
if (!isset($options['name'])) {
$options['name'] = $this->factory->generateAssetName($inputs, $filters);
$options['name'] = $this->factory->generateAssetName($inputs, $filters, $options);
}
$coll = $this->factory->createAsset($inputs, $filters, $options);
$asset = $this->factory->createAsset($inputs, $filters, $options);
if (!$options['debug']) {
return array($this->getAssetUrl($coll, $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++,
)));
}
}
$urls = array();
foreach ($coll as $leaf) {
$urls[] = $this->getAssetUrl($leaf, array_replace($options, array(
'name' => $options['name'].'_'.count($urls),
)));
}
return $urls;
return new TraversableString($one, $many);
}
/**

View File

@ -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())

View File

@ -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())

View File

@ -102,12 +102,16 @@ class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
return array(
array('closure', array('jar' => '/path/to/closure.jar')),
array('coffee'),
array('compass'),
array('cssembed', array('jar' => '/path/to/cssembed.jar')),
array('cssimport'),
array('cssrewrite'),
array('jpegtran'),
array('jpegoptim'),
array('jpegtran'),
array('less'),
array('lessphp'),
array('optipng'),
array('packager'),
array('pngout'),
array('sass'),
array('scss'),

View File

@ -43,41 +43,6 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
$filesystem->remove($this->cacheDir);
}
public function testRoutes()
{
$countRoutes = function($router)
{
$count = 0;
foreach ($router->getRouteCollection()->all() as $name => $route) {
if (0 === strpos($name, '_assetic_')) {
++$count;
}
}
return $count;
};
$kernel = new TestKernel('test', false);
$kernel->boot();
$am = $kernel->getContainer()->get('assetic.asset_manager');
$names = $am->getNames();
$baseline = $expected = count($names);
foreach ($names as $name) {
$asset = $am->get($name);
foreach ($asset as $leaf) {
++$expected;
}
}
$this->assertEquals($baseline, $countRoutes($kernel->getContainer()->get('router')));
$kernel = new TestKernel('test', true);
$kernel->boot();
$this->assertEquals($expected, $countRoutes($kernel->getContainer()->get('router')));
}
public function testTwigRenderDebug()
{
$kernel = new TestKernel('test', true);

View File

@ -34,7 +34,7 @@ class AsseticHelperTest extends \PHPUnit_Framework_TestCase
$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);
}