- optimization {foreach} compiler and processing

This commit is contained in:
uwetews
2016-05-16 17:57:06 +02:00
parent a62557e817
commit 0fcac38c70
4 changed files with 57 additions and 30 deletions

View File

@@ -169,20 +169,28 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
$itemAttr[ 'iteration' ] = true;
$itemAttr[ 'total' ] = true;
}
if (isset($namedAttr[ 'show' ])) {
$namedAttr[ 'total' ] = true;
}
if (isset($itemAttr[ 'show' ])) {
$itemAttr[ 'total' ] = true;
}
$keyTerm = '';
if (isset($itemAttr[ 'key' ])) {
$keyTerm = "{$itemVar}->key => ";
unset($itemAttr[ 'key' ]);
} elseif (isset($attributes[ 'key' ])) {
$keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => ";
}
if ($this->isNamed) {
$foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
}
$this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $local, $itemVar, true));
$needTotal = isset($itemAttr[ 'total' ]);
// Register tag
$this->openTag($compiler, 'foreach',
array('foreach', $compiler->nocache, $local, $itemVar, empty($itemAttr) ? 1 : 2));
// maybe nocache because of nocache variables
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
$needTotal = isset($itemAttr[ 'show' ]) || isset($itemAttr[ 'total' ]) || isset($namedAttr[ 'total' ]) ||
isset($namedAttr[ 'show' ]) || isset($itemAttr[ 'last' ]) || isset($namedAttr[ 'last' ]);
// generate output code
$output = "<?php\n";
$output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " .
@@ -206,8 +214,8 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
if (isset($itemAttr[ 'index' ])) {
$output .= "{$itemVar}->index = -1;\n";
}
$output .= "if (\$_from !== null) {\n";
$output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n";
$output .= "{$itemVar}->_loop = true;\n";
if (isset($attributes[ 'key' ]) && isset($itemAttr[ 'key' ])) {
$output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n";
}
@@ -237,7 +245,9 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
$output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] == {$foreachVar}->value['total'];\n";
}
}
$output .= "{$local}saved = {$itemVar};\n";
if (!empty($itemAttr)) {
$output .= "{$local}saved = {$itemVar};\n";
}
$output .= "?>";
return $output;
@@ -266,12 +276,13 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
list($openTag, $nocache, $local, $itemVar, $foo) = $this->closeTag($compiler, array('foreach'));
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, false));
list($openTag, $nocache, $local, $itemVar, $restore) = $this->closeTag($compiler, array('foreach'));
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, 0));
$output = "<?php\n";
$output .= "{$itemVar} = {$local}saved;\n";
$output .= "}\n";
$output .= "if (!{$itemVar}->_loop) {\n?>";
if ($restore == 2) {
$output .= "{$itemVar} = {$local}saved;\n";
}
$output .= "}\n} else {\n?>\n";
return $output;
}
}
@@ -305,12 +316,15 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
$this->closeTag($compiler, array('foreach', 'foreachelse'));
$output = "<?php\n";
if ($restore) {
if ($restore == 2) {
$output .= "{$itemVar} = {$local}saved;\n";
}
if ($restore > 0) {
$output .= "}\n";
}
$output .= "}\n";
$output .= "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl);\n";
$output .= "?>";
$output .= "?>\n";
return $output;
}
}