mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 19:06:40 +00:00
Use ternary operator with PHP 5.4 when strict_variables is true
This commit is contained in:
committed by
Fabien Potencier
parent
3164ee33d4
commit
f397a8f79b
@@ -44,15 +44,18 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
|
||||
// remove the non-PHP 5.4 version when PHP 5.3 support is dropped
|
||||
// as the non-optimized version is just a workaround for slow ternary operator
|
||||
// when the context has a lot of variables
|
||||
if (version_compare(phpversion(), '5.4.0RC1', '>=') && ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables())) {
|
||||
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
|
||||
// PHP 5.4 ternary operator performance was optimized
|
||||
$compiler
|
||||
->raw('(isset($context[')
|
||||
->string($name)
|
||||
->raw(']) ? $context[')
|
||||
->string($name)
|
||||
->raw('] : null)')
|
||||
;
|
||||
->string($name);
|
||||
if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
|
||||
$compiler->raw('] : null)');
|
||||
} else {
|
||||
$compiler->raw('] : $this->getContext($context, ')->string($name)->raw('))');
|
||||
}
|
||||
} else {
|
||||
$compiler
|
||||
->raw('$this->getContext($context, ')
|
||||
|
||||
@@ -40,7 +40,7 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase
|
||||
$env1 = new Twig_Environment(null, array('strict_variables' => false));
|
||||
|
||||
return array(
|
||||
array($node, '$this->getContext($context, "foo")', $env),
|
||||
version_compare(PHP_VERSION, '5.4.0') >= 0 ? array($node, '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))', $env) : array($node, '$this->getContext($context, "foo")', $env),
|
||||
array($node, $this->getVariableGetter('foo'), $env1),
|
||||
array($self, '$this'),
|
||||
array($context, '$context'),
|
||||
|
||||
Reference in New Issue
Block a user