Skip to content

Commit

Permalink
[AsseticBundle] updated for Assetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kriswallsmith committed Mar 25, 2011
1 parent 0e57943 commit 2e1924e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 54 deletions.
87 changes: 55 additions & 32 deletions src/Symfony/Bundle/AsseticBundle/Templating/AsseticHelper.php
Expand Up @@ -25,6 +25,7 @@ class AsseticHelper extends Helper
protected $debug;
protected $defaultJavascriptsOutput;
protected $defaultStylesheetsOutput;
protected $defaultImageOutput;

/**
* Constructor.
Expand All @@ -33,13 +34,60 @@ class AsseticHelper extends Helper
* @param Boolean $debug The debug mode
* @param string $defaultJavascriptsOutput The default {@link javascripts()} output string
* @param string $defaultStylesheetsOutput The default {@link stylesheets()} output string
* @param string $defaultImageOutput The default {@link image()} output string
*/
public function __construct(AssetFactory $factory, $debug = false, $defaultJavascriptsOutput = 'js/*.js', $defaultStylesheetsOutput = 'css/*.css')
public function __construct(AssetFactory $factory, $debug = false, $defaultJavascriptsOutput = 'js/*.js', $defaultStylesheetsOutput = 'css/*.css', $defaultImageOutput = 'images/*')
{
$this->factory = $factory;
$this->debug = $debug;
$this->defaultJavascriptsOutput = $defaultJavascriptsOutput;
$this->defaultStylesheetsOutput = $defaultStylesheetsOutput;
$this->defaultImageOutput = $defaultImageOutput;
}

/**
* Returns an array of javascript urls.
*
* This convenience method wraps {@link assets()} and provides a default
* output string.
*/
public function javascripts($inputs = array(), $filters = array(), array $options = array())
{
if (!isset($options['output'])) {
$options['output'] = $this->defaultJavascriptsOutput;
}

return $this->getAssetUrls($inputs, $filters, $options);
}

/**
* Returns an array of stylesheet urls.
*
* This convenience method wraps {@link assets()} and provides a default
* output string.
*/
public function stylesheets($inputs = array(), $filters = array(), array $options = array())
{
if (!isset($options['output'])) {
$options['output'] = $this->defaultStylesheetsOutput;
}

return $this->getAssetUrls($inputs, $filters, $options);
}

/**
* Returns an array of one image url.
*
* This convenience method wraps {@link assets()} and provides a default
* output string.
*/
public function image($inputs = array(), $filters = array(), array $options = array())
{
if (!isset($options['output'])) {
$options['output'] = $this->defaultImageOutput;
}

return $this->getAssetUrls($inputs, $filters, $options, true);
}

/**
Expand All @@ -57,10 +105,11 @@ public function __construct(AssetFactory $factory, $debug = false, $defaultJavas
* @param array|string $inputs An array or comma-separated list of input strings
* @param array|string $filters An array or comma-separated list of filter names
* @param array $options An array of options
* @param Boolean $single Use only the last input string
*
* @return array An array of URLs for the asset
*/
public function assets($inputs = array(), $filters = array(), array $options = array())
private function getAssetUrls($inputs = array(), $filters = array(), array $options = array(), $single = false)
{
$explode = function($value)
{
Expand All @@ -79,6 +128,10 @@ public function assets($inputs = array(), $filters = array(), array $options = a
$options['debug'] = $this->debug;
}

if ($single && 1 < count($inputs)) {
$inputs = array_slice($inputs, -1);
}

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

if (!$options['debug']) {
Expand All @@ -93,36 +146,6 @@ public function assets($inputs = array(), $filters = array(), array $options = a
return $urls;
}

/**
* Returns an array of javascript urls.
*
* This convenience method wraps {@link assets()} and provides a default
* output string.
*/
public function javascripts($inputs = array(), $filters = array(), array $options = array())
{
if (!isset($options['output'])) {
$options['output'] = $this->defaultJavascriptsOutput;
}

return $this->assets($inputs, $filters, $options);
}

/**
* Returns an array of stylesheet urls.
*
* This convenience method wraps {@link assets()} and provides a default
* output string.
*/
public function stylesheets($inputs = array(), $filters = array(), array $options = array())
{
if (!isset($options['output'])) {
$options['output'] = $this->defaultStylesheetsOutput;
}

return $this->assets($inputs, $filters, $options);
}

public function getName()
{
return 'assetic';
Expand Down
15 changes: 5 additions & 10 deletions src/Symfony/Bundle/AsseticBundle/Tests/FunctionalTest.php
Expand Up @@ -50,11 +50,8 @@ public function testKernel($debug, $count)
{
$kernel = new TestKernel('test', $debug);
$kernel->boot();
$container = $kernel->getContainer();

$names = $container->get('assetic.asset_manager')->getNames();

$this->assertEquals($count, count($names));
$this->assertEquals($count, count($kernel->getContainer()->get('assetic.asset_manager')->getNames()));
}

/**
Expand All @@ -64,12 +61,9 @@ public function testRoutes($debug, $count)
{
$kernel = new TestKernel('test', $debug);
$kernel->boot();
$container = $kernel->getContainer();

$routes = $container->get('router')->getRouteCollection()->all();

$matches = 0;
foreach (array_keys($routes) as $name) {
foreach (array_keys($kernel->getContainer()->get('router')->getRouteCollection()->all()) as $name) {
if (0 === strpos($name, 'assetic_')) {
++$matches;
}
Expand Down Expand Up @@ -110,9 +104,10 @@ public function testPhpRenderDebug()

public function provideDebugAndAssetCount()
{
// totals include assets defined in both php and twig templates
return array(
array(true, 5),
array(false, 2),
array(true, 8),
array(false, 3),
);
}
}
Expand Up @@ -11,3 +11,9 @@
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock %}

{% block content %}
{% image 'logo.png' %}
<img src="{{ asset_url }}">
{% endimage %}
{% endblock %}
Expand Up @@ -19,3 +19,4 @@ twig:
assetic:
use_controller: true
read_from: "%kernel.root_dir%/web"
bundles: [TestBundle]
Expand Up @@ -31,17 +31,17 @@ protected function setUp()
public function testUrls($debug, $count, $message)
{
$helper = new AsseticHelper(new AssetFactory('/foo', $debug), $debug);
$urls = $helper->assets(array('js/jquery.js', 'js/jquery.plugin.js'));
$urls = $helper->javascripts(array('js/jquery.js', 'js/jquery.plugin.js'));

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

public function getDebugAndCount()
{
return array(
array(false, 1, '->assets() returns one url when not in debug mode'),
array(true, 2, '->assets() returns many urls when in debug mode'),
array(false, 1, '->javascripts() returns one url when not in debug mode'),
array(true, 2, '->javascripts() returns many urls when in debug mode'),
);
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Bundle/AsseticBundle/Twig/DynamicExtension.php
Expand Up @@ -15,7 +15,7 @@
use Assetic\Factory\AssetFactory;

/**
* Assetic integration.
* The dynamic extension is used when use_controllers is enabled.
*
* @author Kris Wallsmith <kris.wallsmith@symfony.com>
*/
Expand All @@ -24,9 +24,9 @@ class DynamicExtension extends AsseticExtension
public function getTokenParsers()
{
return array(
new DynamicTokenParser($this->factory, $this->debug),
new DynamicTokenParser($this->factory, $this->debug, $this->defaultJavascriptsOutput, 'javascripts'),
new DynamicTokenParser($this->factory, $this->debug, $this->defaultStylesheetsOutput, 'stylesheets'),
new DynamicTokenParser($this->factory, 'javascripts', 'js/*.js', $this->debug),
new DynamicTokenParser($this->factory, 'stylesheets', 'css/*.css', $this->debug),
new DynamicTokenParser($this->factory, 'image', 'images/*', $this->debug, true),
);
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Bundle/AsseticBundle/Twig/StaticExtension.php
Expand Up @@ -15,7 +15,7 @@
use Assetic\Factory\AssetFactory;

/**
* Assetic integration.
* The Static extension is used when use_controllers is disabled.
*
* @author Kris Wallsmith <kris.wallsmith@symfony.com>
*/
Expand All @@ -24,9 +24,9 @@ class StaticExtension extends AsseticExtension
public function getTokenParsers()
{
return array(
new StaticTokenParser($this->factory, $this->debug),
new StaticTokenParser($this->factory, $this->debug, $this->defaultJavascriptsOutput, 'javascripts'),
new StaticTokenParser($this->factory, $this->debug, $this->defaultStylesheetsOutput, 'stylesheets'),
new StaticTokenParser($this->factory, 'javascripts', 'js/*.js', $this->debug),
new StaticTokenParser($this->factory, 'stylesheets', 'css/*.css', $this->debug),
new StaticTokenParser($this->factory, 'image', 'images/*', $this->debug, true),
);
}
}

0 comments on commit 2e1924e

Please sign in to comment.