- optimization and code cleanup of {foreach} and {section} compiler

This commit is contained in:
Uwe Tews
2015-08-02 19:43:21 +02:00
parent 05a8fa245e
commit e205cb7a89
5 changed files with 130 additions and 166 deletions

View File

@@ -79,12 +79,13 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
/**
* Compiles code for the {section} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
* @return string compiled code
* @throws \SmartyCompilerException
*/
public function compile($args, $compiler)
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
@@ -95,19 +96,18 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
$compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", $compiler->lex->taglineno);
}
}
$attributes['no'] = $this->counter ++ . '_' . $attributes['name'];
$this->openTag($compiler, 'section', array('section', $compiler->nocache, $attributes));
$local = "\$__section_{$attributes['name']}_" . $this->counter ++ . '_';
$sectionVar = "\$_smarty_tpl->tpl_vars['__smarty_section_{$attributes['name']}']";
$this->openTag($compiler, 'section', array('section', $compiler->nocache, $local, $sectionVar));
// maybe nocache because of nocache variables
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
$sectionVar = "\$_smarty_tpl->tpl_vars['__section_{$attributes['name']}']->value";
$local = "\$section_{$attributes['no']}_";
$initLocal = array('saved' => "isset(\$_smarty_tpl->tpl_vars['__section_{$attributes['name']}']) ? \$_smarty_tpl->tpl_vars['__section_{$attributes['name']}'] : false",);
$initLocal = array('saved' => "isset(\$_smarty_tpl->tpl_vars['__smarty_section_{$attributes['name']}']) ? \$_smarty_tpl->tpl_vars['__section_{$attributes['name']}'] : false",);
$initNamedProperty = array();
$initFor = array();
$incFor = array();
$cmpFor = array();
$propValue = array('index' => "{$sectionVar}['index']", 'show' => 'true', 'step' => 1,
$propValue = array('index' => "{$sectionVar}->value['index']", 'show' => 'true', 'step' => 1,
'iteration' => "{$local}iteration",
);
@@ -132,7 +132,7 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
if (isset($namedAttr['loop'])) {
$initNamedProperty['loop'] = "'loop' => {$v}";
if ($t == 1) {
$v = "{$sectionVar}['loop']";
$v = "{$sectionVar}->value['loop']";
}
} elseif ($t == 1) {
$initLocal['loop'] = $v;
@@ -182,21 +182,21 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
$initNamedProperty['step'] = $propValue['step'];
}
if (isset($namedAttr['iteration'])) {
$propValue['iteration'] = "{$sectionVar}['iteration']";
$propValue['iteration'] = "{$sectionVar}->value['iteration']";
}
$incFor['iteration'] = "{$propValue['iteration']}++";
$initFor['iteration'] = "{$propValue['iteration']} = 1";
if ($propType['step'] == 0) {
if ($propValue['step'] == 1) {
$incFor['index'] = "{$sectionVar}['index']++";
$incFor['index'] = "{$sectionVar}->value['index']++";
} elseif ($propValue['step'] > 1) {
$incFor['index'] = "{$sectionVar}['index'] += {$propValue['step']}";
$incFor['index'] = "{$sectionVar}->value['index'] += {$propValue['step']}";
} else {
$incFor['index'] = "{$sectionVar}['index'] -= " . - $propValue['step'];
$incFor['index'] = "{$sectionVar}->value['index'] -= " . - $propValue['step'];
}
} else {
$incFor['index'] = "{$sectionVar}['index'] += {$propValue['step']}";
$incFor['index'] = "{$sectionVar}->value['index'] += {$propValue['step']}";
}
if (!isset($propValue['max'])) {
@@ -278,7 +278,7 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
$propValue['start'] = "{$local}start";
}
$initFor['index'] = "{$sectionVar}['index'] = {$propValue['start']}";
$initFor['index'] = "{$sectionVar}->value['index'] = {$propValue['start']}";
if (!isset($_attr['start']) && !isset($_attr['step']) && !isset($_attr['max'])) {
$propValue['total'] = $propValue['loop'];
@@ -287,7 +287,7 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
$propType['total'] = $propType['start'] + $propType['loop'] + $propType['step'] + $propType['max'];
if ($propType['total'] == 0) {
$propValue['total'] = min(ceil(($propValue['step'] > 0 ? $propValue['loop'] -
$propValue['start'] : $propValue['start'] + 1) /
$propValue['start'] : (int) $propValue['start'] + 1) /
abs($propValue['step'])), $propValue['max']);
} else {
$total_code = array(1 => 'min(', 2 => 'ceil(', 3 => '(', 4 => "{$propValue['step']} > 0 ? ",
@@ -302,7 +302,7 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
$total_code[6] = $total_code[7] = '';
}
if ($propType['start'] == 0) {
$total_code[9] = $propValue['start'] + 1;
$total_code[9] = (int) $propValue['start'] + 1;
$total_code[10] = '';
}
if ($propType['step'] == 0) {
@@ -326,7 +326,7 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
if (isset($namedAttr['total'])) {
$initNamedProperty['total'] = "'total' => {$propValue['total']}";
if ($propType['total'] > 0) {
$propValue['total'] = "{$sectionVar}['total']";
$propValue['total'] = "{$sectionVar}->value['total']";
}
} elseif ($propType['total'] > 0) {
$initLocal['total'] = $propValue['total'];
@@ -340,7 +340,7 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
}
$_vars = 'array(' . join(', ', $initNamedProperty) . ')';
$output .= "\$_smarty_tpl->tpl_vars['__section_{$attributes['name']}'] = new Smarty_Variable({$_vars});\n";
$output .= "{$sectionVar} = new Smarty_Variable({$_vars});\n";
$cond_code = "{$propValue['total']} != 0";
if ($propType['total'] == 0) {
if ($propValue['total'] == 0) {
@@ -362,49 +362,24 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
$jinc = join(', ', $incFor);
$output .= "for ({$jinit}; {$jcmp}; {$jinc}){\n";
if (isset($namedAttr['rownum'])) {
$output .= "{$sectionVar}['rownum'] = {$propValue['iteration']};\n";
$output .= "{$sectionVar}->value['rownum'] = {$propValue['iteration']};\n";
}
if (isset($namedAttr['index_prev'])) {
$output .= "{$sectionVar}['index_prev'] = {$propValue['index']} - {$propValue['step']};\n";
$output .= "{$sectionVar}->value['index_prev'] = {$propValue['index']} - {$propValue['step']};\n";
}
if (isset($namedAttr['index_next'])) {
$output .= "{$sectionVar}['index_next'] = {$propValue['index']} + {$propValue['step']};\n";
$output .= "{$sectionVar}->value['index_next'] = {$propValue['index']} + {$propValue['step']};\n";
}
if (isset($namedAttr['first'])) {
$output .= "{$sectionVar}['first'] = ({$propValue['iteration']} == 1);\n";
$output .= "{$sectionVar}->value['first'] = ({$propValue['iteration']} == 1);\n";
}
if (isset($namedAttr['last'])) {
$output .= "{$sectionVar}['last'] = ({$propValue['iteration']} == {$propValue['total']});\n";
$output .= "{$sectionVar}->value['last'] = ({$propValue['iteration']} == {$propValue['total']});\n";
}
$output .= "?>";
return $output;
}
/**
* Compiles code for the {$smarty.section} tag
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return string compiled code
* @throws \SmartyCompilerException
*/
public static function compileSpecialVariable($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
if (!isset($parameter[1]) || false === $name = $compiler->getId($parameter[1])) {
$compiler->trigger_template_error("missing or illegal \$Smarty.section name attribute", $compiler->lex->taglineno);
}
if ((!isset($parameter[2]) || false === $property = $compiler->getId($parameter[2])) ||
!in_array(strtolower($property), array('first', 'last', 'index', 'iteration', 'show', 'total'))
) {
$compiler->trigger_template_error("missing or illegal \$Smarty.section property attribute", $compiler->lex->taglineno);
}
$property = strtolower($property);
$sectionVar = "'__section_{$name}'";
return "(isset(\$_smarty_tpl->tpl_vars[{$sectionVar}]->value['{$property}']) ? \$_smarty_tpl->tpl_vars[{$sectionVar}]->value['{$property}'] : null)";
}
}
/**
@@ -418,18 +393,18 @@ class Smarty_Internal_Compile_Sectionelse extends Smarty_Internal_CompileBase
/**
* Compiles code for the {sectionelse} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, $compiler)
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
list($openTag, $nocache, $attributes) = $this->closeTag($compiler, array('section'));
$this->openTag($compiler, 'sectionelse', array('sectionelse', $nocache, $attributes));
list($openTag, $nocache, $local, $sectionVar) = $this->closeTag($compiler, array('section'));
$this->openTag($compiler, 'sectionelse', array('sectionelse', $nocache, $local, $sectionVar));
return "<?php }} else {\n ?>";
}
@@ -446,12 +421,12 @@ class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase
/**
* Compiles code for the {/section} tag
*
* @param array $args array with attributes from parser
* @param object $compiler compiler object
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
*
* @return string compiled code
*/
public function compile($args, $compiler)
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
@@ -461,7 +436,8 @@ class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase
$compiler->tag_nocache = true;
}
list($openTag, $compiler->nocache, $attributes) = $this->closeTag($compiler, array('section', 'sectionelse'));
list($openTag, $compiler->nocache, $local, $sectionVar) = $this->closeTag($compiler, array('section',
'sectionelse'));
$output = "<?php\n";
if ($openTag == 'sectionelse') {
@@ -469,8 +445,8 @@ class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase
} else {
$output .= "}\n}\n";
}
$output .= "if (\$section_{$attributes['no']}_saved) {\n";
$output .= "\$_smarty_tpl->tpl_vars['__section_{$attributes['name']}'] = \$section_{$attributes['no']}_saved;\n";
$output .= "if ({$local}saved) {\n";
$output .= "{$sectionVar} = {$local}saved;\n";
$output .= "}\n";
$output .= "?>";