Browse code

deprecated Twig_Parser::getEnvironment()

Fabien Potencier authored on 24/10/2016 23:11:10
Showing 4 changed files
... ...
@@ -1,5 +1,6 @@
1 1
 * 1.27.0 (2016-XX-XX)
2 2
 
3
+ * deprecated Twig_Parser::getEnvironment()
3 4
  * deprecated Twig_Parser::addHandler() and Twig_Parser::addNodeVisitor()
4 5
  * deprecated Twig_Compiler::addIndentation()
5 6
  * fixed regression when registering two extensions having the same class name
... ...
@@ -23,6 +23,8 @@ Token Parsers
23 23
 * As of Twig 1.27, ``Twig_Parser::getFilename()`` is deprecated. From a token
24 24
   parser, use ``$this->parser->getStream()->getSourceContext()->getPath()`` instead.
25 25
 
26
+* As of Twig 1.27, ``Twig_Parser::getEnvironment()`` is deprecated.
27
+
26 28
 Extensions
27 29
 ----------
28 30
 
... ...
@@ -19,6 +19,8 @@
19 19
  * @see http://en.wikipedia.org/wiki/Operator-precedence_parser
20 20
  *
21 21
  * @author Fabien Potencier <fabien@symfony.com>
22
+ *
23
+ * @internal
22 24
  */
23 25
 class Twig_ExpressionParser
24 26
 {
... ...
@@ -29,11 +31,23 @@ class Twig_ExpressionParser
29 31
     protected $unaryOperators;
30 32
     protected $binaryOperators;
31 33
 
32
-    public function __construct(Twig_Parser $parser, array $unaryOperators, array $binaryOperators)
34
+    private $env;
35
+
36
+    public function __construct(Twig_Parser $parser, Twig_Environment $env = null)
33 37
     {
34 38
         $this->parser = $parser;
35
-        $this->unaryOperators = $unaryOperators;
36
-        $this->binaryOperators = $binaryOperators;
39
+
40
+        if ($env instanceof Twig_Environment) {
41
+            $this->env = $env;
42
+            $this->unaryOperators = $env->getUnaryOperators();
43
+            $this->binaryOperators = $env->getBinaryOperators();
44
+        } else {
45
+            @trigger_error('Passing the operators as constructor arguments to '.__METHOD__.' is deprecated since version 1.27. Pass the environment instead.', E_USER_DEPRECATED);
46
+
47
+            $this->env = $parser->getEnvironment();
48
+            $this->unaryOperators = func_get_arg(1);
49
+            $this->binaryOperators = func_get_arg(2);
50
+        }
37 51
     }
38 52
 
39 53
     public function parseExpression($precedence = 0)
... ...
@@ -594,9 +608,8 @@ class Twig_ExpressionParser
594 608
     {
595 609
         $stream = $this->parser->getStream();
596 610
         $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
597
-        $env = $this->parser->getEnvironment();
598 611
 
599
-        if ($test = $env->getTest($name)) {
612
+        if ($test = $this->env->getTest($name)) {
600 613
             return array($name, $test);
601 614
         }
602 615
 
... ...
@@ -604,7 +617,7 @@ class Twig_ExpressionParser
604 617
             // try 2-words tests
605 618
             $name = $name.' '.$this->parser->getCurrentToken()->getValue();
606 619
 
607
-            if ($test = $env->getTest($name)) {
620
+            if ($test = $this->env->getTest($name)) {
608 621
                 $stream->next();
609 622
 
610 623
                 return array($name, $test);
... ...
@@ -612,7 +625,7 @@ class Twig_ExpressionParser
612 625
         }
613 626
 
614 627
         $e = new Twig_Error_Syntax(sprintf('Unknown "%s" test.', $name), $line, $stream->getSourceContext()->getName());
615
-        $e->addSuggestions($name, array_keys($env->getTests()));
628
+        $e->addSuggestions($name, array_keys($this->env->getTests()));
616 629
 
617 630
         throw $e;
618 631
     }
... ...
@@ -641,11 +654,9 @@ class Twig_ExpressionParser
641 654
 
642 655
     protected function getFunctionNodeClass($name, $line)
643 656
     {
644
-        $env = $this->parser->getEnvironment();
645
-
646
-        if (false === $function = $env->getFunction($name)) {
657
+        if (false === $function = $this->env->getFunction($name)) {
647 658
             $e = new Twig_Error_Syntax(sprintf('Unknown "%s" function.', $name), $line, $this->parser->getStream()->getSourceContext()->getName());
648
-            $e->addSuggestions($name, array_keys($env->getFunctions()));
659
+            $e->addSuggestions($name, array_keys($this->env->getFunctions()));
649 660
 
650 661
             throw $e;
651 662
         }
... ...
@@ -672,11 +683,9 @@ class Twig_ExpressionParser
672 683
 
673 684
     protected function getFilterNodeClass($name, $line)
674 685
     {
675
-        $env = $this->parser->getEnvironment();
676
-
677
-        if (false === $filter = $env->getFilter($name)) {
686
+        if (false === $filter = $this->env->getFilter($name)) {
678 687
             $e = new Twig_Error_Syntax(sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getStream()->getSourceContext()->getName());
679
-            $e->addSuggestions($name, array_keys($env->getFilters()));
688
+            $e->addSuggestions($name, array_keys($this->env->getFilters()));
680 689
 
681 690
             throw $e;
682 691
         }
... ...
@@ -42,8 +42,13 @@ class Twig_Parser implements Twig_ParserInterface
42 42
         $this->env = $env;
43 43
     }
44 44
 
45
+    /**
46
+     * @deprecated since 1.27 (to be removed in 2.0)
47
+     */
45 48
     public function getEnvironment()
46 49
     {
50
+        @trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0.', E_USER_DEPRECATED);
51
+
47 52
         return $this->env;
48 53
     }
49 54
 
... ...
@@ -90,7 +95,7 @@ class Twig_Parser implements Twig_ParserInterface
90 95
         }
91 96
 
92 97
         if (null === $this->expressionParser) {
93
-            $this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators());
98
+            $this->expressionParser = new Twig_ExpressionParser($this, $this->env);
94 99
         }
95 100
 
96 101
         $this->stream = $stream;