From c1f34b1314e3308c307bd46af91dcb4b09125210 Mon Sep 17 00:00:00 2001 From: uwetews Date: Fri, 5 Feb 2016 17:41:11 +0100 Subject: [PATCH] - improvement internal compiler changes --- change_log.txt | 3 + libs/Smarty.class.php | 2 +- .../smarty_internal_runtime_codeframe.php | 4 +- .../smarty_internal_templatecompilerbase.php | 88 ++++++++++++++++++- 4 files changed, 92 insertions(+), 5 deletions(-) diff --git a/change_log.txt b/change_log.txt index 223c59fc..03238ad2 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@  ===== 3.1.30-dev ===== (xx.xx.xx) + 05.02.2016 + - improvement internal compiler changes + 01.02.2016 - bugfix {foreach} compilation failed when $smarty->merge_compiled_includes = true and pre-filters are used. diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 7c447068..3b342e7b 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.30-dev/28'; + const SMARTY_VERSION = '3.1.30-dev/29'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_runtime_codeframe.php b/libs/sysplugins/smarty_internal_runtime_codeframe.php index 0ba32124..10ad35eb 100644 --- a/libs/sysplugins/smarty_internal_runtime_codeframe.php +++ b/libs/sysplugins/smarty_internal_runtime_codeframe.php @@ -49,11 +49,11 @@ class Smarty_Internal_Runtime_CodeFrame $output = "source->filepath . "\" */\n\n"; - + $output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n"; $dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' . ($cache ? 'true' : 'false') . ")"; $output .= "if ({$dec}) {\n"; - $output .= "function {$properties['unifunc']} (\$_smarty_tpl) {\n"; + $output .= "function {$properties['unifunc']} (Smarty_Internal_Template \$_smarty_tpl) {\n"; // include code for plugins if (!$cache) { if (!empty($_template->compiled->required_plugins[ 'compiled' ])) { diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 8fb6a271..5b4312e5 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -1064,8 +1064,8 @@ abstract class Smarty_Internal_TemplateCompilerBase */ public function getId($input) { - if (preg_match('~^[\'"]*([0-9]*[a-zA-Z_]\w*)[\'"]*$~', $input, $match)) { - return $match[ 1 ]; + if (preg_match('~^([\'"]*)([0-9]*[a-zA-Z_]\w*)\1$~', $input, $match)) { + return $match[ 2 ]; } return false; } @@ -1085,6 +1085,53 @@ abstract class Smarty_Internal_TemplateCompilerBase return false; } + /** + * Set nocache flag in variable or create new variable + * + * @param string $varName + */ + public function setNocacheInVariable($varName){ + // create nocache var to make it know for further compiling + if ($_var = $this->getId($varName)) { + if (isset($this->template->tpl_vars[ $_var ])) { + $this->template->tpl_vars[ $_var ] = clone $this->template->tpl_vars[ $_var ]; + $this->template->tpl_vars[ $_var ]->nocache = true; + } else { + $this->template->tpl_vars[ $_var ] = new Smarty_Variable(null, true); + } + } + } + + /** + * @param array $_attr tag attributes + * @param array $validScopes + * + * @return int|string + * @throws \SmartyCompilerException + */ + public function convertScope($_attr, $validScopes){ + $_scope = Smarty::SCOPE_LOCAL; + if (isset($_attr[ 'scope' ])) { + $_scopeName = trim($_attr[ 'scope' ], "'\""); + if (is_numeric($_scopeName) && in_array($_scopeName, $validScopes)) { + $_scope = $_scopeName; + } elseif (is_string($_scopeName)) { + $_scopeName = trim($_scopeName, "'\""); + $_scope = isset($validScopes[ $_scopeName ]) ? $validScopes[ $_scopeName ] : false; + } else { + $_scope = false; + } + if ($_scope === false) { + $err = var_export($_scopeName, true); + $this->trigger_template_error("illegal value '{$err}' for \"scope\" attribute", null, true); + } + if (isset($_attr[ 'bubble_up' ]) && $_attr[ 'bubble_up' ]) { + $_scope += Smarty::SCOPE_BUBBLE_UP; + } + } + return $_scope; + } + /** * Generate nocache code string * @@ -1166,4 +1213,41 @@ abstract class Smarty_Internal_TemplateCompilerBase $e->template = $this->template->source->filepath; throw $e; } + + /** + * Return var_export() value with all white spaces removed + * + * @param mixed $value + * + * @return string + */ + public function getVarExport($value) { + return preg_replace('/\s/', '', var_export($value, true)); + } + + /** + * Check if $value contains variable elements + * + * @param mixed $value + * + * @return bool|int + */ + public function isVariable($value){ + if (is_string($value)) { + return preg_match('/[$(]/', $value); + } + if (is_bool($value) || is_numeric($value)) { + return false; + } + if (is_array($value)) { + foreach ($value as $k => $v) { + if ($this->isVariable($k) || $this->isVariable($v)) { + return true; + } + } + return false; + } + return false; + } + }