diff --git a/composer.json b/composer.json index 046bc5c..27fc1cb 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "psr/log": "^1.0", "ptachoire/cssembed": "^1.0", "symfony/phpunit-bridge": "^3.4 || ^4.0 || ^5.0 || ^6.0 || ^7.0", - "twig/twig": "^2.11", + "twig/twig": "^2.11 || ^3.0", "phpspec/prophecy-phpunit": "^2.0" }, "suggest": { diff --git a/src/Assetic/Extension/Twig/AsseticNode.php b/src/Assetic/Extension/Twig/AsseticNode.php index 711fc69..041361c 100644 --- a/src/Assetic/Extension/Twig/AsseticNode.php +++ b/src/Assetic/Extension/Twig/AsseticNode.php @@ -3,9 +3,12 @@ namespace Assetic\Extension\Twig; use Assetic\Contracts\Asset\AssetInterface; +use Twig\Attribute\YieldReady; use Twig\Compiler; +use Twig\Environment; use Twig\Node\Node; +#[YieldReady] class AsseticNode extends Node { /** @@ -36,7 +39,14 @@ public function __construct(AssetInterface $asset, Node $body, array $inputs, ar array('asset' => $asset, 'inputs' => $inputs, 'filters' => $filters, 'name' => $name) ); - parent::__construct($nodes, $attributes, $lineno, $tag); + // Twig 3.12 deprecated passing the node tag to the constructor; the + // Parser now sets it automatically. Twig 2.x and Twig < 3.12 still + // expect it to be supplied here. + if (version_compare(Environment::VERSION, '3.12.0', '>=')) { + parent::__construct($nodes, $attributes, $lineno); + } else { + parent::__construct($nodes, $attributes, $lineno, $tag); + } } public function compile(Compiler $compiler) diff --git a/src/Assetic/Extension/Twig/AsseticTokenParser.php b/src/Assetic/Extension/Twig/AsseticTokenParser.php index 9e9bfd4..385bcde 100644 --- a/src/Assetic/Extension/Twig/AsseticTokenParser.php +++ b/src/Assetic/Extension/Twig/AsseticTokenParser.php @@ -88,7 +88,13 @@ public function parse(Token $token) // vars=['locale','browser'] $stream->next(); $stream->expect(Token::OPERATOR_TYPE, '='); - $stream->expect(Token::PUNCTUATION_TYPE, '['); + + // Twig 2 lexes the opening "[" as punctuation, Twig 3 as an operator. + if ($stream->test(Token::OPERATOR_TYPE, '[')) { + $stream->next(); + } else { + $stream->expect(Token::PUNCTUATION_TYPE, '['); + } while ($stream->test(Token::STRING_TYPE)) { $attributes['vars'][] = $stream->expect(Token::STRING_TYPE)->getValue(); diff --git a/tests/Assetic/Test/Extension/Twig/AsseticExtensionTest.php b/tests/Assetic/Test/Extension/Twig/AsseticExtensionTest.php index 3be2e22..a36a075 100644 --- a/tests/Assetic/Test/Extension/Twig/AsseticExtensionTest.php +++ b/tests/Assetic/Test/Extension/Twig/AsseticExtensionTest.php @@ -254,6 +254,6 @@ public function testUnclosedTag() private function renderXml($name, $context = []) { - return new \SimpleXMLElement($this->twig->loadTemplate($name)->render($context)); + return new \SimpleXMLElement($this->twig->load($name)->render($context)); } }