mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 20:36:33 +00:00
optimized the . operator performance
This commit is contained in:
@@ -1432,7 +1432,7 @@ function twig_array_batch($items, $size, $fill = null)
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object, $item, array $arguments = array(), $type = Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
|
||||
function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object, $item, array $arguments = array(), $type = Twig_Template::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false, $sandboxed = false)
|
||||
{
|
||||
// array
|
||||
if (Twig_Template::METHOD_CALL !== $type) {
|
||||
@@ -1514,7 +1514,7 @@ function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object,
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($env->hasExtension('Twig_Extension_Sandbox')) {
|
||||
if ($sandboxed) {
|
||||
$env->getExtension('Twig_Extension_Sandbox')->checkPropertyAllowed($object, $item);
|
||||
}
|
||||
|
||||
@@ -1591,7 +1591,7 @@ function twig_get_attribute(Twig_Environment $env, Twig_Source $source, $object,
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($env->hasExtension('Twig_Extension_Sandbox')) {
|
||||
if ($sandboxed) {
|
||||
$env->getExtension('Twig_Extension_Sandbox')->checkMethodAllowed($object, $method);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,9 +23,11 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
|
||||
|
||||
public function compile(Twig_Compiler $compiler)
|
||||
{
|
||||
$env = $compiler->getEnvironment();
|
||||
|
||||
// optimize array calls
|
||||
if (
|
||||
(!$compiler->getEnvironment()->isStrictVariables() || $this->getAttribute('ignore_strict_check'))
|
||||
(!$env->isStrictVariables() || $this->getAttribute('ignore_strict_check'))
|
||||
&& !$this->getAttribute('is_defined_test')
|
||||
&& Twig_Template::ARRAY_CALL === $this->getAttribute('type')
|
||||
) {
|
||||
@@ -58,7 +60,8 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
|
||||
$compiler->raw(', ')->subcompile($this->getNode('attribute'));
|
||||
|
||||
// only generate optional arguments when needed (to make generated code more readable)
|
||||
$needFourth = $this->getAttribute('ignore_strict_check');
|
||||
$needFifth = $env->hasExtension('Twig_Extension_Sandbox');
|
||||
$needFourth = $needFifth || $this->getAttribute('ignore_strict_check');
|
||||
$needThird = $needFourth || $this->getAttribute('is_defined_test');
|
||||
$needSecond = $needThird || Twig_Template::ANY_CALL !== $this->getAttribute('type');
|
||||
$needFirst = $needSecond || $this->hasNode('arguments');
|
||||
@@ -83,6 +86,10 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
|
||||
$compiler->raw(', ')->repr($this->getAttribute('ignore_strict_check'));
|
||||
}
|
||||
|
||||
if ($needFifth) {
|
||||
$compiler->raw(', ')->repr($env->hasExtension('Twig_Extension_Sandbox'));
|
||||
}
|
||||
|
||||
$compiler->raw(')');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ class Twig_Tests_TemplateTest extends \PHPUnit\Framework\TestCase
|
||||
$template = new Twig_TemplateTest($twig);
|
||||
|
||||
try {
|
||||
twig_get_attribute($twig, $template->getSourceContext(), $object, $item, array(), 'any');
|
||||
twig_get_attribute($twig, $template->getSourceContext(), $object, $item, array(), 'any', false, false, true);
|
||||
|
||||
if (!$allowed) {
|
||||
$this->fail();
|
||||
|
||||
Reference in New Issue
Block a user