From cb34e7cb976660f765a89787f53192927a652309 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 12 Jun 2010 15:14:03 +0200 Subject: [PATCH] made a big performance optimization when strict_variables is not set --- lib/Twig/Node/Expression/Name.php | 4 +++- lib/Twig/Resource.php | 10 +++------- test/Twig/Tests/Node/DebugTest.php | 2 +- test/Twig/Tests/Node/Expression/GetAttrTest.php | 6 +++--- test/Twig/Tests/Node/Expression/NameTest.php | 5 ++++- test/Twig/Tests/Node/ForTest.php | 14 +++++++------- test/Twig/Tests/Node/IfTest.php | 10 +++++----- test/Twig/Tests/Node/SetTest.php | 2 +- test/Twig/Tests/Node/TransTest.php | 4 ++-- 9 files changed, 29 insertions(+), 28 deletions(-) diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index 9aae0bc..a7001a3 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -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'])); } } } diff --git a/lib/Twig/Resource.php b/lib/Twig/Resource.php index ee064a3..829ae7e 100644 --- a/lib/Twig/Resource.php +++ b/lib/Twig/Resource.php @@ -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) diff --git a/test/Twig/Tests/Node/DebugTest.php b/test/Twig/Tests/Node/DebugTest.php index f2344d3..1e1e14b 100644 --- a/test/Twig/Tests/Node/DebugTest.php +++ b/test/Twig/Tests/Node/DebugTest.php @@ -51,7 +51,7 @@ EOF $tests[] = array($node, <<env->isDebug()) { - var_export(\$this->getContext(\$context, 'foo')); + var_export((isset(\$context['foo']) ? \$context['foo'] : null)); } EOF ); diff --git a/test/Twig/Tests/Node/Expression/GetAttrTest.php b/test/Twig/Tests/Node/Expression/GetAttrTest.php index 28eb30b..c9700a9 100644 --- a/test/Twig/Tests/Node/Expression/GetAttrTest.php +++ b/test/Twig/Tests/Node/Expression/GetAttrTest.php @@ -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; } diff --git a/test/Twig/Tests/Node/Expression/NameTest.php b/test/Twig/Tests/Node/Expression/NameTest.php index 9f52019..3010aea 100644 --- a/test/Twig/Tests/Node/Expression/NameTest.php +++ b/test/Twig/Tests/Node/Expression/NameTest.php @@ -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)'), ); } } diff --git a/test/Twig/Tests/Node/ForTest.php b/test/Twig/Tests/Node/ForTest.php index f55cd74..aef6ecd 100644 --- a/test/Twig/Tests/Node/ForTest.php +++ b/test/Twig/Tests/Node/ForTest.php @@ -62,9 +62,9 @@ class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase $tests[] = array($node, <<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, <<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, <<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']); diff --git a/test/Twig/Tests/Node/IfTest.php b/test/Twig/Tests/Node/IfTest.php index 41b5422..8569474 100644 --- a/test/Twig/Tests/Node/IfTest.php +++ b/test/Twig/Tests/Node/IfTest.php @@ -55,7 +55,7 @@ class Twig_Tests_Node_IfTest extends Twig_Tests_Node_TestCase $tests[] = array($node, <<getContext(\$context, 'foo'); + echo (isset(\$context['foo']) ? \$context['foo'] : null); } EOF ); @@ -71,9 +71,9 @@ EOF $tests[] = array($node, <<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, <<getContext(\$context, 'foo'); + echo (isset(\$context['foo']) ? \$context['foo'] : null); } else { - echo \$this->getContext(\$context, 'bar'); + echo (isset(\$context['bar']) ? \$context['bar'] : null); } EOF ); diff --git a/test/Twig/Tests/Node/SetTest.php b/test/Twig/Tests/Node/SetTest.php index dff9bde..4c83b63 100644 --- a/test/Twig/Tests/Node/SetTest.php +++ b/test/Twig/Tests/Node/SetTest.php @@ -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, <<getContext(\$context, 'bar')); +list(\$context['foo'], \$context['bar']) = array("foo", (isset(\$context['bar']) ? \$context['bar'] : null)); EOF ); diff --git a/test/Twig/Tests/Node/TransTest.php b/test/Twig/Tests/Node/TransTest.php index 3182fd9..67a235a 100644 --- a/test/Twig/Tests/Node/TransTest.php +++ b/test/Twig/Tests/Node/TransTest.php @@ -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; } -- 1.7.2.5