- optimization of resource processing

This commit is contained in:
uwetews
2016-03-10 22:22:46 +01:00
parent 00be382842
commit 4815464b9c
12 changed files with 187 additions and 233 deletions

View File

@@ -1,4 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)  ===== 3.1.30-dev ===== (xx.xx.xx)
10.03.2014
- optimization of resource processing
09.03.2014 09.03.2014
- improvement rework of 'scope' attribute handling see see NEW_FEATURES.txt https://github.com/smarty-php/smarty/issues/194 - improvement rework of 'scope' attribute handling see see NEW_FEATURES.txt https://github.com/smarty-php/smarty/issues/194
https://github.com/smarty-php/smarty/issues/186 https://github.com/smarty-php/smarty/issues/179 https://github.com/smarty-php/smarty/issues/186 https://github.com/smarty-php/smarty/issues/179

View File

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

View File

@@ -71,12 +71,9 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
throw new SmartyException("PHP templates are disabled"); throw new SmartyException("PHP templates are disabled");
} }
if (!$source->exists) { if (!$source->exists) {
if (isset($_template->parent) && $_template->parent->_objType == 2) { $parentIsTpl = isset($this->parent) && $this->parent->_objType == 2;
$parent_resource = " in '{$_template->parent->template_resource}'"; throw new SmartyException("Unable to load template {$source->type} '{$source->name}'" .
} else { ($parentIsTpl ? " in '{$this->parent->template_resource}'" : ''));
$parent_resource = '';
}
throw new SmartyException("Unable to load template {$source->type} '{$source->name}'{$parent_resource}");
} }
// prepare variables // prepare variables
@@ -100,8 +97,11 @@ class Smarty_Internal_Resource_Php extends Smarty_Internal_Resource_File
*/ */
public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template) public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
{ {
$compiled->filepath = false; $compiled->filepath = $_template->source->filepath;
$compiled->timestamp = false; $compiled->timestamp = $_template->source->timestamp;
$compiled->exists = false; $compiled->exists = $_template->source->exists;
$compiled->file_dependency[ $_template->source->uid ] =
array($compiled->filepath, $this->template->source->getTimeStamp(),
$this->template->source->type,);
} }
} }

View File

@@ -82,16 +82,11 @@ class Smarty_Internal_Runtime_UpdateCache
*/ */
public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter) public function updateCache(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template, $no_output_filter)
{ {
if ($_template->source->handler->uncompiled) {
ob_start();
$_template->source->render($_template);
} else {
ob_start(); ob_start();
if (!isset($_template->compiled)) { if (!isset($_template->compiled)) {
$_template->loadCompiled(); $_template->loadCompiled();
} }
$_template->compiled->render($_template); $_template->compiled->render($_template);
}
if ($_template->smarty->debugging) { if ($_template->smarty->debugging) {
$_template->smarty->_debug->start_cache($_template); $_template->smarty->_debug->start_cache($_template);
} }

View File

@@ -14,7 +14,6 @@
* @package Smarty * @package Smarty
* @subpackage Template * @subpackage Template
* *
* @property Smarty_Template_Source|Smarty_Template_Config $source
* @property Smarty_Template_Compiled $compiled * @property Smarty_Template_Compiled $compiled
* @property Smarty_Template_Cached $cached * @property Smarty_Template_Cached $cached
* *
@@ -107,7 +106,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
public function __construct($template_resource, Smarty $smarty, Smarty_Internal_Data $_parent = null, public function __construct($template_resource, Smarty $smarty, Smarty_Internal_Data $_parent = null,
$_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null) $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
{ {
$this->smarty = &$smarty; $this->smarty = $smarty;
// Smarty parameter // Smarty parameter
$this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id; $this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
$this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id; $this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
@@ -140,12 +139,8 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
} }
// checks if template exists // checks if template exists
if (!$this->source->exists) { if (!$this->source->exists) {
if ($parentIsTpl) { throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'" .
$parent_resource = " in '{$this->parent->template_resource}'"; ($parentIsTpl ? " in '{$this->parent->template_resource}'" : ''));
} else {
$parent_resource = '';
}
throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
} }
// disable caching for evaluated code // disable caching for evaluated code
if ($this->source->handler->recompiled) { if ($this->source->handler->recompiled) {
@@ -158,14 +153,12 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
if (!isset($this->cached) || $this->cached->cache_id !== $this->cache_id || if (!isset($this->cached) || $this->cached->cache_id !== $this->cache_id ||
$this->cached->compile_id !== $this->compile_id $this->cached->compile_id !== $this->compile_id
) { ) {
$this->loadCached(); $this->loadCached(true);
} }
$this->cached->render($this, $no_output_filter); $this->cached->render($this, $no_output_filter);
} elseif ($this->source->handler->uncompiled) {
$this->source->render($this);
} else { } else {
if (!isset($this->compiled) || $this->compiled->compile_id !== $this->compile_id) { if (!isset($this->compiled) || $this->compiled->compile_id !== $this->compile_id) {
$this->loadCompiled(); $this->loadCompiled(true);
} }
$this->compiled->render($this); $this->compiled->render($this);
} }
@@ -242,22 +235,20 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
{ {
$tpl = clone $this; $tpl = clone $this;
$tpl->parent = $this; $tpl->parent = $this;
$_templateId = $this->smarty->_getTemplateId($template, $cache_id, $compile_id, $caching); $smarty = &$this->smarty;
$_templateId = $smarty->_getTemplateId($template, $cache_id, $compile_id, $caching);
// recursive call ? // recursive call ?
if ($tpl->_getTemplateId() != $_templateId) { if ($tpl->_getTemplateId() != $_templateId) {
// already in template cache? // already in template cache?
if (isset($this->smarty->_cache[ 'tplObjects' ][ $_templateId ])) { if (isset($smarty->_cache[ 'tplObjects' ][ $_templateId ])) {
// copy data from cached object // copy data from cached object
$cachedTpl = &$this->smarty->_cache[ 'tplObjects' ][ $_templateId ]; $cachedTpl = &$smarty->_cache[ 'tplObjects' ][ $_templateId ];
$tpl->templateId = $cachedTpl->templateId; $tpl->templateId = $cachedTpl->templateId;
$tpl->template_resource = $cachedTpl->template_resource; $tpl->template_resource = $cachedTpl->template_resource;
$tpl->cache_id = $cachedTpl->cache_id; $tpl->cache_id = $cachedTpl->cache_id;
$tpl->compile_id = $cachedTpl->compile_id; $tpl->compile_id = $cachedTpl->compile_id;
$tpl->source = $cachedTpl->source; $tpl->source = $cachedTpl->source;
// if $caching mode changed the compiled resource is invalid if (isset($cachedTpl->compiled)) {
if ((bool) $tpl->caching !== (bool) $caching) {
unset($tpl->compiled);
} elseif (isset($cachedTpl->compiled)) {
$tpl->compiled = $cachedTpl->compiled; $tpl->compiled = $cachedTpl->compiled;
} else { } else {
unset($tpl->compiled); unset($tpl->compiled);
@@ -275,11 +266,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
if (isset($uid)) { if (isset($uid)) {
// for inline templates we can get all resource information from file dependency // for inline templates we can get all resource information from file dependency
list($filepath, $timestamp, $type) = $tpl->compiled->file_dependency[ $uid ]; list($filepath, $timestamp, $type) = $tpl->compiled->file_dependency[ $uid ];
$tpl->source = $tpl->source = new Smarty_Template_Source($smarty, $filepath, $type, $filepath);
new Smarty_Template_Source(isset($tpl->smarty->_cache[ 'resource_handlers' ][ $type ]) ?
$tpl->smarty->_cache[ 'resource_handlers' ][ $type ] :
Smarty_Resource::load($tpl->smarty, $type), $tpl->smarty,
$filepath, $type, $filepath);
$tpl->source->filepath = $filepath; $tpl->source->filepath = $filepath;
$tpl->source->timestamp = $timestamp; $tpl->source->timestamp = $timestamp;
$tpl->source->exists = true; $tpl->source->exists = true;
@@ -300,13 +287,13 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
$tpl->cache_lifetime = $cache_lifetime; $tpl->cache_lifetime = $cache_lifetime;
// set template scope // set template scope
$tpl->scope = $scope; $tpl->scope = $scope;
if (!isset($tpl->smarty->_cache[ 'tplObjects' ][ $tpl->templateId ]) && !$tpl->source->handler->recompiled) { if (!isset($smarty->_cache[ 'tplObjects' ][ $tpl->templateId ]) && !$tpl->source->handler->recompiled) {
// check if template object should be cached // check if template object should be cached
if ($forceTplCache || (isset($tpl->smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ]) && if ($forceTplCache || (isset($smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ]) &&
$tpl->smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ] > 1) || $smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ] > 1) ||
($tpl->_isParentTemplate() && isset($tpl->smarty->_cache[ 'tplObjects' ][ $tpl->parent->templateId ])) ($tpl->_isParentTemplate() && isset($smarty->_cache[ 'tplObjects' ][ $tpl->parent->templateId ]))
) { ) {
$tpl->smarty->_cache[ 'tplObjects' ][ $tpl->templateId ] = $tpl; $smarty->_cache[ 'tplObjects' ][ $tpl->templateId ] = $tpl;
} }
} }
@@ -316,18 +303,24 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
$tpl->tpl_vars[ $_key ] = new Smarty_Variable($_val, $this->isRenderingCache); $tpl->tpl_vars[ $_key ] = new Smarty_Variable($_val, $this->isRenderingCache);
} }
} }
if ($tpl->caching == 9999 && $tpl->compiled->has_nocache_code) { if ($tpl->caching == 9999) {
if (!isset($tpl->compiled)) {
$this->loadCompiled(true);
}
if ($tpl->compiled->has_nocache_code) {
$this->cached->hashes[ $tpl->compiled->nocache_hash ] = true; $this->cached->hashes[ $tpl->compiled->nocache_hash ] = true;
} }
}
$tpl->_cache = array();
if (isset($uid)) { if (isset($uid)) {
if ($this->smarty->debugging) { if ($smarty->debugging) {
$this->smarty->_debug->start_template($tpl); $smarty->_debug->start_template($tpl);
$this->smarty->_debug->start_render($tpl); $smarty->_debug->start_render($tpl);
} }
$tpl->compiled->getRenderedTemplateCode($tpl, $content_func); $tpl->compiled->getRenderedTemplateCode($tpl, $content_func);
if ($this->smarty->debugging) { if ($smarty->debugging) {
$this->smarty->_debug->end_template($tpl); $smarty->_debug->end_template($tpl);
$this->smarty->_debug->end_render($tpl); $smarty->_debug->end_render($tpl);
} }
} else { } else {
if (isset($tpl->compiled)) { if (isset($tpl->compiled)) {
@@ -511,10 +504,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
/** /**
* Load compiled object * Load compiled object
* *
* @param bool $force force new compiled object
*/ */
public function loadCompiled() public function loadCompiled($force = false)
{ {
if (!isset($this->compiled)) { if ($force || !isset($this->compiled)) {
$this->compiled = Smarty_Template_Compiled::load($this); $this->compiled = Smarty_Template_Compiled::load($this);
} }
} }
@@ -522,10 +516,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
/** /**
* Load cached object * Load cached object
* *
* @param bool $force force new cached object
*/ */
public function loadCached() public function loadCached($force = false)
{ {
if (!isset($this->cached)) { if ($force || !isset($this->cached)) {
$this->cached = Smarty_Template_Cached::load($this); $this->cached = Smarty_Template_Cached::load($this);
} }
} }
@@ -537,11 +532,11 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/ */
public function loadCompiler() public function loadCompiler()
{ {
if (!class_exists($this->source->handler->compiler_class)) { if (!class_exists($this->source->compiler_class)) {
$this->smarty->loadPlugin($this->source->handler->compiler_class); $this->smarty->loadPlugin($this->source->compiler_class);
} }
$this->compiler = new $this->source->handler->compiler_class($this->source->handler->template_lexer_class, $this->compiler =
$this->source->handler->template_parser_class, new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class,
$this->smarty); $this->smarty);
} }

View File

@@ -49,27 +49,6 @@ abstract class Smarty_Resource
*/ */
public $hasCompiledHandler = false; public $hasCompiledHandler = false;
/**
* Name of the Class to compile this resource's contents with
*
* @var string
*/
public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
/**
* Name of the Class to tokenize this resource's contents with
*
* @var string
*/
public $template_lexer_class = 'Smarty_Internal_Templatelexer';
/**
* Name of the Class to parse this resource's contents with
*
* @var string
*/
public $template_parser_class = 'Smarty_Internal_Templateparser';
/** /**
* Load template's source into current template object * Load template's source into current template object
* *

View File

@@ -44,36 +44,14 @@ abstract class Smarty_Resource_Uncompiled extends Smarty_Resource
* populate compiled object with compiled filepath * populate compiled object with compiled filepath
* *
* @param Smarty_Template_Compiled $compiled compiled object * @param Smarty_Template_Compiled $compiled compiled object
* @param Smarty_Internal_Template $_template template object (is ignored) * @param Smarty_Internal_Template $_template template object
*/ */
public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template) public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
{ {
$compiled->filepath = false; $compiled->filepath = $_template->source->filepath;
$compiled->timestamp = false; $compiled->timestamp = $_template->source->timestamp;
$compiled->exists = false; $compiled->exists = $_template->source->exists;
} $compiled->file_dependency[ $_template->source->uid ] =
array($compiled->filepath, $compiled->timestamp, $_template->source->type,);
/**
* render compiled template code
*
* @param Smarty_Internal_Template $_template
*
* @return string
* @throws Exception
*/
public function render($_template)
{
$level = ob_get_level();
ob_start();
try {
$this->renderUncompiled($_template->source, $_template);
return ob_get_clean();
}
catch (Exception $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}
throw $e;
}
} }
} }

View File

@@ -72,6 +72,13 @@ class Smarty_Template_Cached extends Smarty_Template_Resource_Base
*/ */
public $hashes = array(); public $hashes = array();
/**
* Flag if this is a cache resource
*
* @var bool
*/
public $isCache = true;
/** /**
* create Cached Object container * create Cached Object container
* *

View File

@@ -96,11 +96,10 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
*/ */
public function process(Smarty_Internal_Template $_template) public function process(Smarty_Internal_Template $_template)
{ {
if (!$_template->source->handler->uncompiled) {
$_smarty_tpl = $_template; $_smarty_tpl = $_template;
if ($_template->source->handler->recompiled || !$_template->compiled->exists || if ($_template->source->handler->recompiled || !$this->exists || $_template->smarty->force_compile ||
$_template->smarty->force_compile || ($_template->smarty->compile_check && ($_template->smarty->compile_check && $_template->source->getTimeStamp() > $this->getTimeStamp())
$_template->source->getTimeStamp() >
$_template->compiled->getTimeStamp())
) { ) {
$this->compileTemplateSource($_template); $this->compileTemplateSource($_template);
$compileCheck = $_template->smarty->compile_check; $compileCheck = $_template->smarty->compile_check;
@@ -125,7 +124,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$_template->smarty->compile_check = $compileCheck; $_template->smarty->compile_check = $compileCheck;
} else { } else {
$_template->mustCompile = true; $_template->mustCompile = true;
@include($_template->compiled->filepath); @include($this->filepath);
if ($_template->mustCompile) { if ($_template->mustCompile) {
$this->compileTemplateSource($_template); $this->compileTemplateSource($_template);
$compileCheck = $_template->smarty->compile_check; $compileCheck = $_template->smarty->compile_check;
@@ -137,6 +136,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$_template->_subTemplateRegister(); $_template->_subTemplateRegister();
$this->processed = true; $this->processed = true;
} }
}
/** /**
* Load fresh compiled template by including the PHP file * Load fresh compiled template by including the PHP file
@@ -147,15 +147,15 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
private function loadCompiledTemplate(Smarty_Internal_Template $_template) private function loadCompiledTemplate(Smarty_Internal_Template $_template)
{ {
if (function_exists('opcache_invalidate')) { if (function_exists('opcache_invalidate')) {
opcache_invalidate($_template->compiled->filepath, true); opcache_invalidate($this->filepath, true);
} elseif (function_exists('apc_compile_file')) { } elseif (function_exists('apc_compile_file')) {
apc_compile_file($_template->compiled->filepath); apc_compile_file($this->filepath);
} }
$_smarty_tpl = $_template; $_smarty_tpl = $_template;
if (defined('HHVM_VERSION')) { if (defined('HHVM_VERSION')) {
eval("?>" . file_get_contents($_template->compiled->filepath)); eval("?>" . file_get_contents($this->filepath));
} else { } else {
include($_template->compiled->filepath); include($this->filepath);
} }
} }
@@ -179,7 +179,11 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$_template->cached->file_dependency = $_template->cached->file_dependency =
array_merge($_template->cached->file_dependency, $this->file_dependency); array_merge($_template->cached->file_dependency, $this->file_dependency);
} }
if ($_template->source->handler->uncompiled) {
$_template->source->handler->renderUncompiled($_template->source, $_template);
} else {
$this->getRenderedTemplateCode($_template); $this->getRenderedTemplateCode($_template);
}
if ($_template->caching && $this->has_nocache_code) { if ($_template->caching && $this->has_nocache_code) {
$_template->cached->hashes[ $this->nocache_hash ] = true; $_template->cached->hashes[ $this->nocache_hash ] = true;
} }
@@ -207,8 +211,8 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$this->unifunc = null; $this->unifunc = null;
// compile locking // compile locking
if (!$_template->source->handler->recompiled) { if (!$_template->source->handler->recompiled) {
if ($saved_timestamp = $_template->compiled->getTimeStamp()) { if ($saved_timestamp = $this->getTimeStamp()) {
touch($_template->compiled->filepath); touch($this->filepath);
} }
} }
// call compiler // call compiler
@@ -219,7 +223,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
catch (Exception $e) { catch (Exception $e) {
// restore old timestamp in case of error // restore old timestamp in case of error
if (!$_template->source->handler->recompiled && $saved_timestamp) { if (!$_template->source->handler->recompiled && $saved_timestamp) {
touch($_template->compiled->filepath, $saved_timestamp); touch($this->filepath, $saved_timestamp);
} }
throw $e; throw $e;
} }

View File

@@ -40,26 +40,27 @@ class Smarty_Template_Config extends Smarty_Template_Source
public $isConfig = true; public $isConfig = true;
/** /**
* create Source Object container * Name of the Class to compile this resource's contents with
* *
* @param Smarty_Resource $handler Resource Handler this source object communicates with * @var string
* @param Smarty $smarty Smarty instance this source object belongs to
* @param string $resource full template_resource
* @param string $type type of resource
* @param string $name resource name
*/ */
public function __construct(Smarty_Resource $handler, Smarty $smarty, $resource, $type, $name) public $compiler_class = 'Smarty_Internal_Config_File_Compiler';
{
// must clone handler as we change class names /**
$this->handler = clone $handler; // Note: prone to circular references * Name of the Class to tokenize this resource's contents with
$this->handler->compiler_class = 'Smarty_Internal_Config_File_Compiler'; *
$this->handler->template_lexer_class = 'Smarty_Internal_Configfilelexer'; * @var string
$this->handler->template_parser_class = 'Smarty_Internal_Configfileparser'; */
$this->resource = $resource; public $template_lexer_class = 'Smarty_Internal_Configfilelexer';
$this->type = $type;
$this->name = $name; /**
$this->smarty = $smarty; * Name of the Class to parse this resource's contents with
} *
* @var string
*/
public $template_parser_class = 'Smarty_Internal_Configfileparser';
/** /**
* initialize Source Object for given resource * initialize Source Object for given resource
@@ -87,9 +88,8 @@ class Smarty_Template_Config extends Smarty_Template_Source
if (isset($_incompatible_resources[ $type ])) { if (isset($_incompatible_resources[ $type ])) {
throw new SmartyException ("Unable to use resource '{$type}' for config"); throw new SmartyException ("Unable to use resource '{$type}' for config");
} }
$resource = Smarty_Resource::load($_template->smarty, $type); $source = new Smarty_Template_Config($_template->smarty, $template_resource, $type, $name);
$source = new Smarty_Template_Config($resource, $_template->smarty, $template_resource, $type, $name); $source->handler->populate($source, $_template);
$resource->populate($source, $_template);
if (!$source->exists && isset($_template->smarty->default_config_handler_func)) { if (!$source->exists && isset($_template->smarty->default_config_handler_func)) {
Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source); Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source);
} }

View File

@@ -86,6 +86,13 @@ abstract class Smarty_Template_Resource_Base
*/ */
public $includes = array(); public $includes = array();
/**
* Flag if this is a cache resource
*
* @var bool
*/
public $isCache = false;
/** /**
* Process resource * Process resource
* *
@@ -104,15 +111,18 @@ abstract class Smarty_Template_Resource_Base
*/ */
public function getRenderedTemplateCode(Smarty_Internal_Template $_template, $unifunc = null) public function getRenderedTemplateCode(Smarty_Internal_Template $_template, $unifunc = null)
{ {
$_template->isRenderingCache = $this instanceof Smarty_Template_Cached; $smarty = &$_template->smarty;
$unifunc = isset($unifunc) ? $unifunc : $this->unifunc; $_template->isRenderingCache = $this->isCache;
$level = ob_get_level(); $level = ob_get_level();
try { try {
if (empty($unifunc) || !is_callable($unifunc)) { if (!isset($unifunc)) {
$unifunc = $this->unifunc;
}
if (empty($unifunc) || !function_exists($unifunc)) {
throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'"); throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'");
} }
if (isset($_template->smarty->security_policy)) { if (isset($smarty->security_policy)) {
$_template->smarty->security_policy->startTemplate($_template); $smarty->security_policy->startTemplate($_template);
} }
// //
// render compiled or saved template code // render compiled or saved template code
@@ -126,8 +136,8 @@ abstract class Smarty_Template_Resource_Base
if ($_saved_capture_level != count($_template->_cache[ 'capture_stack' ])) { if ($_saved_capture_level != count($_template->_cache[ 'capture_stack' ])) {
$_template->capture_error(); $_template->capture_error();
} }
if (isset($_template->smarty->security_policy)) { if (isset($smarty->security_policy)) {
$_template->smarty->security_policy->exitTemplate(); $smarty->security_policy->exitTemplate();
} }
$_template->isRenderingCache = false; $_template->isRenderingCache = false;
return null; return null;
@@ -137,8 +147,8 @@ abstract class Smarty_Template_Resource_Base
while (ob_get_level() > $level) { while (ob_get_level() > $level) {
ob_end_clean(); ob_end_clean();
} }
if (isset($_template->smarty->security_policy)) { if (isset($smarty->security_policy)) {
$_template->smarty->security_policy->exitTemplate(); $smarty->security_policy->exitTemplate();
} }
throw $e; throw $e;
} }

View File

@@ -102,6 +102,27 @@ class Smarty_Template_Source
*/ */
public $content = null; public $content = null;
/**
* Name of the Class to compile this resource's contents with
*
* @var string
*/
public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
/**
* Name of the Class to tokenize this resource's contents with
*
* @var string
*/
public $template_lexer_class = 'Smarty_Internal_Templatelexer';
/**
* Name of the Class to parse this resource's contents with
*
* @var string
*/
public $template_parser_class = 'Smarty_Internal_Templateparser';
/** /**
* create Source Object container * create Source Object container
* *
@@ -112,9 +133,10 @@ class Smarty_Template_Source
* @param string $name resource name * @param string $name resource name
* *
*/ */
public function __construct(Smarty_Resource $handler, Smarty $smarty, $resource, $type, $name) public function __construct(Smarty $smarty, $resource, $type, $name)
{ {
$this->handler = $handler; // Note: prone to circular references $this->handler = isset($smarty->_cache[ 'resource_handlers' ][ $type ]) ? $smarty->_cache[ 'resource_handlers' ][ $type ] :
Smarty_Resource::load($smarty, $type);
$this->smarty = $smarty; $this->smarty = $smarty;
$this->resource = $resource; $this->resource = $resource;
$this->type = $type; $this->type = $type;
@@ -152,54 +174,15 @@ class Smarty_Template_Source
$type = $smarty->default_resource_type; $type = $smarty->default_resource_type;
$name = $template_resource; $name = $template_resource;
} }
$handler =
isset($smarty->_cache[ 'resource_handlers' ][ $type ]) ? $smarty->_cache[ 'resource_handlers' ][ $type ] :
Smarty_Resource::load($smarty, $type);
// create new source object // create new source object
$source = new Smarty_Template_Source($handler, $smarty, $template_resource, $type, $name); $source = new Smarty_Template_Source($smarty, $template_resource, $type, $name);
$handler->populate($source, $_template); $source->handler->populate($source, $_template);
if (!$source->exists && isset($_template->smarty->default_template_handler_func)) { if (!$source->exists && isset($_template->smarty->default_template_handler_func)) {
Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source); Smarty_Internal_Method_RegisterDefaultTemplateHandler::_getDefaultTemplate($source);
} }
return $source; return $source;
} }
/**
* render the uncompiled source
*
* @param Smarty_Internal_Template $_template template object
*
* @return string
* @throws \Exception
*/
public function renderUncompiled(Smarty_Internal_Template $_template)
{
$this->handler->renderUncompiled($_template->source, $_template);
}
/**
* Render uncompiled source
*
* @param \Smarty_Internal_Template $_template
*/
public function render(Smarty_Internal_Template $_template)
{
if ($_template->source->handler->uncompiled) {
if ($_template->smarty->debugging) {
$_template->smarty->_debug->start_render($_template);
}
$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);
}
if ($_template->smarty->debugging) {
$_template->smarty->_debug->end_render($_template);
}
}
}
/** /**
* Get source time stamp * Get source time stamp
* *