From ad65c850f79e7b237307c196c1503a8a91dbaf1e Mon Sep 17 00:00:00 2001 From: uwetews Date: Wed, 21 Oct 2015 02:02:42 +0200 Subject: [PATCH] - move some code into runtime extensions --- libs/Smarty.class.php | 2 +- .../smarty_internal_compile_assign.php | 2 +- .../smarty_internal_compile_call.php | 4 +- .../smarty_internal_compile_foreach.php | 2 +- .../sysplugins/smarty_internal_compile_if.php | 6 +- .../smarty_internal_compile_include.php | 4 +- .../smarty_internal_compile_while.php | 2 +- .../smarty_internal_runtime_foreach.php | 44 +++ .../smarty_internal_runtime_inheritance.php | 2 +- .../smarty_internal_runtime_inline.php | 76 ++++++ .../smarty_internal_runtime_subtemplate.php | 147 ++++++++++ ...hp => smarty_internal_runtime_tplfunc.php} | 63 +++-- .../smarty_internal_runtime_var.php | 38 +++ libs/sysplugins/smarty_internal_template.php | 255 +----------------- 14 files changed, 369 insertions(+), 278 deletions(-) create mode 100644 libs/sysplugins/smarty_internal_runtime_foreach.php create mode 100644 libs/sysplugins/smarty_internal_runtime_inline.php create mode 100644 libs/sysplugins/smarty_internal_runtime_subtemplate.php rename libs/sysplugins/{smarty_internal_function_call_handler.php => smarty_internal_runtime_tplfunc.php} (54%) create mode 100644 libs/sysplugins/smarty_internal_runtime_var.php diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index e2e99eb3..b543e2cb 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -119,7 +119,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.28-dev/66'; + const SMARTY_VERSION = '3.1.28-dev/68'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_compile_assign.php b/libs/sysplugins/smarty_internal_compile_assign.php index 7c0b6657..b9caa37d 100644 --- a/libs/sysplugins/smarty_internal_compile_assign.php +++ b/libs/sysplugins/smarty_internal_compile_assign.php @@ -61,7 +61,7 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase } // compiled output if (isset($parameter['smarty_internal_index'])) { - $output = "createLocalArrayVariable($_attr[var], $_nocache, $_scope);\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];"; + $output = "_var->createLocalArrayVariable(\$_smarty_tpl, $_attr[var], $_nocache, $_scope);\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) { diff --git a/libs/sysplugins/smarty_internal_compile_call.php b/libs/sysplugins/smarty_internal_compile_call.php index 0b241bfd..2970f0b2 100644 --- a/libs/sysplugins/smarty_internal_compile_call.php +++ b/libs/sysplugins/smarty_internal_compile_call.php @@ -79,10 +79,10 @@ class Smarty_Internal_Compile_Call extends Smarty_Internal_CompileBase // was there an assign attribute if (isset($_assign)) { $_output = - "callTemplateFunction({$_name}, \$_smarty_tpl, {$_params}, {$_nocache});\n\$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n"; + "_tplfunc->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});\n\$_smarty_tpl->assign({$_assign}, ob_get_clean());?>\n"; } else { $_output = - "callTemplateFunction({$_name}, \$_smarty_tpl, {$_params}, {$_nocache});?>\n"; + "_tplfunc->callTemplateFunction(\$_smarty_tpl, {$_name}, {$_params}, {$_nocache});?>\n"; } return $_output; } diff --git a/libs/sysplugins/smarty_internal_compile_foreach.php b/libs/sysplugins/smarty_internal_compile_foreach.php index d1b6bdd5..c582d41b 100644 --- a/libs/sysplugins/smarty_internal_compile_foreach.php +++ b/libs/sysplugins/smarty_internal_compile_foreach.php @@ -184,7 +184,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo $output .= "{$local}{$k} = {$code}\n"; } $output .= "{$itemVar} = new Smarty_Variable();\n"; - $output .= "{$local}total = \$_smarty_tpl->_count(\$_from);\n"; + $output .= "{$local}total = \$_smarty_tpl->_foreach->count(\$_from);\n"; if (isset($itemAttr['show'])) { $output .= "{$itemVar}->show = ({$local}total > 0);\n"; } diff --git a/libs/sysplugins/smarty_internal_compile_if.php b/libs/sysplugins/smarty_internal_compile_if.php index f6ce9a88..11017d1a 100644 --- a/libs/sysplugins/smarty_internal_compile_if.php +++ b/libs/sysplugins/smarty_internal_compile_if.php @@ -58,7 +58,7 @@ class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase if (is_array($parameter['if condition']['var'])) { $_output = "tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . - "]->value)) \$_smarty_tpl->createLocalArrayVariable(" . $parameter['if condition']['var']['var'] . + "]->value)) \$_smarty_tpl->_var->createLocalArrayVariable(\$_smarty_tpl, " . $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 = "tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . - "]->value)) \$_smarty_tpl->createLocalArrayVariable(" . + "]->value)) \$_smarty_tpl->_var->createLocalArrayVariable(\$_smarty_tpl, " . $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->createLocalArrayVariable(" . + "]->value)) \$_smarty_tpl->_var->createLocalArrayVariable(\$_smarty_tpl, " . $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'] . " = " . diff --git a/libs/sysplugins/smarty_internal_compile_include.php b/libs/sysplugins/smarty_internal_compile_include.php index 64aed4ce..beed76d6 100644 --- a/libs/sysplugins/smarty_internal_compile_include.php +++ b/libs/sysplugins/smarty_internal_compile_include.php @@ -248,7 +248,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase if (isset($_assign)) { $_output .= "ob_start();\n"; } - $_output .= "\$_smarty_tpl->renderInline(\$_smarty_tpl->setupSubTemplate({$fullResourceName}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid']}'), '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func']}');\n"; + $_output .= "\$_smarty_tpl->_inline->render(\$_smarty_tpl, {$fullResourceName}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid']}', '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func']}');\n"; if (isset($_assign)) { $_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(ob_get_clean());\n"; } @@ -271,7 +271,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase if (isset($_assign)) { $_output .= "ob_start();\n"; } - $_output .= "\$_smarty_tpl->setupSubTemplate({$fullResourceName}, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl})->render();\n"; + $_output .= "\$_smarty_tpl->_subtemplate->render(\$_smarty_tpl, {$fullResourceName}, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl});\n"; if (isset($_assign)) { $_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(ob_get_clean());\n"; } diff --git a/libs/sysplugins/smarty_internal_compile_while.php b/libs/sysplugins/smarty_internal_compile_while.php index 233cc729..119a163e 100644 --- a/libs/sysplugins/smarty_internal_compile_while.php +++ b/libs/sysplugins/smarty_internal_compile_while.php @@ -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->createLocalArrayVariable(" . $parameter['if condition']['var']['var'] . + "]->value)) \$_smarty_tpl->_var->createLocalArrayVariable(\$_smarty_tpl, " . $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'] . " = " . diff --git a/libs/sysplugins/smarty_internal_runtime_foreach.php b/libs/sysplugins/smarty_internal_runtime_foreach.php new file mode 100644 index 00000000..87bb0cf4 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_foreach.php @@ -0,0 +1,44 @@ +getIterator()); + } elseif ($value instanceof Iterator) { + return iterator_count($value); + } elseif ($value instanceof PDOStatement) { + return $value->rowCount(); + } elseif ($value instanceof Traversable) { + return iterator_count($value); + } elseif ($value instanceof ArrayAccess) { + if ($value->offsetExists(0)) { + return 1; + } + } elseif (is_object($value)) { + return count($value); + } + return 0; + } +} diff --git a/libs/sysplugins/smarty_internal_runtime_inheritance.php b/libs/sysplugins/smarty_internal_runtime_inheritance.php index 54a55565..44100c4e 100644 --- a/libs/sysplugins/smarty_internal_runtime_inheritance.php +++ b/libs/sysplugins/smarty_internal_runtime_inheritance.php @@ -1,7 +1,7 @@ setupSubtemplate($parent, $template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, + $parent_scope, $cache_tpl_obj, $uid); + if ($parent->smarty->debugging) { + $parent->smarty->_debug->start_template($tpl); + $parent->smarty->_debug->start_render($tpl); + } + $tpl->compiled->getRenderedTemplateCode($tpl, $content_func); + if ($parent->smarty->debugging) { + $parent->smarty->_debug->end_template($tpl); + $parent->smarty->_debug->end_render($tpl); + } + if ($tpl->caching == 9999 && $tpl->compiled->has_nocache_code) { + $parent->cached->hashes[$tpl->compiled->nocache_hash] = true; + } + } + + /** + * Set source object of inline template by $uid + * + * @param \Smarty_Internal_Template $tpl + * @param string $uid + * + * @throws \SmartyException + */ + public function setSourceByUid(Smarty_Internal_Template $tpl, $uid) + { + // inline templates have same compiled resource + $tpl->compiled = $tpl->parent->compiled; + if (isset($tpl->compiled->file_dependency[$uid])) { + list($filepath, $timestamp, $resource) = $tpl->compiled->file_dependency[$uid]; + $tpl->source = new Smarty_Template_Source(isset($tpl->smarty->_cache['resource_handlers'][$resource]) ? + $tpl->smarty->_cache['resource_handlers'][$resource] : + Smarty_Resource::load($tpl->smarty, $resource), $tpl->smarty, + $filepath, $resource, $filepath); + $tpl->source->filepath = $filepath; + $tpl->source->timestamp = $timestamp; + $tpl->source->exists = true; + $tpl->source->uid = $uid; + } else { + $tpl->source = null; + } + } +} diff --git a/libs/sysplugins/smarty_internal_runtime_subtemplate.php b/libs/sysplugins/smarty_internal_runtime_subtemplate.php new file mode 100644 index 00000000..bc859c90 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_subtemplate.php @@ -0,0 +1,147 @@ +setupSubtemplate($parent, $template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, + $parent_scope, $cache_tpl_obj) + ->render(); + } + + /** + * Runtime function to get subtemplate object from cache or clone from parent + * + * @param \Smarty_Internal_Template $parent calling template + * @param string $template template name + * @param mixed $cache_id cache id + * @param mixed $compile_id compile id + * @param integer $caching cache mode + * @param integer $cache_lifetime life time of cache data + * @param array $data passed parameter template variables + * @param int $parent_scope scope in which {include} should execute + * @param bool $cache_tpl_obj cache template object + * @param string|null $uid source uid + * + * @return \Smarty_Internal_Template template object + * @throws \SmartyException + */ + public function setupSubtemplate(Smarty_Internal_Template $parent, $template, $cache_id, $compile_id, $caching, + $cache_lifetime, $data, $parent_scope, $cache_tpl_obj, $uid = null) + { + // if there are cached template objects calculate $templateID + $_templateId = isset($parent->smarty->_cache['template_objects']) ? + $parent->smarty->_getTemplateId($template, $cache_id, $compile_id) : null; + // already in template cache? + /* @var Smarty_Internal_Template $tpl */ + if (isset($parent->smarty->_cache['template_objects'][$_templateId])) { + // clone cached template object because of possible recursive call + $tpl = clone $parent->smarty->_cache['template_objects'][$_templateId]; + $tpl->parent = $parent; + // if $caching mode changed the compiled resource is invalid + if ((bool) $tpl->caching !== (bool) $caching) { + unset($tpl->compiled); + } + // get variables from calling scope + if ($parent_scope == Smarty::SCOPE_LOCAL) { + $tpl->tpl_vars = $parent->tpl_vars; + $tpl->config_vars = $parent->config_vars; + } + $tpl->tpl_function = $parent->tpl_function; + // copy inheritance object? + if (isset($parent->_inheritance)) { + $tpl->_inheritance = $parent->_inheritance; + } else { + unset($tpl->_inheritance); + } + } else { + $tpl = clone $parent; + $tpl->parent = $parent; + if (!isset($tpl->templateId) || $tpl->templateId !== $_templateId) { + $tpl->templateId = $_templateId; + $tpl->template_resource = $template; + $tpl->cache_id = $cache_id; + $tpl->compile_id = $compile_id; + // $uid is set if template is inline + if (isset($uid)) { + $this->setSourceByUid($tpl, $uid); + // if template is called multiple times set flag to to cache template objects + $cache_tpl_obj = $cache_tpl_obj || (isset($tpl->compiled->includes[$tpl->template_resource]) && + $tpl->compiled->includes[$tpl->template_resource] > 1); + } else { + $tpl->source = null; + unset($tpl->compiled); + } + if (!isset($tpl->source)) { + $tpl->source = Smarty_Template_Source::load($tpl); + } + unset($tpl->cached); + // check if template object should be cached + if (!$tpl->source->handler->recompiled && (isset($tpl->parent->templateId) && + isset($tpl->smarty->_cache['template_objects'][$tpl->parent->templateId]) || + ($cache_tpl_obj && $tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) || + $tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) + ) { + $tpl->smarty->_cache['template_objects'][$tpl->_getTemplateId()] = $tpl; + } + } + } + $tpl->caching = $caching; + $tpl->cache_lifetime = $cache_lifetime; + if ($caching == 9999) { + $tpl->cached = $parent->cached; + } + // get variables from calling scope + if ($parent_scope != Smarty::SCOPE_LOCAL) { + if ($parent_scope == Smarty::SCOPE_PARENT) { + $tpl->tpl_vars = &$parent->tpl_vars; + $tpl->config_vars = &$parent->config_vars; + } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) { + $tpl->tpl_vars = &Smarty::$global_tpl_vars; + $tpl->config_vars = $parent->config_vars; + } elseif ($parent_scope == Smarty::SCOPE_ROOT) { + $ptr = $tpl->parent; + while (!empty($ptr->parent)) { + $ptr = $ptr->parent; + } + $tpl->tpl_vars = &$ptr->tpl_vars; + $tpl->config_vars = &$ptr->config_vars; + } else { + $tpl->tpl_vars = $parent->tpl_vars; + $tpl->config_vars = $parent->config_vars; + } + } + + if (!empty($data)) { + // set up variable values + foreach ($data as $_key => $_val) { + $tpl->tpl_vars[$_key] = new Smarty_Variable($_val); + } + } + return $tpl; + } +} diff --git a/libs/sysplugins/smarty_internal_function_call_handler.php b/libs/sysplugins/smarty_internal_runtime_tplfunc.php similarity index 54% rename from libs/sysplugins/smarty_internal_function_call_handler.php rename to libs/sysplugins/smarty_internal_runtime_tplfunc.php index dc3c264a..ec9d8da6 100644 --- a/libs/sysplugins/smarty_internal_function_call_handler.php +++ b/libs/sysplugins/smarty_internal_runtime_tplfunc.php @@ -1,36 +1,63 @@ tpl_function[$name])) { + if (!$tpl->caching || ($tpl->caching && $nocache)) { + $function = $tpl->tpl_function[$name]['call_name']; + } else { + if (isset($tpl->tpl_function[$name]['call_name_caching'])) { + $function = $tpl->tpl_function[$name]['call_name_caching']; + } else { + $function = $tpl->tpl_function[$name]['call_name']; + } + } + if (function_exists($function)) { + $function ($tpl, $params); + return; + } + // try to load template function dynamically + if ($this->addTplFuncToCache($tpl, $name, $function)) { + $function ($tpl, $params); + return; + } + } + throw new SmartyException("Unable to find template function '{$name}'"); + } + + /** + * + * Add template function to cache file for nocache calls + * + * @param Smarty_Internal_Template $tpl * @param string $_name template function name - * @param Smarty_Internal_Template $_smarty_tpl * @param string $_function PHP function name * * @return bool */ - public static function call($_name, Smarty_Internal_Template $_smarty_tpl, $_function) + public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function) { - $funcParam = $_smarty_tpl->tpl_function[$_name]; + $funcParam = $tpl->tpl_function[$_name]; if (is_file($funcParam['compiled_filepath'])) { // read compiled file $code = file_get_contents($funcParam['compiled_filepath']); @@ -43,7 +70,7 @@ class Smarty_Internal_Function_Call_Handler eval($match[0]); if (function_exists($_function)) { // search cache file template - $tplPtr = $_smarty_tpl; + $tplPtr = $tpl; while (!isset($tplPtr->cached) && isset($tplPtr->parent)) { $tplPtr = $tplPtr->parent; } diff --git a/libs/sysplugins/smarty_internal_runtime_var.php b/libs/sysplugins/smarty_internal_runtime_var.php new file mode 100644 index 00000000..f4f1c997 --- /dev/null +++ b/libs/sysplugins/smarty_internal_runtime_var.php @@ -0,0 +1,38 @@ +tpl_vars[$tpl_var])) { + $tpl->tpl_vars[$tpl_var] = new Smarty_Variable(array(), $nocache, $scope); + } else { + $tpl->tpl_vars[$tpl_var] = clone $tpl->tpl_vars[$tpl_var]; + if ($scope != Smarty::SCOPE_LOCAL) { + $tpl->tpl_vars[$tpl_var]->scope = $scope; + } + if (!(is_array($tpl->tpl_vars[$tpl_var]->value) || + $tpl->tpl_vars[$tpl_var]->value instanceof ArrayAccess) + ) { + settype($tpl->tpl_vars[$tpl_var]->value, 'array'); + } + } + } +} diff --git a/libs/sysplugins/smarty_internal_template.php b/libs/sysplugins/smarty_internal_template.php index 0daac390..37690c8b 100644 --- a/libs/sysplugins/smarty_internal_template.php +++ b/libs/sysplugins/smarty_internal_template.php @@ -18,6 +18,11 @@ * @property Smarty_Template_Compiled $compiled * @property Smarty_Template_Cached $cached * @property Smarty_Internal_Runtime_Inheritance $_inheritance + * @property Smarty_Internal_Runtime_Subtemplate $_subtemplate + * @property Smarty_Internal_Runtime_Inline $_inline + * @property Smarty_Internal_Runtime_Tplfunc $_tplfunc + * @property Smarty_Internal_Runtime_Var $_var + * @property Smarty_Internal_Runtime_Foreach $_foreach * @method bool mustCompile() */ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase @@ -203,9 +208,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase } if ($this->smarty->debugging) { $this->smarty->_debug->end_template($this); - } - // debug output - if ($this->smarty->debugging) { + // debug output $this->smarty->_debug->display_debug($this, true); } if ($merge_tpl_vars) { @@ -222,9 +225,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase } if ($this->smarty->debugging) { $this->smarty->_debug->end_template($this); - } - if ($this->smarty->debugging == 2 and $display === false) { - if ($this->smarty->debugging) { + if ($this->smarty->debugging == 2 and $display === false) { $this->smarty->_debug->display_debug($this, true); } } @@ -283,192 +284,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase $this->smarty->_getTemplateId($this->template_resource, $this->cache_id, $this->compile_id); } - /** - * Template code runtime function to set up an inline subtemplate - * - * @param string $template template name - * @param mixed $cache_id cache id - * @param mixed $compile_id compile id - * @param integer $caching cache mode - * @param integer $cache_lifetime life time of cache data - * @param array $data passed parameter template variables - * @param int $parent_scope scope in which {include} should execute - * @param bool $cache_tpl_obj cache template object - * @param string|null $uid source uid - * - * @return \Smarty_Internal_Template template object - * @throws \SmartyException - */ - public function setupSubtemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, - $cache_tpl_obj, $uid = null) - { - // if there are cached template objects calculate $templateID - $_templateId = isset($this->smarty->_cache['template_objects']) ? - $this->smarty->_getTemplateId($template, $cache_id, $compile_id) : null; - // already in template cache? - /* @var Smarty_Internal_Template $tpl */ - if (isset($this->smarty->_cache['template_objects'][$_templateId])) { - // clone cached template object because of possible recursive call - $tpl = clone $this->smarty->_cache['template_objects'][$_templateId]; - $tpl->parent = $this; - // if $caching mode changed the compiled resource is invalid - if ((bool) $tpl->caching !== (bool) $caching) { - unset($tpl->compiled); - } - // get variables from calling scope - if ($parent_scope == Smarty::SCOPE_LOCAL) { - $tpl->tpl_vars = $this->tpl_vars; - $tpl->config_vars = $this->config_vars; - } - $tpl->tpl_function = $this->tpl_function; - // copy inheritance object? - if (isset($this->_inheritance)) { - $tpl->_inheritance = $this->_inheritance; - } else { - unset($tpl->_inheritance); - } - } else { - $tpl = clone $this; - $tpl->parent = $this; - if (!isset($tpl->templateId) || $tpl->templateId !== $_templateId) { - $tpl->templateId = $_templateId; - $tpl->template_resource = $template; - $tpl->cache_id = $cache_id; - $tpl->compile_id = $compile_id; - // $uid is set if template is inline - if (isset($uid)) { - // inline templates have same compiled resource - $tpl->compiled = $this->compiled; - // if template is called multiple times set flag to to cache template objects - if (isset($tpl->compiled->includes[$template]) && $tpl->compiled->includes[$template] > 1) { - $cache_tpl_obj = true; - } - if (isset($tpl->compiled->file_dependency[$uid])) { - list($filepath, $timestamp, $resource) = $tpl->compiled->file_dependency[$uid]; - $tpl->source = - new Smarty_Template_Source(isset($tpl->smarty->_cache['resource_handlers'][$resource]) ? - $tpl->smarty->_cache['resource_handlers'][$resource] : - Smarty_Resource::load($tpl->smarty, $resource), $tpl->smarty, - $filepath, $resource, $filepath); - $tpl->source->filepath = $filepath; - $tpl->source->timestamp = $timestamp; - $tpl->source->exist = true; - $tpl->source->uid = $uid; - } else { - $tpl->source = null; - } - } else { - $tpl->source = null; - unset($tpl->compiled); - } - if (!isset($tpl->source)) { - $tpl->source = Smarty_Template_Source::load($tpl); - } - unset($tpl->cached); - // check if template object should be cached - if (!$tpl->source->handler->recompiled && (isset($tpl->parent->templateId) && - isset($tpl->smarty->_cache['template_objects'][$tpl->parent->templateId]) || - ($cache_tpl_obj && $tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) || - $tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) - ) { - $tpl->smarty->_cache['template_objects'][$tpl->_getTemplateId()] = $tpl; - } - } - } - $tpl->caching = $caching; - $tpl->cache_lifetime = $cache_lifetime; - if ($caching == 9999) { - $tpl->cached = $this->cached; - } - // get variables from calling scope - if ($parent_scope != Smarty::SCOPE_LOCAL) { - if ($parent_scope == Smarty::SCOPE_PARENT) { - $tpl->tpl_vars = &$this->tpl_vars; - $tpl->config_vars = &$this->config_vars; - } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) { - $tpl->tpl_vars = &Smarty::$global_tpl_vars; - $tpl->config_vars = $this->config_vars; - } elseif ($parent_scope == Smarty::SCOPE_ROOT) { - $ptr = $tpl->parent; - while (!empty($ptr->parent)) { - $ptr = $ptr->parent; - } - $tpl->tpl_vars = &$ptr->tpl_vars; - $tpl->config_vars = &$ptr->config_vars; - } else { - $tpl->tpl_vars = $this->tpl_vars; - $tpl->config_vars = $this->config_vars; - } - } - - if (!empty($data)) { - // set up variable values - foreach ($data as $_key => $_val) { - $tpl->tpl_vars[$_key] = new Smarty_Variable($_val); - } - } - return $tpl; - } - - /** - * Template code runtime function to render inline subtemplate - * - * @param Smarty_Internal_Template $tpl - * @param string $content_func name of content function - * - * @throws \Exception - */ - public function renderInline(Smarty_Internal_Template $tpl, $content_func) - { - if ($this->smarty->debugging) { - $this->smarty->_debug->start_template($tpl); - $this->smarty->_debug->start_render($tpl); - } - $tpl->compiled->getRenderedTemplateCode($tpl, $content_func); - if ($this->smarty->debugging) { - $this->smarty->_debug->end_template($tpl); - $this->smarty->_debug->end_render($tpl); - } - if ($tpl->caching == 9999 && $tpl->compiled->has_nocache_code) { - $this->cached->hashes[$tpl->compiled->nocache_hash] = true; - } - } - - /** - * Call template function - * - * @param string $name template function name - * @param object|\Smarty_Internal_Template $_smarty_tpl template object - * @param array $params parameter array - * @param bool $nocache true if called nocache - * - * @throws \SmartyException - */ - public function callTemplateFunction($name, Smarty_Internal_Template $_smarty_tpl, $params, $nocache) - { - if (isset($_smarty_tpl->tpl_function[$name])) { - if (!$_smarty_tpl->caching || ($_smarty_tpl->caching && $nocache)) { - $function = $_smarty_tpl->tpl_function[$name]['call_name']; - } else { - if (isset($_smarty_tpl->tpl_function[$name]['call_name_caching'])) { - $function = $_smarty_tpl->tpl_function[$name]['call_name_caching']; - } else { - $function = $_smarty_tpl->tpl_function[$name]['call_name']; - } - } - if (function_exists($function)) { - $function ($_smarty_tpl, $params); - return; - } - // try to load template function dynamically - if (Smarty_Internal_Function_Call_Handler::call($name, $_smarty_tpl, $function)) { - $function ($_smarty_tpl, $params); - return; - } - } - throw new SmartyException("Unable to find template function '{$name}'"); - } - /** * This function is executed automatically when a compiled or cached template file is included * - Decode saved properties from compiled template and cache files @@ -538,62 +353,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase return $is_valid; } - /** - * Template code runtime function to create a local Smarty variable for array assignments - * - * @param string $tpl_var template variable name - * @param bool $nocache cache mode of variable - * @param int $scope scope of variable - */ - public function createLocalArrayVariable($tpl_var, $nocache = false, $scope = Smarty::SCOPE_LOCAL) - { - if (!isset($this->tpl_vars[$tpl_var])) { - $this->tpl_vars[$tpl_var] = new Smarty_Variable(array(), $nocache, $scope); - } else { - $this->tpl_vars[$tpl_var] = clone $this->tpl_vars[$tpl_var]; - if ($scope != Smarty::SCOPE_LOCAL) { - $this->tpl_vars[$tpl_var]->scope = $scope; - } - if (!(is_array($this->tpl_vars[$tpl_var]->value) || - $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess) - ) { - settype($this->tpl_vars[$tpl_var]->value, 'array'); - } - } - } - - /** - * [util function] counts an array, arrayAccess/traversable or PDOStatement object - * - * @param mixed $value - * - * @return int the count for arrays and objects that implement countable, 1 for other objects that don't, and 0 - * for empty elements - */ - public function _count($value) - { - if (is_array($value) === true || $value instanceof Countable) { - return count($value); - } elseif ($value instanceof IteratorAggregate) { - // Note: getIterator() returns a Traversable, not an Iterator - // thus rewind() and valid() methods may not be present - return iterator_count($value->getIterator()); - } elseif ($value instanceof Iterator) { - return iterator_count($value); - } elseif ($value instanceof PDOStatement) { - return $value->rowCount(); - } elseif ($value instanceof Traversable) { - return iterator_count($value); - } elseif ($value instanceof ArrayAccess) { - if ($value->offsetExists(0)) { - return 1; - } - } elseif (is_object($value)) { - return count($value); - } - return 0; - } - /** * runtime error not matching capture tags */