- move subtemplate code into runtime extension and optimize for size and speed

This commit is contained in:
uwetews
2015-09-01 02:27:22 +02:00
parent 37e6529c7b
commit 2ba7109043
8 changed files with 291 additions and 202 deletions

View File

@@ -118,7 +118,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.28-dev/48';
const SMARTY_VERSION = '3.1.28-dev/54';
/**
* define variable scopes
@@ -207,7 +207,7 @@ class Smarty extends Smarty_Internal_TemplateBase
const RESOURCE_CACHE_TEMPLATE = 2; // cache all template objects
const RESOURCE_CACHE_ON = 4; // cache source nad compiled resources
const RESOURCE_CACHE_ON = 4; // cache source and compiled resources
/**#@-*/
@@ -675,7 +675,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
private static $obsoleteProperties = array('resource_caching', 'template_resource_caching',
'direct_access_security', '_dir_perms', '_file_perms',
'plugin_search_order');
'plugin_search_order', 'inheritance_merge_compiled_includes');
private static $accessMap = array('template_dir' => 'getTemplateDir', 'config_dir' => 'getConfigDir',
'plugins_dir' => 'getPluginsDir', 'compile_dir' => 'getCompileDir',
@@ -1064,15 +1064,14 @@ class Smarty extends Smarty_Internal_TemplateBase
} else {
$data = null;
}
$_templateId = $this->_getTemplateId($template, $cache_id, $compile_id);
if (isset($this->_cache['isCached'][$_templateId])) {
if ($this->caching && isset($this->_cache['isCached'][$_templateId = $this->_getTemplateId($template, $cache_id, $compile_id)])) {
$tpl = $do_clone ? clone $this->_cache['isCached'][$_templateId] : $this->_cache['isCached'][$_templateId];
$tpl->parent = $parent;
$tpl->tpl_vars = array();
$tpl->config_vars = array();
} else {
/* @var Smarty_Internal_Template $tpl */
$tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id, null, null, $_templateId);
$tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id, null, null);
}
if ($do_clone) {
$tpl->smarty = clone $tpl->smarty;

View File

@@ -275,10 +275,10 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
}
if (isset($_assign)) {
$_output .= "ob_start();\n";
$_output .= "\$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, {$_isChild}, '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$t_hash]['func']}');\n";
$_output .= "\$_smarty_tpl->_Inline->renderInline(\$_smarty_tpl, {$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, {$_isChild}, '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$t_hash]['func']}', '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$t_hash]['uid']}');\n";
$_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(ob_get_clean());\n";
} else {
$_output .= "\$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, {$_isChild}, '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$t_hash]['func']}');\n";
$_output .= "\$_smarty_tpl->_Inline->renderInline(\$_smarty_tpl, {$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, {$_isChild}, '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$t_hash]['func']}', '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$t_hash]['uid']}');\n";
}
if ($update_compile_id) {
$_output .= $compiler->makeNocacheCode("\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n");
@@ -298,10 +298,10 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
// was there an assign attribute
if (isset($_assign)) {
$_output .= "ob_start();\n";
$_output .= "\$_smarty_tpl->getSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl});\n";
$_output .= "\$_smarty_tpl->_Subtemplate->renderSubtemplate(\$_smarty_tpl, $include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl}, {$_isChild});\n";
$_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(ob_get_clean());\n";
} else {
$_output .= "\$_smarty_tpl->getSubTemplate($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl});\n";
$_output .= "\$_smarty_tpl->_Subtemplate->renderSubtemplate(\$_smarty_tpl, $include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl}, {$_cache_tpl}, {$_isChild});\n";
}
if ($update_compile_id) {
$_output .= "\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n";

View File

@@ -0,0 +1,59 @@
<?php
/**
* Runtime Method renderInline
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*/
class Smarty_Internal_Runtime_Inline
{
/**
* Template code runtime function to render inline subtemplate
*
* @param Smarty_Internal_Template $callerTpl
* @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 bool $isChild flag if subtemplate is an inheritance child
* @param string $content_func name of content function
* @param string $uid source uid
*
* @throws \Exception
*/
public function renderInline(Smarty_Internal_Template $callerTpl, $template, $cache_id, $compile_id,
$caching, $cache_lifetime, $data, $parent_scope, $cache_tpl_obj,
$isChild, $content_func, $uid)
{
// call runtime extension
/* @var Smarty_Internal_Template $tpl */
$tpl = $callerTpl->_Subtemplate->setupSubtemplate($callerTpl, $template, $cache_id, $compile_id,
$caching, $cache_lifetime, $data,
$parent_scope, $cache_tpl_obj, $uid);
$tpl->isChild = $isChild;
if ($callerTpl->smarty->debugging) {
$callerTpl->smarty->_debug->start_template($tpl);
$callerTpl->smarty->_debug->start_render($tpl);
}
$tpl->compiled->getRenderedTemplateCode($tpl, $content_func);
if ($callerTpl->smarty->debugging) {
$callerTpl->smarty->_debug->end_template($tpl);
$callerTpl->smarty->_debug->end_render($tpl);
}
if ($caching == 9999 && $tpl->compiled->has_nocache_code) {
$callerTpl->cached->hashes[$tpl->compiled->nocache_hash] = true;
}
if (!isset($callerTpl->_Block) && $isChild && isset($tpl->_Block) &&
($callerTpl->isChild || !empty($callerTpl->source->components))
) {
$callerTpl->_Block = $tpl->_Block;
}
}
}

View File

@@ -0,0 +1,165 @@
<?php
/**
* Runtime Method _getSubTemplate
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*
**/
class Smarty_Internal_Runtime_Subtemplate
{
/**
* Template code runtime function to get subtemplate content
*
* @param \Smarty_Internal_Template $callerTpl template object of caller
* @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 bool $isChild flag if subtemplate is an inheritance child
*
* @throws \SmartyException
*/
public function renderSubtemplate(Smarty_Internal_Template $callerTpl, $template, $cache_id, $compile_id, $caching,
$cache_lifetime, $data, $parent_scope, $cache_tpl_obj, $isChild)
{
$tpl = $this->setupSubtemplate($callerTpl, $template, $cache_id, $compile_id, $caching, $cache_lifetime, $data,
$parent_scope, $cache_tpl_obj);
$tpl->isChild = $isChild;
$tpl->render();
if ($tpl->isChild && !isset($callerTpl->_Block) && isset($tpl->_Block) &&
($callerTpl->isChild || !empty($callerTpl->source->components))
) {
$callerTpl->_Block = $tpl->_Block;
}
}
/**
* Template code runtime function to set up an inline subtemplate
*
* @param \Smarty_Internal_Template $callerTpl
* @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 $callerTpl, $template, $cache_id, $compile_id, $caching,
$cache_lifetime, $data, $parent_scope, $cache_tpl_obj, $uid = null)
{
$_templateId = isset($callerTpl->smarty->_cache['template_objects']) ?
$callerTpl->smarty->_getTemplateId($template, $cache_id, $compile_id) : null;
// already in template cache?
/* @var Smarty_Internal_Template $tpl */
if (isset($callerTpl->smarty->_cache['template_objects'][$_templateId])) {
// clone cached template object because of possible recursive call
$tpl = clone $callerTpl->smarty->_cache['template_objects'][$_templateId];
$tpl->parent = $callerTpl;
if ((bool) $tpl->caching !== (bool) $caching) {
unset($tpl->compiled);
}
// get variables from calling scope
if ($parent_scope == Smarty::SCOPE_LOCAL) {
$tpl->tpl_vars = $callerTpl->tpl_vars;
$tpl->config_vars = $callerTpl->config_vars;
}
$tpl->tpl_function = $callerTpl->tpl_function;
//if (isset($callerTpl->_cache['inheritanceBlocks'])) {
// $tpl->_cache['inheritanceBlocks'] = $callerTpl->_cache['inheritanceBlocks'];
//}
} else {
$tpl = clone $callerTpl;
$tpl->parent = $callerTpl;
$tpl->isChild = false;
if (!isset($tpl->templateId) || $tpl->templateId !== $_templateId) {
$tpl->templateId = $_templateId;
$tpl->template_resource = $template;
$tpl->cache_id = $cache_id;
$tpl->compile_id = $compile_id;
if (isset($uid)) {
$tpl->compiled = $callerTpl->compiled;
if (isset($tpl->compiled->includes["{$tpl->source->type}:{$tpl->source->name}"]) &&
$tpl->compiled->includes["{$tpl->source->type}:{$tpl->source->name}"] > 1
) {
$cache_tpl_obj = true;
}
if (isset($tpl->compiled->file_dependency[$uid])) {
$info = $tpl->compiled->file_dependency[$uid];
$tpl->source =
new Smarty_Template_Source(isset($tpl->smarty->_cache['resource_handlers'][$info[2]]) ?
$tpl->smarty->_cache['resource_handlers'][$info[2]] :
Smarty_Resource::load($tpl->smarty, $info[2]), $tpl->smarty,
$info[0], $info[2], $info[0]);
$tpl->source->filepath = $info[0];
$tpl->source->timestamp = $info[1];
$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 = $callerTpl->cached;
}
// get variables from calling scope
if ($parent_scope != Smarty::SCOPE_LOCAL) {
if ($parent_scope == Smarty::SCOPE_PARENT) {
$tpl->tpl_vars = &$callerTpl->tpl_vars;
$tpl->config_vars = &$callerTpl->config_vars;
} elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
$tpl->tpl_vars = &Smarty::$global_tpl_vars;
$tpl->config_vars = $callerTpl->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 = $callerTpl->tpl_vars;
$tpl->config_vars = $callerTpl->config_vars;
}
}
if (!empty($data)) {
// set up variable values
foreach ($data as $_key => $_val) {
$tpl->tpl_vars[$_key] = new Smarty_Variable($_val);
}
}
return $tpl;
}
}

View File

@@ -66,9 +66,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
/**
* Template Id
*
* @var string
* @var null|string
*/
public $templateId = '';
public $templateId = null;
/**
* Known template functions
@@ -97,11 +97,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
* @param mixed $_compile_id compile id or null
* @param bool $_caching use caching?
* @param int $_cache_lifetime cache life-time in seconds
* @param string $_templateId optional from extern
*
* @throws \SmartyException
*/
public function __construct($template_resource, Smarty $smarty, Smarty_Internal_Data $_parent = null, $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null, $_templateId = null)
public function __construct($template_resource, Smarty $smarty, Smarty_Internal_Data $_parent = null,
$_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
{
$this->smarty = &$smarty;
// Smarty parameter
@@ -116,12 +116,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
// Template resource
$this->template_resource = $template_resource;
$this->source = Smarty_Template_Source::load($this);
$this->templateId = isset($_templateId) ? $_templateId : $this->smarty->_getTemplateId($template_resource, $this->cache_id, $this->compile_id);
// copy block data of template inheritance
if (isset($this->parent) && $this->parent->_objType == 2) {
$this->block_data = $this->parent->block_data;
}
}
/**
@@ -189,8 +183,8 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
$this->caching = false;
}
// read from cache or render
$isCacheTpl = $this->caching == Smarty::CACHING_LIFETIME_CURRENT ||
$this->caching == Smarty::CACHING_LIFETIME_SAVED;
$isCacheTpl =
$this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED;
if ($isCacheTpl) {
if (!isset($this->cached)) {
$this->loadCached();
@@ -285,152 +279,14 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
}
/**
* Template code runtime function to get subtemplate content
* Get unique template id
*
* @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
*
* @return string template content
* @throws \SmartyException
* @return string
*/
public function getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl_obj, $isChild)
public function _getTemplateId()
{
$tpl = $this->setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl_obj);
$tpl->isChild = $isChild;
$tpl->render();
if ($tpl->isChild && !isset($this->_Block) && isset($tpl->_Block) &&
($this->isChild || !empty($this->source->components))
) {
$this->_Block = $tpl->_Block;
}
}
/**
* 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 bool $isChild flag if subtemplate is an inheritance child
*
* @return \Smarty_Internal_Template template object
*/
public function setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl_obj)
{
$_templateId = $this->smarty->_getTemplateId($template, $cache_id, $compile_id);
// 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 ((bool) $tpl->caching !== (bool) $caching) {
unset($tpl->compiled);
}
if ($parent_scope = Smarty::SCOPE_LOCAL) {
$tpl->tpl_vars = $this->tpl_vars;
$tpl->config_vars = $this->config_vars;
}
$tpl->tpl_function = $this->tpl_function;
} else {
$tpl = clone $this;
$tpl->parent = $this;
$tpl->isChild = false;
if ($tpl->templateId !== $_templateId) {
$tpl->templateId = $_templateId;
$tpl->template_resource = $template;
$tpl->cache_id = $cache_id;
$tpl->compile_id = $compile_id;
$tpl->source = Smarty_Template_Source::load($tpl);
unset($tpl->compiled, $tpl->cached);
$tpl->cacheTpl($cache_tpl_obj);
}
}
$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 (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
$tpl->tpl_vars = &$this->tpl_vars;
$tpl->config_vars = &$this->config_vars;
} else {
$tpl->tpl_vars = &$scope_ptr->tpl_vars;
$tpl->config_vars = &$scope_ptr->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 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 bool $isChild flag if subtemplate is an inheritance child
* @param string $content_func name of content function
*
* @return string template content
* @throws \Exception
*/
public function getInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl_obj, $isChild, $content_func)
{
$tpl = $this->setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl_obj);
if (!isset($tpl->compiled)) {
$tpl->compiled = $this->compiled;
if ($tpl->compiled->includes[$tpl->source->type . ':' . $tpl->source->name] > 1) {
$tpl->cacheTpl(true);
}
}
$tpl->isChild = $isChild;
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 ($caching == 9999 && $tpl->compiled->has_nocache_code) {
$this->cached->hashes[$tpl->compiled->nocache_hash] = true;
}
if (!isset($this->_Block) && $isChild && isset($tpl->_Block) &&
($this->isChild || !empty($this->source->components))
) {
$this->_Block = $tpl->_Block;
}
return isset($this->templateId) ? $this->templateId : $this->templateId =
$this->smarty->_getTemplateId($this->template_resource, $this->cache_id, $this->compile_id);
}
/**
@@ -483,8 +339,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
}
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
* - Check if compiled or cache file is valid
@@ -656,7 +511,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
} elseif (is_object($value)) {
return count($value);
}
return 0;
}
@@ -706,7 +560,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
if (!class_exists($this->source->handler->compiler_class)) {
$this->smarty->loadPlugin($this->source->handler->compiler_class);
}
$this->compiler = new $this->source->handler->compiler_class($this->source->handler->template_lexer_class, $this->source->handler->template_parser_class, $this->smarty);
$this->compiler = new $this->source->handler->compiler_class($this->source->handler->template_lexer_class,
$this->source->handler->template_parser_class,
$this->smarty);
}
/**

View File

@@ -182,7 +182,7 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
$template->loadCached();
}
$result = $template->cached->isCached($template);
$template->smarty->_cache['isCached'][$template->templateId] = $template;
$template->smarty->_cache['isCached'][$template->_getTemplateId()] = $template;
} else {
return false;
}

View File

@@ -19,13 +19,6 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
*/
public $nocache_hash = null;
/**
* create Compiled Object container
*/
public function __construct()
{
}
/**
* get a Compiled Object of this source
*
@@ -36,7 +29,9 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
static function load($_template)
{
// check runtime cache
if (!$_template->source->handler->recompiled && ($_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)) {
if (!$_template->source->handler->recompiled &&
($_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)
) {
$_cache_key = $_template->source->unique_resource . '#';
if ($_template->caching) {
$_cache_key .= 'caching#';
@@ -53,7 +48,9 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$compiled->populateCompiledFilepath($_template);
}
// runtime cache
if (!$_template->source->handler->recompiled && ($_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)) {
if (!$_template->source->handler->recompiled &&
($_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)
) {
$_template->source->compileds[$_cache_key] = $compiled;
}
return $compiled;
@@ -72,8 +69,8 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
((int) $_template->smarty->config_read_hidden + (int) $_template->smarty->config_booleanize * 2 +
(int) $_template->smarty->config_overwrite * 4);
} else {
$_flag = '_' .
((int) $_template->smarty->merge_compiled_includes + (int) $_template->smarty->escape_html * 2);
$_flag =
'_' . ((int) $_template->smarty->merge_compiled_includes + (int) $_template->smarty->escape_html * 2);
}
$_filepath = $_template->source->uid . $_flag;
// if use_sub_dirs, break file into directories
@@ -119,8 +116,8 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
public function process(Smarty_Internal_Template $_template)
{
$_smarty_tpl = $_template;
if ($_template->source->handler->recompiled || !$_template->compiled->exists || $_template->smarty->force_compile ||
($_template->smarty->compile_check &&
if ($_template->source->handler->recompiled || !$_template->compiled->exists ||
$_template->smarty->force_compile || ($_template->smarty->compile_check &&
$_template->source->getTimeStamp() > $_template->compiled->getTimeStamp())
) {
$this->compileTemplateSource($_template);
@@ -154,19 +151,20 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$_template->smarty->compile_check = $compileCheck;
}
}
if (!isset($_template->smarty->_cache['template_objects'][$_template->templateId]) &&
$_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC &&
$_template->parent instanceof Smarty_Internal_Template && isset($_template->parent->compiled)
if ($_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC && isset($_template->parent) &&
isset($_template->parent->compiled) && !$_template->source->isConfig &&
!in_array($_template->source->type, array('eval', 'string')) &&
!empty($_template->parent->compiled->includes) &&
isset($_template->smarty->_cache['template_objects'][$_template->_getTemplateId()])
) {
foreach ($_template->parent->compiled->includes as $key => $count) {
$_template->compiled->includes[$key] = isset($_template->compiled->includes[$key]) ? $_template->compiled->includes[$key] +
$count : $count;
$_template->compiled->includes[$key] =
isset($_template->compiled->includes[$key]) ? $_template->compiled->includes[$key] + $count :
$count;
}
if (!$_template->source->isConfig && !in_array($_template->source->type, array('eval', 'string'))) {
$key = $_template->source->type . ':' . $_template->source->name;
if (isset($_template->compiled->includes[$key]) && $_template->compiled->includes[$key] > 1) {
$_template->smarty->_cache['template_objects'][$_template->templateId] = $_template;
}
$key = $_template->source->type . ':' . $_template->source->name;
if (isset($_template->compiled->includes[$key]) && $_template->compiled->includes[$key] > 1) {
$_template->smarty->_cache['template_objects'][$_template->templateId] = $_template;
}
}
$this->processed = true;
@@ -208,7 +206,8 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$this->process($_template);
}
if (isset($_template->cached)) {
$_template->cached->file_dependency = array_merge($_template->cached->file_dependency, $this->file_dependency);
$_template->cached->file_dependency =
array_merge($_template->cached->file_dependency, $this->file_dependency);
}
$this->getRenderedTemplateCode($_template);
if ($_template->caching && $this->has_nocache_code) {

View File

@@ -125,7 +125,6 @@ class Smarty_Template_Source
* @param string $type type of resource
* @param string $name resource name
*
* @internal param string $unique_resource unique resource name
*/
public function __construct(Smarty_Resource $handler, Smarty $smarty, $resource, $type, $name)
{
@@ -147,7 +146,8 @@ class Smarty_Template_Source
* @return Smarty_Template_Source Source Object
* @throws SmartyException
*/
public static function load(Smarty_Internal_Template $_template = null, Smarty $smarty = null, $template_resource = null)
public static function load(Smarty_Internal_Template $_template = null, Smarty $smarty = null,
$template_resource = null)
{
if ($_template) {
$smarty = $_template->smarty;
@@ -157,9 +157,19 @@ class Smarty_Template_Source
throw new SmartyException('Missing template name');
}
// parse resource_name, load resource handler, identify unique resource name
list($name, $type) = Smarty_Resource::parseResourceName($template_resource, $smarty->default_resource_type);
if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $template_resource, $match)) {
$type = $match[1];
$name = substr($template_resource, strlen($match[0]));
} else {
// no resource given, use default
// or single character before the colon is not a resource type, but part of the filepath
$type = $smarty->default_resource_type;
$name = $template_resource;
}
$handler = Smarty_Resource::load($smarty, $type);
$handler = isset($smarty->_cache['resource_handlers'][$type]) ?
$smarty->_cache['resource_handlers'][$type] :
Smarty_Resource::load($smarty, $type);
// if resource is not recompiling and resource name is not dotted we can check the source cache
if (($smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON) && !$handler->recompiled &&
!(isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/'))
@@ -185,13 +195,13 @@ class Smarty_Template_Source
$is_relative = isset($name[1]) && $name[0] == '.' && ($name[1] == '.' || $name[1] == '/') &&
($type == 'file' ||
(isset($_template->parent->source) && $_template->parent->source->type == 'extends'));
$unique_resource = $handler->buildUniqueResourceName($smarty, $is_relative ? $source->filepath .
$name : $name);
$unique_resource =
$handler->buildUniqueResourceName($smarty, $is_relative ? $source->filepath . $name : $name);
}
$source->unique_resource = $unique_resource;
// save in runtime cache if not relative
if (!$is_relative) {
$smarty->_cache['source_objects'][$unique_resource] = $source;
$smarty->_cache['source_objects'][$unique_resource] = $source;
}
}
return $source;
@@ -223,7 +233,8 @@ class Smarty_Template_Source
}
$this->handler->renderUncompiled($_template->source, $_template);
if (isset($_template->parent) && $_template->parent->_objType == 2 && !empty($_template->tpl_function)) {
$_template->parent->tpl_function = array_merge($_template->parent->tpl_function, $_template->tpl_function);
$_template->parent->tpl_function =
array_merge($_template->parent->tpl_function, $_template->tpl_function);
}
if ($_template->smarty->debugging) {
$_template->smarty->_debug->end_render($_template);