[AsseticBundle] cleaned up php templating support

This commit is contained in:
Kris Wallsmith 2011-04-01 11:57:33 -07:00
parent 6c3f50a585
commit e6d4734d4e
12 changed files with 169 additions and 87 deletions

View File

@ -72,9 +72,13 @@ class AsseticExtension extends Extension
if ($parameterBag->resolveValue($parameterBag->get('assetic.use_controller'))) {
$loader->load('controller.xml');
$container->setParameter('assetic.twig_extension.class', '%assetic.twig_extension.dynamic.class%');
$container->getDefinition('assetic.helper.dynamic')->addTag('templating.helper', array('alias' => 'assetic'));
$container->remove('assetic.helper.static');
} else {
$loader->load('asset_writer.xml');
$container->setParameter('assetic.twig_extension.class', '%assetic.twig_extension.static.class%');
$container->getDefinition('assetic.helper.static')->addTag('templating.helper', array('alias' => 'assetic'));
$container->remove('assetic.helper.dynamic');
}
// register config resources

View File

@ -27,7 +27,6 @@ class TemplatingPass implements CompilerPassInterface
return;
}
$am = $container->getDefinition('assetic.asset_manager');
$engines = $container->getParameterBag()->resolveValue($container->getParameter('templating.engines'));
if (!in_array('twig', $engines)) {

View File

@ -23,18 +23,18 @@ class AsseticHelperFormulaLoader extends BasePhpFormulaLoader
protected function registerPrototypes()
{
return array(
'$view[\'assetic\']->assets(*)' => array(),
'$view[\'assetic\']->javascripts(*)' => array('output' => 'js/*.js'),
'$view[\'assetic\']->stylesheets(*)' => array('output' => 'css/*.css'),
'$view["assetic"]->assets(*)' => array(),
'$view[\'assetic\']->image(*)' => array('output' => 'images/*', 'single' => true),
'$view["assetic"]->javascripts(*)' => array('output' => 'js/*.js'),
'$view["assetic"]->stylesheets(*)' => array('output' => 'css/*.css'),
'$view->get(\'assetic\')->assets(*)' => array(),
'$view["assetic"]->image(*)' => array('output' => 'images/*', 'single' => true),
'$view->get(\'assetic\')->javascripts(*)' => array('output' => 'js/*.js'),
'$view->get(\'assetic\')->stylesheets(*)' => array('output' => 'css/*.css'),
'$view->get("assetic")->assets(*)' => array(),
'$view->get(\'assetic\')->image(*)' => array('output' => 'images/*', 'single' => true),
'$view->get("assetic")->javascripts(*)' => array('output' => 'js/*.js'),
'$view->get("assetic")->stylesheets(*)' => array('output' => 'css/*.css'),
'$view->get("assetic")->image(*)' => array('output' => 'images/*', 'single' => true),
);
}
@ -60,6 +60,12 @@ class Helper
global $_call;
$_call = func_get_args();
}
public function image()
{
global $_call;
$_call = func_get_args();
}
}
class View extends ArrayObject

View File

@ -5,17 +5,26 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="assetic.helper.class">Symfony\Bundle\AsseticBundle\Templating\AsseticHelper</parameter>
<parameter key="assetic.helper.dynamic.class">Symfony\Bundle\AsseticBundle\Templating\DynamicAsseticHelper</parameter>
<parameter key="assetic.helper.static.class">Symfony\Bundle\AsseticBundle\Templating\StaticAsseticHelper</parameter>
<parameter key="assetic.php_formula_loader.class">Symfony\Bundle\AsseticBundle\Factory\Loader\AsseticHelperFormulaLoader</parameter>
</parameters>
<services>
<service id="assetic.helper" class="%assetic.helper.class%">
<tag name="templating.helper" alias="assetic" />
<service id="assetic.helper.dynamic" class="%assetic.helper.dynamic.class%">
<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">
<tag name="assetic.formula_loader" alias="php" />
<tag name="assetic.templating.php" />
@ -23,6 +32,7 @@
<argument type="service" id="assetic.config_cache" />
<argument>%kernel.debug%</argument>
</service>
<service id="assetic.php_formula_loader.real" class="%assetic.php_formula_loader.class%" public="false">
<tag name="assetic.templating.php" />
<argument type="service" id="assetic.asset_factory" />

View File

@ -11,6 +11,7 @@
namespace Symfony\Bundle\AsseticBundle\Templating;
use Assetic\Asset\AssetInterface;
use Assetic\Factory\AssetFactory;
use Symfony\Component\Templating\Helper\Helper;
@ -19,42 +20,30 @@ use Symfony\Component\Templating\Helper\Helper;
*
* @author Kris Wallsmith <kris.wallsmith@symfony.com>
*/
class AsseticHelper extends Helper
abstract class AsseticHelper extends Helper
{
protected $factory;
protected $debug;
protected $defaultJavascriptsOutput;
protected $defaultStylesheetsOutput;
protected $defaultImageOutput;
/**
* Constructor.
*
* @param AssetFactory $factory The asset factory
* @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
* @param AssetFactory $factory The asset factory
* @param Boolean $debug The debug mode
*/
public function __construct(AssetFactory $factory, $debug = false, $defaultJavascriptsOutput = 'js/*.js', $defaultStylesheetsOutput = 'css/*.css', $defaultImageOutput = 'images/*')
public function __construct(AssetFactory $factory, $debug = false)
{
$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;
$options['output'] = 'js/*';
}
return $this->getAssetUrls($inputs, $filters, $options);
@ -62,14 +51,11 @@ class AsseticHelper extends Helper
/**
* 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;
$options['output'] = 'css/*';
}
return $this->getAssetUrls($inputs, $filters, $options);
@ -77,17 +63,16 @@ class AsseticHelper extends Helper
/**
* 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;
$options['output'] = 'images/*';
}
return $this->getAssetUrls($inputs, $filters, $options, true);
$options['single'] = true;
return $this->getAssetUrls($inputs, $filters, $options);
}
/**
@ -105,11 +90,10 @@ class AsseticHelper extends Helper
* @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
*/
private function getAssetUrls($inputs = array(), $filters = array(), array $options = array(), $single = false)
private function getAssetUrls($inputs = array(), $filters = array(), array $options = array())
{
$explode = function($value)
{
@ -128,24 +112,40 @@ class AsseticHelper extends Helper
$options['debug'] = $this->debug;
}
if ($single && 1 < count($inputs)) {
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($coll->getTargetUrl());
return array($this->getAssetUrl($coll, $options));
}
$urls = array();
foreach ($coll as $leaf) {
$urls[] = $leaf->getTargetUrl();
$urls[] = $this->getAssetUrl($leaf, array_replace($options, array(
'name' => $options['name'].'_'.count($urls),
)));
}
return $urls;
}
/**
* Returns an URL for the supplied asset.
*
* @param AssetInterface $asset An asset
* @param array $options An array of options
*
* @return string An echo-ready URL
*/
abstract protected function getAssetUrl(AssetInterface $asset, $options = array());
public function getName()
{
return 'assetic';

View File

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Templating;
use Assetic\Asset\AssetInterface;
use Assetic\Factory\AssetFactory;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\RouterHelper;
/**
* The dynamic "assetic" templating helper.
*
* @author Kris Wallsmith <kris.wallsmith@symfony.com>
*/
class DynamicAsseticHelper extends AsseticHelper
{
private $routerHelper;
/**
* Constructor.
*
* @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)
{
$this->routerHelper = $routerHelper;
parent::__construct($factory, $debug);
}
protected function getAssetUrl(AssetInterface $asset, $options = array())
{
return $this->routerHelper->generate('assetic_'.$options['name']);
}
}

View File

@ -1,44 +0,0 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Templating;
use Assetic\Factory\Loader\FormulaLoaderInterface;
use Assetic\Factory\Resource\ResourceInterface;
/**
* Loads formulae from PHP templates.
*
* @author Kris Wallsmith <kris.wallsmith@symfony.com>
*/
class FormulaLoader implements FormulaLoaderInterface
{
public function load(ResourceInterface $resource)
{
$tokens = token_get_all($resource->getContent());
/**
* @todo Find and extract asset formulae from calls to the following:
*
* * $view['assetic']->assets(...)
* * $view['assetic']->javascripts(...)
* * $view['assetic']->stylesheets(...)
* * $view->get('assetic')->assets(...)
* * $view->get('assetic')->javascripts(...)
* * $view->get('assetic')->stylesheets(...)
*
* The loader will also need to be aware of debug mode and the default
* output strings associated with each method.
*/
return array();
}
}

View File

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Bundle\AsseticBundle\Templating;
use Assetic\Asset\AssetInterface;
use Assetic\Factory\AssetFactory;
use Symfony\Component\Templating\Helper\AssetsHelper;
/**
* The static "assetic" templating helper.
*
* @author Kris Wallsmith <kris.wallsmith@symfony.com>
*/
class StaticAsseticHelper extends AsseticHelper
{
private $assetsHelper;
/**
* Constructor.
*
* @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)
{
$this->assetsHelper = $assetsHelper;
parent::__construct($factory, $debug);
}
protected function getAssetUrl(AssetInterface $asset, $options = array())
{
return $this->assetsHelper->getUrl($asset->getTargetUrl(), isset($options['package']) ? $options['package'] : null);
}
}

View File

@ -16,6 +16,7 @@ use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\CheckYuiFilterPass
use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\CheckClosureFilterPass;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Scope;
use Symfony\Component\Finder\Finder;
@ -53,11 +54,14 @@ class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
$this->container = new ContainerBuilder();
$this->container->addScope(new Scope('request'));
$this->container->register('request', 'Symfony\\Component\\HttpFoundation\\Request')->setScope('request');
$this->container->register('templating.helper.assets', $this->getMockClass('Symfony\\Component\\Templating\\Helper\\AssetsHelper'));
$this->container->register('templating.helper.router', $this->getMockClass('Symfony\\Bundle\\FrameworkBundle\\Templating\\Helper\\RouterHelper'))
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\Routing\\RouterInterface')));
$this->container->register('twig', 'Twig_Environment');
$this->container->setParameter('kernel.bundles', array());
$this->container->setParameter('kernel.cache_dir', __DIR__);
$this->container->setParameter('kernel.debug', false);
$this->container->setParameter('kernel.root_dir', __DIR__);
$this->container->setParameter('kernel.cache_dir', __DIR__);
$this->container->setParameter('kernel.bundles', array());
}
/**

View File

@ -106,7 +106,7 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
{
// totals include assets defined in both php and twig templates
return array(
array(true, 8),
array(true, 6),
array(false, 3),
);
}

View File

@ -11,3 +11,7 @@
<script src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>
<?php $view['slots']->stop() ?>
<?php foreach ($view['assetic']->image('logo.png') as $url): ?>
<img src="<?php echo $view->escape($url) ?>">
<?php endforeach; ?>

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\AsseticBundle\Tests\Templating;
use Assetic\Asset\AssetCollection;
use Assetic\Asset\AssetInterface;
use Assetic\Asset\StringAsset;
use Assetic\Factory\AssetFactory;
use Symfony\Bundle\AsseticBundle\Templating\AsseticHelper;
@ -30,7 +31,7 @@ class AsseticHelperTest extends \PHPUnit_Framework_TestCase
*/
public function testUrls($debug, $count, $message)
{
$helper = new AsseticHelper(new AssetFactory('/foo', $debug), $debug);
$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');
@ -45,3 +46,11 @@ class AsseticHelperTest extends \PHPUnit_Framework_TestCase
);
}
}
class AsseticHelperForTest extends AsseticHelper
{
protected function getAssetUrl(AssetInterface $asset, $options = array())
{
return $asset->getTargetUrl();
}
}