- improvement {foreach} observe {break n} and {continue n} nesting levels when restoring saved $item and $key variables

This commit is contained in:
uwetews
2016-09-09 22:54:42 +02:00
parent 0e942e4821
commit 62099617ae
7 changed files with 107 additions and 72 deletions

View File

@@ -2,6 +2,7 @@
09.09.2016 09.09.2016
- bugfix/optimization {foreach} did not execute the {foreachelse} when iterating empty objects https://github.com/smarty-php/smarty/pull/287 - bugfix/optimization {foreach} did not execute the {foreachelse} when iterating empty objects https://github.com/smarty-php/smarty/pull/287
- bugfix {foreach} must keep the @properties when restoring a saved $item variable as the properties might be used outside {foreach} https://github.com/smarty-php/smarty/issues/267 - bugfix {foreach} must keep the @properties when restoring a saved $item variable as the properties might be used outside {foreach} https://github.com/smarty-php/smarty/issues/267
- improvement {foreach} observe {break n} and {continue n} nesting levels when restoring saved $item and $key variables
08.09.2016 08.09.2016
- bugfix implement wrapper for removed method getConfigVariable() https://github.com/smarty-php/smarty/issues/286 - bugfix implement wrapper for removed method getConfigVariable() https://github.com/smarty-php/smarty/issues/286
@@ -52,7 +53,7 @@
23.07.2016 23.07.2016
- bugfix setTemplateDir('/') and setTemplateDir('') did create wrong absolute filepath https://github.com/smarty-php/smarty/issues/245 - bugfix setTemplateDir('/') and setTemplateDir('') did create wrong absolute filepath https://github.com/smarty-php/smarty/issues/245
- optimization of filepath normalization - optimization of filepath normalization
- improvement remove double funtion declaration in plugin shared.escape_special_cars.php https://github.com/smarty-php/smarty/issues/229 - improvement remove double function declaration in plugin shared.escape_special_cars.php https://github.com/smarty-php/smarty/issues/229
19.07.2016 19.07.2016
- bugfix multiple {include} with relative filepath within {block}{/block} could fail https://github.com/smarty-php/smarty/issues/246 - bugfix multiple {include} with relative filepath within {block}{/block} could fail https://github.com/smarty-php/smarty/issues/246

View File

@@ -114,7 +114,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.31-dev/15'; const SMARTY_VERSION = '3.1.31-dev/16';
/** /**
* define variable scopes * define variable scopes

View File

@@ -40,9 +40,31 @@ class Smarty_Internal_Compile_Break extends Smarty_Internal_CompileBase
* @param array $parameter array with compilation parameter * @param array $parameter array with compilation parameter
* *
* @return string compiled code * @return string compiled code
* @throws \SmartyCompilerException
*/ */
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
list($levels, $foreachLevels) = $this->checkLevels($args, $compiler);
$output = "<?php\n";
if ($foreachLevels) {
/* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
$foreachCompiler = $compiler->getTagCompiler('foreach');
$output .= $foreachCompiler->compileRestore($foreachLevels);
}
$output .= "break {$levels};?>";
return $output;
}
/**
* check attributes and return array of break and foreach levels
*
* @param array $args array with attributes from parser
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
* @param string $tag tag name
*
* @return array
* @throws \SmartyCompilerException
*/
public function checkLevels($args, Smarty_Internal_TemplateCompilerBase $compiler, $tag = 'break')
{ {
static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true); static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true);
// check and get attributes // check and get attributes
@@ -56,22 +78,33 @@ class Smarty_Internal_Compile_Break extends Smarty_Internal_CompileBase
if (!is_numeric($_attr[ 'levels' ])) { if (!is_numeric($_attr[ 'levels' ])) {
$compiler->trigger_template_error('level attribute must be a numeric constant', null, true); $compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
} }
$_levels = $_attr[ 'levels' ]; $levels = $_attr[ 'levels' ];
} else { } else {
$_levels = 1; $levels = 1;
} }
$level_count = $_levels; $level_count = $levels;
$stack_count = count($compiler->_tag_stack) - 1; $stack_count = count($compiler->_tag_stack) - 1;
while ($level_count > 0 && $stack_count >= 0) { $foreachLevels = 0;
$lastTag = '';
while ($level_count >= 0 && $stack_count >= 0) {
if (isset($_is_loopy[ $compiler->_tag_stack[ $stack_count ][ 0 ] ])) { if (isset($_is_loopy[ $compiler->_tag_stack[ $stack_count ][ 0 ] ])) {
$lastTag = $compiler->_tag_stack[ $stack_count ][ 0 ];
if ($level_count === 0) {
break;
}
$level_count --; $level_count --;
if ($compiler->_tag_stack[ $stack_count ][ 0 ] === 'foreach') {
$foreachLevels ++;
}
} }
$stack_count --; $stack_count --;
} }
if ($level_count != 0) { if ($level_count != 0) {
$compiler->trigger_template_error("cannot break {$_levels} level(s)", null, true); $compiler->trigger_template_error("cannot {$tag} {$levels} level(s)", null, true);
} }
if ($lastTag === 'foreach' && $tag === 'break') {
return "<?php break {$_levels};?>"; $foreachLevels --;
}
return array($levels, $foreachLevels);
} }
} }

View File

@@ -14,23 +14,8 @@
* @package Smarty * @package Smarty
* @subpackage Compiler * @subpackage Compiler
*/ */
class Smarty_Internal_Compile_Continue extends Smarty_Internal_CompileBase class Smarty_Internal_Compile_Continue extends Smarty_Internal_Compile_Break
{ {
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $optional_attributes = array('levels');
/**
* Attribute definition: Overwrites base class.
*
* @var array
* @see Smarty_Internal_CompileBase
*/
public $shorttag_order = array('levels');
/** /**
* Compiles code for the {continue} tag * Compiles code for the {continue} tag
@@ -44,34 +29,14 @@ class Smarty_Internal_Compile_Continue extends Smarty_Internal_CompileBase
*/ */
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter) public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{ {
static $_is_loopy = array('for' => true, 'foreach' => true, 'while' => true, 'section' => true); list($levels, $foreachLevels) = $this->checkLevels($args, $compiler, 'continue');
// check and get attributes $output = "<?php\n";
$_attr = $this->getAttributes($compiler, $args); if ($foreachLevels > 1) {
/* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
if ($_attr[ 'nocache' ] === true) { $foreachCompiler = $compiler->getTagCompiler('foreach');
$compiler->trigger_template_error('nocache option not allowed', null, true); $output .= $foreachCompiler->compileRestore($foreachLevels - 1);
} }
$output .= "continue {$levels};?>";
if (isset($_attr[ 'levels' ])) { return $output;
if (!is_numeric($_attr[ 'levels' ])) {
$compiler->trigger_template_error('level attribute must be a numeric constant', null, true);
}
$_levels = $_attr[ 'levels' ];
} else {
$_levels = 1;
}
$level_count = $_levels;
$stack_count = count($compiler->_tag_stack) - 1;
while ($level_count > 0 && $stack_count >= 0) {
if (isset($_is_loopy[ $compiler->_tag_stack[ $stack_count ][ 0 ] ])) {
$level_count --;
}
$stack_count --;
}
if ($level_count != 0) {
$compiler->trigger_template_error("cannot continue {$_levels} level(s)", null, true);
}
return "<?php continue {$_levels};?>";
} }
} }

View File

@@ -231,7 +231,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
if (isset($itemAttr[ 'last' ])) { if (isset($itemAttr[ 'last' ])) {
$output .= "{$itemVar}->last = {$itemVar}->iteration == {$itemVar}->total;\n"; $output .= "{$itemVar}->last = {$itemVar}->iteration == {$itemVar}->total;\n";
} }
if ($this->isNamed) { if (isset($foreachVar)) {
if (isset($namedAttr[ 'iteration' ])) { if (isset($namedAttr[ 'iteration' ])) {
$output .= "{$foreachVar}->value['iteration']++;\n"; $output .= "{$foreachVar}->value['iteration']++;\n";
} }
@@ -252,6 +252,18 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
return $output; return $output;
} }
/**
* Compiles code for to restore saved template variables
*
* @param int $levels number of levels to restore
*
* @return string compiled code
*/
public function compileRestore($levels)
{
return "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl, {$levels});\n";
}
} }
/** /**
@@ -323,7 +335,9 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
$output .= "}\n"; $output .= "}\n";
} }
$output .= "}\n"; $output .= "}\n";
$output .= "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl);\n"; /* @var Smarty_Internal_Compile_Foreach $foreachCompiler */
$foreachCompiler = $compiler->getTagCompiler('foreach');
$output .= $foreachCompiler->compileRestore(1);
$output .= "?>\n"; $output .= "?>\n";
return $output; return $output;
} }

View File

@@ -93,20 +93,28 @@ class Smarty_Internal_Runtime_Foreach
/** /**
* Restore saved variables * Restore saved variables
* *
* will be called by {break n} or {continue n} for the required number of levels
*
* @param \Smarty_Internal_Template $tpl * @param \Smarty_Internal_Template $tpl
* @param int $levels number of levels
*/ */
public function restore(Smarty_Internal_Template $tpl) public function restore(Smarty_Internal_Template $tpl, $levels = 1)
{ {
while ($levels) {
$saveVars = array_pop($this->stack); $saveVars = array_pop($this->stack);
if (isset($saveVars['item'])) { if (!empty($saveVars)) {
$item = &$saveVars['item']; if (isset($saveVars[ 'item' ])) {
$tpl->tpl_vars[ $item[0] ]->value = $item[1]->value; $item = &$saveVars[ 'item' ];
$tpl->tpl_vars[ $item[ 0 ] ]->value = $item[ 1 ]->value;
} }
if (isset($saveVars['key'])) { if (isset($saveVars[ 'key' ])) {
$tpl->tpl_vars[ $saveVars['key'][0] ] = $saveVars['key'][1]; $tpl->tpl_vars[ $saveVars[ 'key' ][ 0 ] ] = $saveVars[ 'key' ][ 1 ];
} }
if (isset($saveVars['named'])) { if (isset($saveVars[ 'named' ])) {
$tpl->tpl_vars[ $saveVars['named'][0] ] = $saveVars['named'][1]; $tpl->tpl_vars[ $saveVars[ 'named' ][ 0 ] ] = $saveVars[ 'named' ][ 1 ];
}
}
$levels--;
} }
} }

View File

@@ -844,9 +844,26 @@ abstract class Smarty_Internal_TemplateCompilerBase
* @param mixed $param2 optional parameter * @param mixed $param2 optional parameter
* @param mixed $param3 optional parameter * @param mixed $param3 optional parameter
* *
* @return string compiled code * @return string|bool compiled code or false
*/ */
public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null) public function callTagCompiler($tag, $args, $param1 = null, $param2 = null, $param3 = null)
{
$tagCompiler = $this->getTagCompiler($tag);
// compile this tag
return $tagCompiler === false ? false : $tagCompiler->compile($args, $this, $param1, $param2, $param3);
}
/**
* lazy loads internal compile plugin for tag compile objects cached for reuse.
*
* class name format: Smarty_Internal_Compile_TagName
* plugin filename format: Smarty_Internal_TagName.php
*
* @param string $tag tag name
*
* @return Smarty_Internal_CompileBase|bool tag compiler object or false if not found
*/
public function getTagCompiler($tag)
{ {
// re-use object if already exists // re-use object if already exists
if (!isset(self::$_tag_objects[ $tag ])) { if (!isset(self::$_tag_objects[ $tag ])) {
@@ -860,12 +877,9 @@ abstract class Smarty_Internal_TemplateCompilerBase
self::$_tag_objects[ $tag ] = new $class_name; self::$_tag_objects[ $tag ] = new $class_name;
} else { } else {
self::$_tag_objects[ $tag ] = false; self::$_tag_objects[ $tag ] = false;
return false;
} }
} }
// compile this tag return self::$_tag_objects[ $tag ];
return self::$_tag_objects[ $tag ] === false ? false :
self::$_tag_objects[ $tag ]->compile($args, $this, $param1, $param2, $param3);
} }
/** /**