From: Fabien Potencier Date: Tue, 15 Oct 2013 16:27:17 +0000 (+0200) Subject: made it possible to use named operators as variables X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=8d805aacb8f23cdf8ff7c91c4c6f7d16e04f3c3c;p=konrad%2Ftwig.git made it possible to use named operators as variables --- diff --git a/CHANGELOG b/CHANGELOG index e3c92c2..cb52fa6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.14.1 (2013-XX-XX) + * made it possible to use named operators as variables * fixed the possibility to have a variable named 'matches' * added support for PHP 5.5 DateTimeInterface diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 67d3337..96f9224 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -162,9 +162,10 @@ class Twig_ExpressionParser break; case Twig_Token::OPERATOR_TYPE: - if ('matches' == $token->getValue()) { + if (preg_match(Twig_Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) { + // in this context, string operators are variable names $this->parser->getStream()->next(); - $node = new Twig_Node_Expression_Name('matches', $token->getLine()); + $node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine()); break; } diff --git a/test/Twig/Tests/Fixtures/expressions/operators_as_variables.test b/test/Twig/Tests/Fixtures/expressions/operators_as_variables.test new file mode 100644 index 0000000..fe29d08 --- /dev/null +++ b/test/Twig/Tests/Fixtures/expressions/operators_as_variables.test @@ -0,0 +1,16 @@ +--TEST-- +Twig allows to use named operators as variable names +--TEMPLATE-- +{% for match in matches %} + {{- match }} +{% endfor %} +{{ in }} +{{ is }} +--DATA-- +return array('matches' => array(1, 2, 3), 'in' => 'in', 'is' => 'is') +--EXPECT-- +1 +2 +3 +in +is diff --git a/test/Twig/Tests/Fixtures/expressions/two_word_operators_as_variables.test b/test/Twig/Tests/Fixtures/expressions/two_word_operators_as_variables.test new file mode 100644 index 0000000..47f37e4 --- /dev/null +++ b/test/Twig/Tests/Fixtures/expressions/two_word_operators_as_variables.test @@ -0,0 +1,8 @@ +--TEST-- +Twig does not allow to use two-word named operators as variable names +--TEMPLATE-- +{{ starts with }} +--DATA-- +return array() +--EXCEPTION-- +Twig_Error_Syntax: Unexpected token "operator" of value "starts with" in "index.twig" at line 2 diff --git a/test/Twig/Tests/Fixtures/regression/matches_as_variable.test b/test/Twig/Tests/Fixtures/regression/matches_as_variable.test deleted file mode 100644 index 00ca200..0000000 --- a/test/Twig/Tests/Fixtures/regression/matches_as_variable.test +++ /dev/null @@ -1,12 +0,0 @@ ---TEST-- -Twig allows to use 'matches' for a variable name ---TEMPLATE-- -{% for match in matches %} - {{- match }} -{% endfor %} ---DATA-- -return array('matches' => array(1, 2, 3)) ---EXPECT-- -1 -2 -3