Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: eriwin/assetic
...
head fork: eriwin/assetic
Checking mergeability… Don’t worry, you can still create the pull request.
  • 8 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
19 src/Assetic/Extension/Twig/AsseticExtension.php
@@ -11,18 +11,21 @@
namespace Assetic\Extension\Twig;
-use Assetic\ValueSupplierInterface;
+use Assetic\Cache\ConfigCache;
use Assetic\Factory\AssetFactory;
+use Assetic\ValueSupplierInterface;
class AsseticExtension extends \Twig_Extension
{
protected $factory;
+ protected $cache;
protected $functions;
protected $valueSupplier;
- public function __construct(AssetFactory $factory, $functions = array(), ValueSupplierInterface $valueSupplier = null)
+ public function __construct(AssetFactory $factory, ConfigCache $cache, $functions = array(), ValueSupplierInterface $valueSupplier = null)
{
$this->factory = $factory;
+ $this->cache = $cache;
$this->functions = array();
$this->valueSupplier = $valueSupplier;
@@ -54,6 +57,13 @@ public function getFunctions()
return $functions;
}
+ public function getNodeVisitors()
+ {
+ return array(
+ new AsseticNodeVisitor($this->cache),
+ );
+ }
+
public function getGlobals()
{
return array(
@@ -69,6 +79,11 @@ public function getFilterInvoker($function)
return new AsseticFilterInvoker($this->factory, $this->functions[$function]);
}
+ public function getConfigCache()
+ {
+ return $this->cache;
+ }
+
public function getName()
{
return 'assetic';
View
86 src/Assetic/Extension/Twig/AsseticNodeVisitor.php
@@ -0,0 +1,86 @@
+<?php
+
+/*
+ * This file is part of the Assetic package, an OpenSky project.
+ *
+ * (c) 2010-2012 OpenSky Project Inc
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Assetic\Extension\Twig;
+
+use Assetic\Cache\ConfigCache;
+
+/**
+ * Watches for Assetic nodes and stores formulae to a cache.
+ */
+class AsseticNodeVisitor implements \Twig_NodeVisitorInterface
+{
+ private $cache;
+ private $formulae;
+
+ public function __construct(ConfigCache $cache)
+ {
+ $this->cache = $cache;
+ }
+
+ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
+ {
+ if ($node instanceof \Twig_Node_Module) {
+ $this->formulae = array();
+ } elseif ($node instanceof AsseticNode) {
+ $this->formulae[$node->getAttribute('name')] = array(
+ $node->getAttribute('inputs'),
+ $node->getAttribute('filters'),
+ array(
+ 'output' => $node->getAttribute('asset')->getTargetPath(),
+ 'name' => $node->getAttribute('name'),
+ 'debug' => $node->getAttribute('debug'),
+ 'combine' => $node->getAttribute('combine'),
+ 'vars' => $node->getAttribute('vars'),
+ ),
+ );
+ } elseif ($node instanceof \Twig_Node_Expression_Function) {
+ $name = version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<')
+ ? $node->getNode('name')->getAttribute('name')
+ : $node->getAttribute('name');
+
+ if ($env->getFunction($name) instanceof AsseticFilterFunction) {
+ $arguments = array();
+ foreach ($node->getNode('arguments') as $argument) {
+ $arguments[] = eval('return '.$env->compile($argument).';');
+ }
+
+ $invoker = $env->getExtension('assetic')->getFilterInvoker($name);
+
+ $inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
+ $filters = $invoker->getFilters();
+ $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
+
+ if (!isset($options['name'])) {
+ $options['name'] = $invoker->getFactory()->generateAssetName($inputs, $filters, $options);
+ }
+
+ $this->formulae[$options['name']] = array($inputs, $filters, $options);
+ }
+ }
+
+ return $node;
+ }
+
+ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
+ {
+ if ($node instanceof \Twig_Node_Module) {
+ $this->cache->set($node->getAttribute('filename'), $this->formulae);
+ }
+
+ return $node;
+ }
+
+ public function getPriority()
+ {
+ return 0;
+ }
+}
View
88 src/Assetic/Extension/Twig/TwigFormulaLoader.php
@@ -11,7 +11,9 @@
namespace Assetic\Extension\Twig;
+use Assetic\Cache\ConfigCache;
use Assetic\Factory\Loader\FormulaLoaderInterface;
+use Assetic\Factory\Resource\IteratorResourceInterface;
use Assetic\Factory\Resource\ResourceInterface;
/**
@@ -28,72 +30,42 @@ public function __construct(\Twig_Environment $twig)
$this->twig = $twig;
}
- public function load(ResourceInterface $resource)
+ public function load(ResourceInterface $resources)
{
- try {
- $tokens = $this->twig->tokenize($resource->getContent(), (string) $resource);
- $nodes = $this->twig->parse($tokens);
- } catch (\Exception $e) {
- return array();
- }
+ $cache = $this->twig->getExtension('assetic')->getConfigCache();
- return $this->loadNode($nodes);
- }
+ if (!$resources instanceof IteratorResourceInterface) {
+ $resources = array($resources);
+ }
- /**
- * Loads assets from the supplied node.
- *
- * @param \Twig_Node $node
- *
- * @return array An array of asset formulae indexed by name
- */
- private function loadNode(\Twig_Node $node)
- {
$formulae = array();
-
- if ($node instanceof AsseticNode) {
- $formulae[$node->getAttribute('name')] = array(
- $node->getAttribute('inputs'),
- $node->getAttribute('filters'),
- array(
- 'output' => $node->getAttribute('asset')->getTargetPath(),
- 'name' => $node->getAttribute('name'),
- 'debug' => $node->getAttribute('debug'),
- 'combine' => $node->getAttribute('combine'),
- 'vars' => $node->getAttribute('vars'),
- ),
- );
- } elseif ($node instanceof \Twig_Node_Expression_Function) {
- $name = version_compare(\Twig_Environment::VERSION, '1.2.0-DEV', '<')
- ? $node->getNode('name')->getAttribute('name')
- : $node->getAttribute('name');
-
- if ($this->twig->getFunction($name) instanceof AsseticFilterFunction) {
- $arguments = array();
- foreach ($node->getNode('arguments') as $argument) {
- $arguments[] = eval('return '.$this->twig->compile($argument).';');
- }
-
- $invoker = $this->twig->getExtension('assetic')->getFilterInvoker($name);
-
- $inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
- $filters = $invoker->getFilters();
- $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());
-
- if (!isset($options['name'])) {
- $options['name'] = $invoker->getFactory()->generateAssetName($inputs, $filters, $options);
- }
-
- $formulae[$options['name']] = array($inputs, $filters, $options);
+ foreach ($resources as $resource) {
+ $name = (string) $resource;
+
+ try {
+ $this->loadTemplate($name, $cache);
+ } catch (\Exception $e) {
+ // ignore twig errors (none of our business)
+ continue;
}
- }
- foreach ($node as $child) {
- if ($child instanceof \Twig_Node) {
- $formulae += $this->loadNode($child);
- }
+ // fetch the formulae from the config cache
+ $formulae += $cache->get($name);
}
return $formulae;
}
+
+ private function loadTemplate($name, ConfigCache $cache)
+ {
+ // load the template to ensure what's in the cache is fresh
+ $this->twig->loadTemplate($name);
+
+ // force a parse if necessary
+ if (!$cache->has($name)) {
+ $source = $this->twig->getLoader()->getSource($name);
+ $tokens = $this->twig->tokenize($source, $name);
+ $nodes = $this->twig->parse($tokens);
+ }
+ }
}
View
16 tests/Assetic/Test/Extension/Twig/AsseticExtensionTest.php
@@ -11,8 +11,9 @@
namespace Assetic\Test\Extension\Twig;
-use Assetic\Factory\AssetFactory;
+use Assetic\Cache\ConfigCache;
use Assetic\Extension\Twig\AsseticExtension;
+use Assetic\Factory\AssetFactory;
class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
{
@@ -32,6 +33,11 @@ class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
private $factory;
/**
+ * @var ConfigCache
+ */
+ private $cache;
+
+ /**
* @var \Twig_Environment
*/
private $twig;
@@ -56,9 +62,13 @@ protected function setUp()
$this->factory->setAssetManager($this->am);
$this->factory->setFilterManager($this->fm);
+ $this->cache = $this->getMockBuilder('Assetic\Cache\ConfigCache')
+ ->disableOriginalConstructor()
+ ->getMock();
+
$this->twig = new \Twig_Environment();
$this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__.'/templates'));
- $this->twig->addExtension(new AsseticExtension($this->factory, array(), $this->valueSupplier));
+ $this->twig->addExtension(new AsseticExtension($this->factory, $this->cache, array(), $this->valueSupplier));
}
public function testReference()
@@ -193,7 +203,7 @@ public function testFilterFunction()
->with('some_filter')
->will($this->returnValue($filter));
- $this->twig->addExtension(new AsseticExtension($this->factory, array(
+ $this->twig->addExtension(new AsseticExtension($this->factory, $this->cache, array(
'some_func' => array(
'filter' => 'some_filter',
'options' => array('output' => 'css/*.css'),
View
36 tests/Assetic/Test/Extension/Twig/TwigFormulaLoaderTest.php
@@ -11,9 +11,10 @@
namespace Assetic\Test\Extension\Twig;
-use Assetic\Factory\AssetFactory;
+use Assetic\Cache\ConfigCache;
use Assetic\Extension\Twig\AsseticExtension;
use Assetic\Extension\Twig\TwigFormulaLoader;
+use Assetic\Factory\AssetFactory;
class TwigFormulaLoaderTest extends \PHPUnit_Framework_TestCase
{
@@ -34,15 +35,15 @@ protected function setUp()
$factory->setAssetManager($this->am);
$factory->setFilterManager($this->fm);
- $twig = new \Twig_Environment();
- $twig->addExtension(new AsseticExtension($factory, array(
+ $this->twig = new \Twig_Environment();
+ $this->twig->addExtension(new AsseticExtension($factory, new ConfigCache(sys_get_temp_dir().'/assetic'), array(
'some_func' => array(
'filter' => 'some_filter',
'options' => array('output' => 'css/*.css'),
),
)));
- $this->loader = new TwigFormulaLoader($twig);
+ $this->loader = new TwigFormulaLoader($this->twig);
}
public function testMixture()
@@ -63,10 +64,14 @@ public function testMixture()
),
);
+ $this->twig->setLoader(new \Twig_Loader_Array(array(
+ 'mixture.twig' => file_get_contents(__DIR__.'/templates/mixture.twig'),
+ )));
+
$resource = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
$resource->expects($this->once())
- ->method('getContent')
- ->will($this->returnValue(file_get_contents(__DIR__.'/templates/mixture.twig')));
+ ->method('__toString')
+ ->will($this->returnValue('mixture.twig'));
$this->am->expects($this->any())
->method('get')
->with('foo')
@@ -86,23 +91,16 @@ public function testFunction()
),
);
- $resource = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
- $resource->expects($this->once())
- ->method('getContent')
- ->will($this->returnValue(file_get_contents(__DIR__.'/templates/function.twig')));
-
- $formulae = $this->loader->load($resource);
- $this->assertEquals($expected, $formulae);
- }
+ $this->twig->setLoader(new \Twig_Loader_Array(array(
+ 'function.twig' => file_get_contents(__DIR__.'/templates/function.twig'),
+ )));
- public function testUnclosedTag()
- {
$resource = $this->getMock('Assetic\\Factory\\Resource\\ResourceInterface');
$resource->expects($this->once())
- ->method('getContent')
- ->will($this->returnValue(file_get_contents(__DIR__.'/templates/unclosed_tag.twig')));
+ ->method('__toString')
+ ->will($this->returnValue('function.twig'));
$formulae = $this->loader->load($resource);
- $this->assertEquals(array(), $formulae);
+ $this->assertEquals($expected, $formulae);
}
}
View
5 tests/Assetic/Test/Extension/Twig/templates/unclosed_tag.twig
@@ -1,5 +0,0 @@
-<assets>
-{% block assets %}
-{% stylesheets 'foo.css' 'bar.css' filter='?foo,bar' output='css/packed.css' debug=true %}<asset url="{{ asset_url }}" />
-{% endblock %}
-</assets>

No commit comments for this range

Something went wrong with that request. Please try again.