Browse code

moved tests handling in Twig_ParserExpression instead of Twig_Extension_Core

Fabien Potencier authored on 24/10/2016 22:21:07
Showing 2 changed files
... ...
@@ -44,7 +44,11 @@ class Twig_ExpressionParser
44 44
             $op = $this->binaryOperators[$token->getValue()];
45 45
             $this->parser->getStream()->next();
46 46
 
47
-            if (isset($op['callable'])) {
47
+            if ('is not' === $token->getValue()) {
48
+                $expr = $this->parseNotTestExpression($expr);
49
+            } elseif ('is' === $token->getValue()) {
50
+                $expr = $this->parseTestExpression($expr);
51
+            } elseif (isset($op['callable'])) {
48 52
                 $expr = call_user_func($op['callable'], $this->parser, $expr);
49 53
             } else {
50 54
                 $expr1 = $this->parseExpression(self::OPERATOR_LEFT === $op['associativity'] ? $op['precedence'] + 1 : $op['precedence']);
... ...
@@ -567,6 +571,74 @@ class Twig_ExpressionParser
567 571
         return new Twig_Node($targets);
568 572
     }
569 573
 
574
+    private function parseNotTestExpression(Twig_NodeInterface $node)
575
+    {
576
+        return new Twig_Node_Expression_Unary_Not($this->parseTestExpression($node), $this->parser->getCurrentToken()->getLine());
577
+    }
578
+
579
+    private function parseTestExpression(Twig_NodeInterface $node)
580
+    {
581
+        $stream = $this->parser->getStream();
582
+        list($name, $test) = $this->getTest($node->getTemplateLine());
583
+
584
+        $class = $this->getTestNodeClass($test);
585
+        $arguments = null;
586
+        if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
587
+            $arguments = $this->parser->getExpressionParser()->parseArguments(true);
588
+        }
589
+
590
+        return new $class($node, $name, $arguments, $this->parser->getCurrentToken()->getLine());
591
+    }
592
+
593
+    private function getTest($line)
594
+    {
595
+        $stream = $this->parser->getStream();
596
+        $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
597
+        $env = $this->parser->getEnvironment();
598
+
599
+        if ($test = $env->getTest($name)) {
600
+            return array($name, $test);
601
+        }
602
+
603
+        if ($stream->test(Twig_Token::NAME_TYPE)) {
604
+            // try 2-words tests
605
+            $name = $name.' '.$this->parser->getCurrentToken()->getValue();
606
+
607
+            if ($test = $env->getTest($name)) {
608
+                $stream->next();
609
+
610
+                return array($name, $test);
611
+            }
612
+        }
613
+
614
+        $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $stream->getSourceContext()->getName());
615
+        $e->addSuggestions($name, array_keys($env->getTests()));
616
+
617
+        throw $e;
618
+    }
619
+
620
+    private function getTestNodeClass($test)
621
+    {
622
+        if ($test instanceof Twig_SimpleTest && $test->isDeprecated()) {
623
+            $message = sprintf('Twig Test "%s" is deprecated', $name);
624
+            if (!is_bool($test->getDeprecatedVersion())) {
625
+                $message .= sprintf(' since version %s', $test->getDeprecatedVersion());
626
+            }
627
+            if ($test->getAlternative()) {
628
+                $message .= sprintf('. Use "%s" instead', $test->getAlternative());
629
+            }
630
+            $message .= sprintf(' in %s at line %d.', $stream->getSourceContext()->getName(), $stream->getCurrent()->getLine());
631
+
632
+            @trigger_error($message, E_USER_DEPRECATED);
633
+        }
634
+
635
+        if ($test instanceof Twig_SimpleTest) {
636
+            return $test->getNodeClass();
637
+        }
638
+
639
+        return $test instanceof Twig_Test_Node ? $test->getClass() : 'Twig_Node_Expression_Test';
640
+    }
641
+
570 642
     protected function getFunctionNodeClass($name, $line)
571 643
     {
572 644
         $env = $this->parser->getEnvironment();
... ...
@@ -258,82 +258,14 @@ class Twig_Extension_Core extends Twig_Extension
258 258
                 '/' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Div', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
259 259
                 '//' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_FloorDiv', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
260 260
                 '%' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mod', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
261
-                'is' => array('precedence' => 100, 'callable' => array($this, 'parseTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
262
-                'is not' => array('precedence' => 100, 'callable' => array($this, 'parseNotTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
261
+                'is' => array('precedence' => 100, 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
262
+                'is not' => array('precedence' => 100, 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
263 263
                 '**' => array('precedence' => 200, 'class' => 'Twig_Node_Expression_Binary_Power', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
264 264
                 '??' => array('precedence' => 300, 'class' => 'Twig_Node_Expression_NullCoalesce', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
265 265
             ),
266 266
         );
267 267
     }
268 268
 
269
-    public function parseNotTestExpression(Twig_Parser $parser, Twig_NodeInterface $node)
270
-    {
271
-        return new Twig_Node_Expression_Unary_Not($this->parseTestExpression($parser, $node), $parser->getCurrentToken()->getLine());
272
-    }
273
-
274
-    public function parseTestExpression(Twig_Parser $parser, Twig_NodeInterface $node)
275
-    {
276
-        $stream = $parser->getStream();
277
-        list($name, $test) = $this->getTest($parser, $node->getTemplateLine());
278
-
279
-        if ($test instanceof Twig_SimpleTest && $test->isDeprecated()) {
280
-            $message = sprintf('Twig Test "%s" is deprecated', $name);
281
-            if (!is_bool($test->getDeprecatedVersion())) {
282
-                $message .= sprintf(' since version %s', $test->getDeprecatedVersion());
283
-            }
284
-            if ($test->getAlternative()) {
285
-                $message .= sprintf('. Use "%s" instead', $test->getAlternative());
286
-            }
287
-            $message .= sprintf(' in %s at line %d.', $stream->getSourceContext()->getName(), $stream->getCurrent()->getLine());
288
-
289
-            @trigger_error($message, E_USER_DEPRECATED);
290
-        }
291
-
292
-        $class = $this->getTestNodeClass($parser, $test);
293
-        $arguments = null;
294
-        if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
295
-            $arguments = $parser->getExpressionParser()->parseArguments(true);
296
-        }
297
-
298
-        return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
299
-    }
300
-
301
-    protected function getTest(Twig_Parser $parser, $line)
302
-    {
303
-        $stream = $parser->getStream();
304
-        $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
305
-        $env = $parser->getEnvironment();
306
-
307
-        if ($test = $env->getTest($name)) {
308
-            return array($name, $test);
309
-        }
310
-
311
-        if ($stream->test(Twig_Token::NAME_TYPE)) {
312
-            // try 2-words tests
313
-            $name = $name.' '.$parser->getCurrentToken()->getValue();
314
-
315
-            if ($test = $env->getTest($name)) {
316
-                $parser->getStream()->next();
317
-
318
-                return array($name, $test);
319
-            }
320
-        }
321
-
322
-        $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $stream->getSourceContext()->getName());
323
-        $e->addSuggestions($name, array_keys($env->getTests()));
324
-
325
-        throw $e;
326
-    }
327
-
328
-    protected function getTestNodeClass(Twig_Parser $parser, $test)
329
-    {
330
-        if ($test instanceof Twig_SimpleTest) {
331
-            return $test->getNodeClass();
332
-        }
333
-
334
-        return $test instanceof Twig_Test_Node ? $test->getClass() : 'Twig_Node_Expression_Test';
335
-    }
336
-
337 269
     public function getName()
338 270
     {
339 271
         return 'core';