Skip to content

Commit

Permalink
Merge pull request #751 from stof/twig_2
Browse files Browse the repository at this point in the history
Make the Twig integration compatible with Twig 2.0
  • Loading branch information
stof committed Aug 29, 2015
2 parents 35b3281 + 5210a38 commit 2aedd85
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ matrix:
- php: 7.0
include:
- php: 5.3
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable' SYMFONY_DEPRECATIONS_HELPER=weak
- php: 5.6
env: DEPENDENCIES=dev

Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
"php": ">=5.3.1",
"symfony/process": "~2.1"
},
"conflict": {
"twig/twig": "<1.12"
},
"require-dev": {
"phpunit/phpunit": "~4",
"twig/twig": "~1.8",
"phpunit/phpunit": "~4.8",
"symfony/phpunit-bridge": "~2.7",
"twig/twig": "~1.8|~2.0",
"leafo/lessphp": "^0.3.7",
"leafo/scssphp": "*@dev",
"ptachoire/cssembed": "*",
Expand Down
2 changes: 1 addition & 1 deletion src/Assetic/Extension/Twig/AsseticExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getFunctions()
{
$functions = array();
foreach ($this->functions as $function => $filter) {
$functions[$function] = new AsseticFilterFunction($function);
$functions[] = new AsseticFilterFunction($function);
}

return $functions;
Expand Down
19 changes: 7 additions & 12 deletions src/Assetic/Extension/Twig/AsseticFilterFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@

namespace Assetic\Extension\Twig;

class AsseticFilterFunction extends \Twig_Function
class AsseticFilterFunction extends \Twig_SimpleFunction
{
private $filter;

public function __construct($filter, $options = array())
{
$this->filter = $filter;

parent::__construct($options);
}

public function compile()
public function __construct($name, $options = array())
{
return sprintf('$this->env->getExtension(\'assetic\')->getFilterInvoker(\'%s\')->invoke', $this->filter);
parent::__construct($name, '\Assetic\Extension\Twig\AsseticFilterInvoker::invoke', array_merge($options, array(
'needs_environment' => false,
'needs_context' => false,
'node_class' => '\Assetic\Extension\Twig\AsseticFilterNode',
)));
}
}
22 changes: 22 additions & 0 deletions src/Assetic/Extension/Twig/AsseticFilterNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Assetic package, an OpenSky project.
*
* (c) 2010-2014 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;

class AsseticFilterNode extends \Twig_Node_Expression_Function
{
protected function compileCallable(\Twig_Compiler $compiler)
{
$compiler->raw(sprintf('$this->env->getExtension(\'assetic\')->getFilterInvoker(\'%s\')->invoke', $this->getAttribute('name')));

$this->compileArguments($compiler);
}
}
18 changes: 9 additions & 9 deletions src/Assetic/Extension/Twig/AsseticNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ class AsseticNode extends \Twig_Node
* * combine: Whether to combine assets
* * var_name: The name of the variable to expose to the body node
*
* @param AssetInterface $asset The asset
* @param \Twig_NodeInterface $body The body node
* @param array $inputs An array of input strings
* @param array $filters An array of filter strings
* @param string $name The name of the asset
* @param array $attributes An array of attributes
* @param integer $lineno The line number
* @param string $tag The tag name
* @param AssetInterface $asset The asset
* @param \Twig_Node $body The body node
* @param array $inputs An array of input strings
* @param array $filters An array of filter strings
* @param string $name The name of the asset
* @param array $attributes An array of attributes
* @param integer $lineno The line number
* @param string $tag The tag name
*/
public function __construct(AssetInterface $asset, \Twig_NodeInterface $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null)
public function __construct(AssetInterface $asset, \Twig_Node $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null)
{
$nodes = array('body' => $body);

Expand Down
49 changes: 47 additions & 2 deletions src/Assetic/Extension/Twig/AsseticTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function parse(\Twig_Token $token)
$attributes[$key] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
} else {
$token = $stream->getCurrent();
throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getFilename());
}
}

Expand All @@ -133,7 +133,7 @@ public function parse(\Twig_Token $token)

$asset = $this->factory->createAsset($inputs, $filters, $attributes + array('name' => $name));

return $this->createNode($asset, $body, $inputs, $filters, $name, $attributes, $token->getLine(), $this->getTag());
return $this->createBodyNode($asset, $body, $inputs, $filters, $name, $attributes, $token->getLine(), $this->getTag());
}

public function getTag()
Expand All @@ -146,8 +146,53 @@ public function testEndTag(\Twig_Token $token)
return $token->test(array('end'.$this->getTag()));
}

/**
* @param AssetInterface $asset
* @param \Twig_Node $body
* @param array $inputs
* @param array $filters
* @param string $name
* @param array $attributes
* @param int $lineno
* @param string $tag
*
* @return \Twig_Node
*/
protected function createBodyNode(AssetInterface $asset, \Twig_Node $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null)
{
$reflector = new \ReflectionMethod($this, 'createNode');

if (__CLASS__ !== $reflector->getDeclaringClass()->name) {
@trigger_error(sprintf('Overwriting %s::createNode is deprecated since 1.3. Overwrite %s instead.', __CLASS__, __METHOD__), E_USER_DEPRECATED);

return $this->createNode($asset, $body, $inputs, $filters, $name, $attributes, $lineno, $tag);
}

return new AsseticNode($asset, $body, $inputs, $filters, $name, $attributes, $lineno, $tag);
}

/**
* @param AssetInterface $asset
* @param \Twig_NodeInterface $body
* @param array $inputs
* @param array $filters
* @param string $name
* @param array $attributes
* @param int $lineno
* @param string $tag
*
* @return \Twig_Node
*
* @deprecated since 1.3.0, to be removed in 2.0. Use createBodyNode instead.
*/
protected function createNode(AssetInterface $asset, \Twig_NodeInterface $body, array $inputs, array $filters, $name, array $attributes = array(), $lineno = 0, $tag = null)
{
@trigger_error(sprintf('The %s method is deprecated since 1.3 and will be removed in 2.0. Use createBodyNode instead.', __METHOD__), E_USER_DEPRECATED);

if (!$body instanceof \Twig_Node) {
throw new \InvalidArgumentException('The body must be a Twig_Node. Custom implementations of Twig_NodeInterface are not supported.');
}

return new AsseticNode($asset, $body, $inputs, $filters, $name, $attributes, $lineno, $tag);
}
}
4 changes: 1 addition & 3 deletions src/Assetic/Extension/Twig/TwigFormulaLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ private function loadNode(\Twig_Node $node)
),
);
} 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');
$name = $node->getAttribute('name');

if ($this->twig->getFunction($name) instanceof AsseticFilterFunction) {
$arguments = array();
Expand Down
3 changes: 1 addition & 2 deletions tests/Assetic/Test/Extension/Twig/AsseticExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ protected function setUp()
$this->factory->setAssetManager($this->am);
$this->factory->setFilterManager($this->fm);

$this->twig = new \Twig_Environment();
$this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__.'/templates'));
$this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(__DIR__.'/templates'));
$this->twig->addExtension(new AsseticExtension($this->factory, array(), $this->valueSupplier));
}

Expand Down
6 changes: 5 additions & 1 deletion tests/Assetic/Test/Extension/Twig/TwigFormulaLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class TwigFormulaLoaderTest extends \PHPUnit_Framework_TestCase
{
private $am;
private $fm;
/**
* @var TwigFormulaLoader
*/
private $loader;

protected function setUp()
{
Expand All @@ -33,7 +37,7 @@ protected function setUp()
$factory->setAssetManager($this->am);
$factory->setFilterManager($this->fm);

$twig = new \Twig_Environment();
$twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'));
$twig->addExtension(new AsseticExtension($factory, array(
'some_func' => array(
'filter' => 'some_filter',
Expand Down

0 comments on commit 2aedd85

Please sign in to comment.