made a big performance optimization when strict_variables is not set
authorFabien Potencier <fabien.potencier@gmail.com>
Sat, 12 Jun 2010 13:14:03 +0000 (15:14 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Sat, 12 Jun 2010 13:22:07 +0000 (15:22 +0200)
lib/Twig/Node/Expression/Name.php
lib/Twig/Resource.php
test/Twig/Tests/Node/DebugTest.php
test/Twig/Tests/Node/Expression/GetAttrTest.php
test/Twig/Tests/Node/Expression/NameTest.php
test/Twig/Tests/Node/ForTest.php
test/Twig/Tests/Node/IfTest.php
test/Twig/Tests/Node/SetTest.php
test/Twig/Tests/Node/TransTest.php

index 9aae0bc..a7001a3 100644 (file)
@@ -20,8 +20,10 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
     {
         if ('self' === $this['name']) {
             $compiler->raw('$this');
-        } else {
+        } elseif ($compiler->getEnvironment()->isStrictVariables()) {
             $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $this['name'], $this['name']));
+        } else {
+            $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this['name'], $this['name']));
         }
     }
 }
index ee064a3..829ae7e 100644 (file)
@@ -26,15 +26,11 @@ abstract class Twig_Resource
 
     protected function getContext($context, $item)
     {
-        if (array_key_exists($item, $context)) {
-            return $context[$item];
+        if (!array_key_exists($item, $context)) {
+            throw new InvalidArgumentException(sprintf('Variable "%s" does not exist.', $item));
         }
 
-        if (!$this->env->isStrictVariables()) {
-            return null;
-        }
-
-        throw new InvalidArgumentException(sprintf('Variable "%s" does not exist.', $item));
+        return $context[$item];
     }
 
     protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_Node_Expression_GetAttr::TYPE_ANY)
index f2344d3..1e1e14b 100644 (file)
@@ -51,7 +51,7 @@ EOF
 
         $tests[] = array($node, <<<EOF
 if (\$this->env->isDebug()) {
-    var_export(\$this->getContext(\$context, 'foo'));
+    var_export((isset(\$context['foo']) ? \$context['foo'] : null));
 }
 EOF
         );
index 28eb30b..c9700a9 100644 (file)
@@ -49,10 +49,10 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase
         $attr = new Twig_Node_Expression_Constant('bar', 0);
         $args = new Twig_Node();
         $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_ANY, 0);
-        $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array(), "any")');
+        $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "any")');
 
         $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_ARRAY, 0);
-        $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array(), "array")');
+        $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array(), "array")');
 
 
         $args = new Twig_Node(array(
@@ -60,7 +60,7 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase
             new Twig_Node_Expression_Constant('bar', 0),
         ));
         $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_Node_Expression_GetAttr::TYPE_METHOD, 0);
-        $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array($this->getContext($context, \'foo\'), "bar", ), "method")');
+        $tests[] = array($node, '$this->getAttribute((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", array((isset($context[\'foo\']) ? $context[\'foo\'] : null), "bar", ), "method")');
 
         return $tests;
     }
index 9f52019..3010aea 100644 (file)
@@ -36,8 +36,11 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Tests_Node_TestCase
     {
         $node = new Twig_Node_Expression_Name('foo', 0);
 
+        $env = new Twig_Environment(null, array('strict_variables' => true));
+
         return array(
-            array($node, '$this->getContext($context, \'foo\')'),
+            array($node, '$this->getContext($context, \'foo\')', $env),
+            array($node, '(isset($context[\'foo\']) ? $context[\'foo\'] : null)'),
         );
     }
 }
index f55cd74..aef6ecd 100644 (file)
@@ -62,9 +62,9 @@ class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase
 
         $tests[] = array($node, <<<EOF
 \$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'items'));
+\$context['_seq'] = twig_iterator_to_array((isset(\$context['items']) ? \$context['items'] : null));
 foreach (\$context['_seq'] as \$context['key'] => \$context['item']) {
-    echo \$this->getContext(\$context, 'foo');
+    echo (isset(\$context['foo']) ? \$context['foo'] : null);
 }
 \$_parent = \$context['_parent'];
 unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']);
@@ -82,7 +82,7 @@ EOF
 
         $tests[] = array($node, <<<EOF
 \$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'values'));
+\$context['_seq'] = twig_iterator_to_array((isset(\$context['values']) ? \$context['values'] : null));
 \$countable = is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable);
 \$length = \$countable ? count(\$context['_seq']) : null;
 \$context['loop'] = array(
@@ -98,7 +98,7 @@ if (\$countable) {
     \$context['loop']['last'] = 1 === \$length;
 }
 foreach (\$context['_seq'] as \$context['k'] => \$context['v']) {
-    echo \$this->getContext(\$context, 'foo');
+    echo (isset(\$context['foo']) ? \$context['foo'] : null);
     ++\$context['loop']['index0'];
     ++\$context['loop']['index'];
     \$context['loop']['first'] = false;
@@ -125,7 +125,7 @@ EOF
         $tests[] = array($node, <<<EOF
 \$context['_parent'] = (array) \$context;
 \$context['_iterated'] = false;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'values'));
+\$context['_seq'] = twig_iterator_to_array((isset(\$context['values']) ? \$context['values'] : null));
 \$countable = is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable);
 \$length = \$countable ? count(\$context['_seq']) : null;
 \$context['loop'] = array(
@@ -142,7 +142,7 @@ if (\$countable) {
 }
 foreach (\$context['_seq'] as \$context['k'] => \$context['v']) {
     \$context['_iterated'] = true;
-    echo \$this->getContext(\$context, 'foo');
+    echo (isset(\$context['foo']) ? \$context['foo'] : null);
     ++\$context['loop']['index0'];
     ++\$context['loop']['index'];
     \$context['loop']['first'] = false;
@@ -153,7 +153,7 @@ foreach (\$context['_seq'] as \$context['k'] => \$context['v']) {
     }
 }
 if (!\$context['_iterated']) {
-    echo \$this->getContext(\$context, 'foo');
+    echo (isset(\$context['foo']) ? \$context['foo'] : null);
 }
 \$_parent = \$context['_parent'];
 unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
index 41b5422..8569474 100644 (file)
@@ -55,7 +55,7 @@ class Twig_Tests_Node_IfTest extends Twig_Tests_Node_TestCase
 
         $tests[] = array($node, <<<EOF
 if (true) {
-    echo \$this->getContext(\$context, 'foo');
+    echo (isset(\$context['foo']) ? \$context['foo'] : null);
 }
 EOF
         );
@@ -71,9 +71,9 @@ EOF
 
         $tests[] = array($node, <<<EOF
 if (true) {
-    echo \$this->getContext(\$context, 'foo');
+    echo (isset(\$context['foo']) ? \$context['foo'] : null);
 } elseif (false) {
-    echo \$this->getContext(\$context, 'bar');
+    echo (isset(\$context['bar']) ? \$context['bar'] : null);
 }
 EOF
         );
@@ -87,9 +87,9 @@ EOF
 
         $tests[] = array($node, <<<EOF
 if (true) {
-    echo \$this->getContext(\$context, 'foo');
+    echo (isset(\$context['foo']) ? \$context['foo'] : null);
 } else {
-    echo \$this->getContext(\$context, 'bar');
+    echo (isset(\$context['bar']) ? \$context['bar'] : null);
 }
 EOF
         );
index dff9bde..4c83b63 100644 (file)
@@ -59,7 +59,7 @@ EOF
         $values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 0), new Twig_Node_Expression_Name('bar', 0)), array(), 0);
         $node = new Twig_Node_Set(false, $names, $values, 0);
         $tests[] = array($node, <<<EOF
-list(\$context['foo'], \$context['bar']) = array("foo", \$this->getContext(\$context, 'bar'));
+list(\$context['foo'], \$context['bar']) = array("foo", (isset(\$context['bar']) ? \$context['bar'] : null));
 EOF
         );
 
index 3182fd9..67a235a 100644 (file)
@@ -74,7 +74,7 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase
             new Twig_Node_Text(' pommes', 0),
         ), array(), 0);
         $node = new Twig_Node_Trans(null, $body, null, 0);
-        $tests[] = array($node, 'echo strtr(gettext("J\'ai %foo% pommes"), array("%foo%" => $this->getContext($context, \'foo\'), ));');
+        $tests[] = array($node, 'echo strtr(gettext("J\'ai %foo% pommes"), array("%foo%" => (isset($context[\'foo\']) ? $context[\'foo\'] : null), ));');
 
         $count = new Twig_Node_Expression_Constant(12, 0);
         $body = new Twig_Node(array(
@@ -90,7 +90,7 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase
             new Twig_Node_Text(' apples', 0),
         ), array(), 0);
         $node = new Twig_Node_Trans($count, $body, $plural, 0);
-        $tests[] = array($node, 'echo strtr(ngettext("Hey %name%, I have one apple", "Hey %name%, I have %count% apples", abs(12)), array("%name%" => $this->getContext($context, \'name\'), "%name%" => $this->getContext($context, \'name\'), "%count%" => abs(12), ));');
+        $tests[] = array($node, 'echo strtr(ngettext("Hey %name%, I have one apple", "Hey %name%, I have %count% apples", abs(12)), array("%name%" => (isset($context[\'name\']) ? $context[\'name\'] : null), "%name%" => (isset($context[\'name\']) ? $context[\'name\'] : null), "%count%" => abs(12), ));');
 
         return $tests;
     }