- optimization of {assign}, {if} and {while} compiled code

This commit is contained in:
uwetews
2015-12-31 02:20:47 +01:00
parent 7eb56ac5e3
commit c60cdb3e64
10 changed files with 113 additions and 86 deletions

View File

@@ -1,4 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)
31.12.2015
- optimization of {assign}, {if} and {while} compiled code
30.12.2015
- bugfix plugin names starting with "php" did not compile https://github.com/smarty-php/smarty/issues/147

View File

@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.30-dev/13';
const SMARTY_VERSION = '3.1.30-dev/14';
/**
* define variable scopes

View File

@@ -21,8 +21,8 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
*
* @var array
*/
public $valid_scopes = array('local' => true, 'parent' => true, 'root' => true, 'global' => true,
'smarty' => true, 'tpl_root' => true);
public $valid_scopes = array('local' => true, 'parent' => true, 'root' => true, 'global' => true,
'smarty' => true, 'tpl_root' => true);
/**
* Compiles code for the {assign} tag
@@ -47,52 +47,55 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
if ($compiler->tag_nocache || $compiler->nocache) {
$_nocache = 'true';
// create nocache var to make it know for further compiling
if (isset($compiler->template->tpl_vars[trim($_attr['var'], "'")])) {
$compiler->template->tpl_vars[trim($_attr['var'], "'")]->nocache = true;
if (isset($compiler->template->tpl_vars[ trim($_attr[ 'var' ], "'") ])) {
$compiler->template->tpl_vars[ trim($_attr[ 'var' ], "'") ]->nocache = true;
} else {
$compiler->template->tpl_vars[trim($_attr['var'], "'")] = new Smarty_Variable(null, true);
$compiler->template->tpl_vars[ trim($_attr[ 'var' ], "'") ] = new Smarty_Variable(null, true);
}
}
// scope setup
$_scope = Smarty::SCOPE_LOCAL;
if (isset($_attr['scope'])) {
$_attr['scope'] = trim($_attr['scope'], "'\"");
if (!isset($this->valid_scopes[$_attr['scope']])) {
$compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null, true);
if (isset($_attr[ 'scope' ])) {
$_attr[ 'scope' ] = trim($_attr[ 'scope' ], "'\"");
if (!isset($this->valid_scopes[ $_attr[ 'scope' ] ])) {
$compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null,
true);
}
if ($_attr['scope'] != 'local') {
if ($_attr['scope'] == 'parent') {
if ($_attr[ 'scope' ] != 'local') {
if ($_attr[ 'scope' ] == 'parent') {
$_scope = Smarty::SCOPE_PARENT;
} elseif ($_attr['scope'] == 'root') {
} elseif ($_attr[ 'scope' ] == 'root') {
$_scope = Smarty::SCOPE_ROOT;
} elseif ($_attr['scope'] == 'global') {
} elseif ($_attr[ 'scope' ] == 'global') {
$_scope = Smarty::SCOPE_GLOBAL;
} elseif ($_attr['scope'] == 'smarty') {
} elseif ($_attr[ 'scope' ] == 'smarty') {
$_scope = Smarty::SCOPE_SMARTY;
} elseif ($_attr['scope'] == 'tpl_root') {
} elseif ($_attr[ 'scope' ] == 'tpl_root') {
$_scope = Smarty::SCOPE_TPL_ROOT;
}
$_scope += (isset($_attr['bubble_up']) && $_attr['bubble_up'] == 'false') ? 0 : Smarty::SCOPE_BUBBLE_UP;
$_scope += (isset($_attr[ 'bubble_up' ]) && $_attr[ 'bubble_up' ] == 'false') ? 0 :
Smarty::SCOPE_BUBBLE_UP;
}
}
// compiled output
if (isset($parameter['smarty_internal_index'])) {
$output =
"<?php \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, $_attr[var], $_nocache);\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];";
} else {
// implement Smarty2's behaviour of variables assigned by reference
if ($compiler->template->smarty instanceof SmartyBC) {
$output =
"<?php if (isset(\$_smarty_tpl->tpl_vars[$_attr[var]])) {\$_smarty_tpl->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
$output .= "\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value = $_attr[value]; \$_smarty_tpl->tpl_vars[$_attr[var]]->nocache = $_nocache;";
$output .= "\n} else \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_Variable($_attr[value], $_nocache);";
if (isset($parameter[ 'smarty_internal_index' ])) {
$output = "<?php \$_smarty_tpl->_createLocalArrayVariable({$_attr['var']}, {$_nocache});\n";
$output .= "\$_smarty_tpl->tpl_vars[{$_attr['var']}]->value{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
if ($_scope != Smarty::SCOPE_LOCAL) {
$output .= "\$_smarty_tpl->ext->_updateScope->updateScope(\$_smarty_tpl, {$_attr['var']}, {$_scope});\n";
} else {
$output = "<?php \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_Variable($_attr[value], $_nocache);";
$output .= "if (\$_smarty_tpl->scope & Smarty::SCOPE_BUBBLE_UP) {\n";
$output .= "\$_smarty_tpl->ext->_updateScope->updateScope(\$_smarty_tpl, {$_attr['var']});\n}\n";
}
$output .= '?>';
} else {
if ($compiler->template->smarty instanceof SmartyBC) {
$_smartyBC = 'true';
} else {
$_smartyBC = 'false';
}
$output =
"<?php \$_smarty_tpl->_assignInScope({$_attr['var']}, {$_attr['value']}, {$_nocache}, {$_scope}, {$_smartyBC});\n?>";
}
$output .= "\n\$_smarty_tpl->ext->_updateScope->updateScope(\$_smarty_tpl, $_attr[var], $_scope);";
$output .= '?>';
return $output;
}
}

View File

@@ -58,7 +58,7 @@ class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase
if (is_array($parameter['if condition']['var'])) {
$_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
"]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " . $parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->_createLocalArrayVariable(" . $parameter['if condition']['var']['var'] .
"$_nocache);\n";
$_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
$parameter['if condition']['var']['smarty_internal_index'] . " = " .
@@ -162,7 +162,7 @@ class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase
$_output = "<?php } else { if (!isset(\$_smarty_tpl->tpl_vars[" .
$parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" .
$parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " .
"]->value)) \$_smarty_tpl->_createLocalArrayVariable(" .
$parameter['if condition']['var']['var'] . "$_nocache);\n";
$_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
$parameter['if condition']['var']['smarty_internal_index'] . " = " .
@@ -195,7 +195,7 @@ class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase
$parameter['if condition']['var']['var'] .
"]) || !is_array(\$_smarty_tpl->tpl_vars[" .
$parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " .
"]->value)) \$_smarty_tpl->_createLocalArrayVariable(" .
$parameter['if condition']['var']['var'] . "$_nocache);\n");
$_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
$parameter['if condition']['var']['smarty_internal_index'] . " = " .

View File

@@ -60,7 +60,7 @@ class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
if (is_array($parameter['if condition']['var'])) {
$_output .= "if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
"]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " . $parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->_createLocalArrayVariable(" . $parameter['if condition']['var']['var'] .
"$_nocache);\n";
$_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
$parameter['if condition']['var']['smarty_internal_index'] . " = " .

View File

@@ -12,7 +12,6 @@
*
* @property Smarty_Internal_Runtime_Inheritance $_inheritance
* @property Smarty_Internal_Runtime_TplFunction $_tplFunction
* @property Smarty_Internal_Runtime_Var $_var
* @property Smarty_Internal_Runtime_Foreach $_foreach
* @property Smarty_Internal_Runtime_WriteFile $_writeFile
* @property Smarty_Internal_Runtime_CodeFrame $_codeFrame

View File

@@ -1,7 +1,7 @@
<?php
/**
* Runtime Methods updateScope
* Runtime Extension updateScope
*
* @package Smarty
* @subpackage PluginsInternal
@@ -13,41 +13,52 @@ class Smarty_Internal_Runtime_UpdateScope
/**
* Update new assigned template variable in other effected scopes
*
* @param \Smarty_Internal_Template $tpl template object
* @param string $varName variable name
* @param int $scope scope to which bubble up variable value
* @param \Smarty_Internal_Data $tpl data object
* @param string $varName variable name
* @param int $scope scope to which bubble up variable value
*/
public function updateScope(Smarty_Internal_Template $tpl, $varName, $scope = Smarty::SCOPE_LOCAL)
public function updateScope(Smarty_Internal_Data $tpl, $varName, $scope = Smarty::SCOPE_LOCAL)
{
if (!$scope && !$tpl->scope) {
$scopes = array();
if ($scope) {
$scopes[] = $scope;
}
if ($tpl->scope) {
$scopes[] = $tpl->scope;
}
if (empty($scopes)) {
return;
}
foreach (array($scope, $tpl->scope) as $s) {
/* @var Smarty_Internal_Data $ptr */
$ptr = null;
foreach ($scopes as $s) {
$s = ($bubble_up = $s >= Smarty::SCOPE_BUBBLE_UP) ? $s - Smarty::SCOPE_BUBBLE_UP : $s;
if ($bubble_up && $s) {
$ptr = $tpl->parent;
if (isset($ptr)) {
$ptr->tpl_vars[$varName] = $tpl->tpl_vars[$varName];
$ptr = $ptr->parent;
if (isset($tpl->parent)) {
$ptr = $tpl->parent;
$ptr->tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ];
if (isset($ptr->parent)) {
$ptr = $ptr->parent;
}
}
if ($s == Smarty::SCOPE_PARENT) {
continue;
}
while (isset($ptr) && $ptr->_objType == 2) {
$ptr->tpl_vars[$varName] = $tpl->tpl_vars[$varName];
$ptr->tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ];
$ptr = $ptr->parent;
}
if ($s == Smarty::SCOPE_TPL_ROOT) {
continue;
} elseif ($s == Smarty::SCOPE_SMARTY) {
$tpl->smarty->tpl_vars[$varName] = $tpl->tpl_vars[$varName];
$tpl->smarty->tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ];
} elseif ($s == Smarty::SCOPE_GLOBAL) {
Smarty::$global_tpl_vars[$varName] = $tpl->tpl_vars[$varName];
Smarty::$global_tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ];
} elseif ($s == Smarty::SCOPE_ROOT) {
while (isset($ptr->parent)) {
$ptr = $ptr->parent;
}
$ptr->tpl_vars[$varName] = $tpl->tpl_vars[$varName];
$ptr->tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ];
}
}
}

View File

@@ -1,33 +0,0 @@
<?php
/**
* Runtime Methods createLocalArrayVariable
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*
**/
class Smarty_Internal_Runtime_Var
{
/**
* Template code runtime function to create a local Smarty variable for array assignments
*
* @param \Smarty_Internal_Template $tpl template object
* @param string $varName template variable name
* @param bool $nocache cache mode of variable
*/
public function createLocalArrayVariable(Smarty_Internal_Template $tpl, $varName, $nocache = false)
{
if (!isset($tpl->tpl_vars[$varName])) {
$tpl->tpl_vars[$varName] = new Smarty_Variable(array(), $nocache);
} else {
$tpl->tpl_vars[$varName] = clone $tpl->tpl_vars[$varName];
if (!(is_array($tpl->tpl_vars[$varName]->value) ||
$tpl->tpl_vars[$varName]->value instanceof ArrayAccess)
) {
settype($tpl->tpl_vars[$varName]->value, 'array');
}
}
}
}

View File

@@ -393,6 +393,51 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
return isset($this->parent) && $this->parent->_objType == 2;
}
/**
* Assign variable in scope
*
* @param string $varName variable name
* @param mixed $value value
* @param bool $nocache nocache flag
* @param int $scope scope into which variable shall be assigned
* @param bool $smartyBC true if called in Smarty bc class
*
* @throws \SmartyException
*/
public function _assignInScope($varName, $value, $nocache, $scope, $smartyBC)
{
if ($smartyBC && isset($this->tpl_vars[ $varName ])) {
$this->tpl_vars[ $varName ] = clone $this->tpl_vars[ $varName ];
$this->tpl_vars[ $varName ]->value = $value;
$this->tpl_vars[ $varName ]->nocache = $nocache;
} else {
$this->tpl_vars[ $varName ] = new Smarty_Variable($value, $nocache);
}
if ($scope || $this->scope & Smarty::SCOPE_BUBBLE_UP) {
$this->ext->_updateScope->updateScope($this, $varName, $scope);
}
}
/**
* Template code runtime function to create a local Smarty variable for array assignments
*
* @param string $varName template variable name
* @param bool $nocache cache mode of variable
*/
public function _createLocalArrayVariable($varName, $nocache = false)
{
if (!isset($this->tpl_vars[ $varName ])) {
$this->tpl_vars[ $varName ] = new Smarty_Variable(array(), $nocache);
} else {
$this->tpl_vars[ $varName ] = clone $this->tpl_vars[ $varName ];
if (!(is_array($this->tpl_vars[ $varName ]->value) ||
$this->tpl_vars[ $varName ]->value instanceof ArrayAccess)
) {
settype($this->tpl_vars[ $varName ]->value, 'array');
}
}
}
/**
* This function is executed automatically when a compiled or cached template file is included
* - Decode saved properties from compiled template and cache files

View File

@@ -481,7 +481,6 @@ class Smarty_Internal_TestInstall
'smarty_internal_runtime_tplfunction.php' => true,
'smarty_internal_runtime_updatecache.php' => true,
'smarty_internal_runtime_updatescope.php' => true,
'smarty_internal_runtime_var.php' => true,
'smarty_internal_runtime_writefile.php' => true,
'smarty_internal_smartytemplatecompiler.php' => true,
'smarty_internal_template.php' => true,