introduce Smarty::$resource_cache_mode and cache template object of {include} inside loop

This commit is contained in:
uwetews
2015-08-10 21:57:06 +02:00
parent 5c9c57df7d
commit 0260b525b8
10 changed files with 107 additions and 60 deletions

View File

@@ -112,7 +112,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.28-dev/44';
const SMARTY_VERSION = '3.1.28-dev/45';
/**
* define variable scopes
@@ -192,6 +192,17 @@ class Smarty extends Smarty_Internal_TemplateBase
const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler';
/**
* Resource caching modes
*/
const RESOURCE_CACHE_OFF = 0;
const RESOURCE_CACHE_AUTOMATIC = 1; // cache template objects by rules
const RESOURCE_CACHE_TEMPLATE = 2; // cache all template objects
const RESOURCE_CACHE_ON = 4; // cache source nad compiled resources
/**#@-*/
/**
@@ -582,14 +593,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*
* @var bool
*/
public $resource_caching = false;
/**
* enable template resource caching
*
* @var bool
*/
public $template_resource_caching = true;
public $resource_cache_mode = 1;
/**
* check If-Modified-Since headers
@@ -724,6 +728,13 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
public $_parserdebug = false;
/**
* removed properties
*
* @var array
*/
public $obsoleteProperties = array('resource_caching', 'template_resource_caching');
/**#@-*/
/**
@@ -1610,6 +1621,8 @@ class Smarty extends Smarty_Internal_TemplateBase
if (isset($allowed[$name])) {
return $this->{$allowed[$name]}();
} elseif (in_array($name, $this->obsoleteProperties)) {
return null;
} else {
trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
}
@@ -1631,6 +1644,8 @@ class Smarty extends Smarty_Internal_TemplateBase
if (isset($allowed[$name])) {
$this->{$allowed[$name]}($value);
} elseif (in_array($name, $this->obsoleteProperties)) {
return;
} else {
trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE);
}

View File

@@ -34,6 +34,7 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
*/
public function compile($args, $compiler, $parameter)
{
$compiler->loopNesting++;
if ($parameter == 0) {
$this->required_attributes = array('start', 'to');
$this->optional_attributes = array('max', 'step');
@@ -44,7 +45,7 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$output = "<?php ";
$output = "<?php\n";
if ($parameter == 1) {
foreach ($_attr['start'] as $_statement) {
if (is_array($_statement['var'])) {
@@ -54,8 +55,8 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
$var = $_statement['var'];
$index = '';
}
$output .= " \$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable;";
$output .= " \$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value];\n";
$output .= "\$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable;\n";
$output .= "\$_smarty_tpl->tpl_vars[$var]->value{$index} = {$_statement['value']};\n";
}
if (is_array($_attr['var'])) {
$var = $_attr['var']['var'];
@@ -64,7 +65,7 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
$var = $_attr['var'];
$index = '';
}
$output .= " if ($_attr[ifexp]) { for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n";
$output .= "if ($_attr[ifexp]) {\nfor (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n";
} else {
$_statement = $_attr['start'];
if (is_array($_statement['var'])) {
@@ -148,6 +149,7 @@ class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase
*/
public function compile($args, $compiler, $parameter)
{
$compiler->loopNesting--;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// must endblock be nocache?
@@ -157,10 +159,11 @@ class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase
list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('for', 'forelse'));
if ($openTag == 'forelse') {
return "<?php } ?>";
} else {
return "<?php }} ?>";
$output = "<?php }\n";
if ($openTag != 'forelse') {
$output .= "}\n";
}
$output .= "?>\n";
return $output;
}
}

View File

@@ -87,6 +87,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
$compiler->loopNesting++;
// init
$this->isNamed = false;
// check and get attributes
@@ -317,8 +318,7 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$compiler->loopNesting--;
// must endblock be nocache?
if ($compiler->nocache) {
$compiler->tag_nocache = true;

View File

@@ -53,6 +53,7 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
$compiler->loopNesting++;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
@@ -101,6 +102,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
$compiler->loopNesting--;
$this->compiler = $compiler;
$saved_data = $this->closeTag($compiler, array('function'));
$_attr = $saved_data[0];

View File

@@ -88,6 +88,12 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
}
}
//
if ($compiler->loopNesting > 0) {
$_cache_tpl = 'true';
} else {
$_cache_tpl = 'false';
}
// assume caching is off
$_caching = Smarty::CACHING_OFF;
@@ -271,9 +277,9 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
//$compiler->suppressNocacheProcessing = true;
}
if (isset($_assign)) {
$_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(\$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, '{$_hash}', '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func']}'));\n";
$_output .= " \$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(\$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, '{$_hash}', '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func']}'));\n";
} else {
$_output .= "echo \$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, '{$_hash}', '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func']}');\n";
$_output .= "echo \$_smarty_tpl->getInlineSubTemplate({$include_file}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, '{$_hash}', '{$compiler->parent_compiler->mergedSubTemplatesData[$tpl_name][$uid]['func']}');\n";
}
if ($update_compile_id) {
$_output .= $compiler->makeNocacheCode("\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n");
@@ -292,9 +298,9 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
}
// was there an assign attribute
if (isset($_assign)) {
$_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(\$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope));\n";
$_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(\$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl}));\n";
} else {
$_output .= "echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope);\n";
$_output .= "echo \$_smarty_tpl->getSubTemplate ($include_file, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl});\n";
}
if ($update_compile_id) {
$_output .= "\$_smarty_tpl->compile_id = array_pop(\$_compile_id_save);\n";

View File

@@ -87,6 +87,7 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
$compiler->loopNesting++;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$attributes = array('name' => $compiler->getId($_attr['name']));
@@ -118,7 +119,7 @@ class Smarty_Internal_Compile_Section extends Smarty_Internal_Compile_Private_Fo
$namedAttr = $this->matchResults['named'];
}
$namedAttr['index'] = true;
$output = "<?php ";
$output = "<?php\n";
foreach ($_attr as $attr_name => $attr_value) {
switch ($attr_name) {
case 'loop':
@@ -428,9 +429,7 @@ class Smarty_Internal_Compile_Sectionclose extends Smarty_Internal_CompileBase
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$compiler->loopNesting--;
// must endblock be nocache?
if ($compiler->nocache) {
$compiler->tag_nocache = true;

View File

@@ -28,6 +28,7 @@ class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
{
$compiler->loopNesting++;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$this->openTag($compiler, 'while', $compiler->nocache);
@@ -38,6 +39,7 @@ class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
// maybe nocache because of nocache variables
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
$_output = "<?php\n";
if (is_array($parameter['if condition'])) {
if ($compiler->nocache) {
$_nocache = ',true';
@@ -56,7 +58,7 @@ class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
$_nocache = '';
}
if (is_array($parameter['if condition']['var'])) {
$_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['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'] .
"$_nocache);\n";
@@ -64,17 +66,16 @@ class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
$parameter['if condition']['var']['smarty_internal_index'] . " = " .
$parameter['if condition']['value'] . ") {?>";
} else {
$_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] .
$_output .= "if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] .
"])) \$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] .
"] = new Smarty_Variable(null{$_nocache});";
$_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var'] . "]->value = " .
$parameter['if condition']['value'] . ") {?>";
}
return $_output;
} else {
return "<?php while ({$parameter['if condition']}) {?>";
$_output .= "while ({$parameter['if condition']}) {?>";
}
return $_output;
}
}
@@ -96,12 +97,12 @@ class Smarty_Internal_Compile_Whileclose extends Smarty_Internal_CompileBase
*/
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
{
$compiler->loopNesting--;
// must endblock be nocache?
if ($compiler->nocache) {
$compiler->tag_nocache = true;
}
$compiler->nocache = $this->closeTag($compiler, array('while'));
return "<?php }?>";
return "<?php }?>\n";
}
}

View File

@@ -83,6 +83,13 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/
public $_capture_stack = array(0 => array());
/**
* Template Id
*
* @var string
*/
public $templateId = '';
/**
* Known template functions
*
@@ -118,6 +125,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
// Template resource
$this->template_resource = $template_resource;
$this->source = Smarty_Template_Source::load($this);
$this->templateId = $this->getTemplateId($template_resource, $this->cache_id, $this->compile_id);
// copy block data of template inheritance
if ($this->parent instanceof Smarty_Internal_Template) {
$this->block_data = $this->parent->block_data;
@@ -448,9 +456,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*
* @returns string template content
*/
public function getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope)
public function getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl)
{
$tpl = $this->setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope);
$tpl = $this->setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl);
return $tpl->render();
}
@@ -467,10 +475,8 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*
* @returns \Smarty_Internal_Template template object
*/
public function setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope)
public function setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl)
{
$tpl = $_templateId = null;
if ($this->smarty->template_resource_caching) {
$_templateId = $this->getTemplateId($template, $cache_id, $compile_id);
// already in template cache?
if (isset($this->smarty->template_objects[$_templateId])) {
@@ -482,13 +488,15 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
}
$tpl->caching = $caching;
$tpl->cache_lifetime = $cache_lifetime;
}
}
if (!isset($tpl)) {
} else {
$tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
if ($this->smarty->template_resource_caching && !$tpl->source->recompiled) {
$this->smarty->template_objects[$_templateId] = $tpl;
}
if (!$tpl->source->recompiled && !isset($this->smarty->template_objects[$_templateId]) &&
(isset($this->smarty->template_objects[$this->templateId]) ||
($cache_tpl && $this->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) ||
$this->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)
) {
$this->smarty->template_objects[$_templateId] = $tpl;
}
if ($caching == 9999) {
$tpl->cached = $this->cached;
@@ -531,9 +539,12 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*
* @returns object template content
*/
public function getInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $hash, $content_func)
public function getInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl, $hash, $content_func)
{
$tpl = $this->setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope);
$tpl = $this->setupSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $cache_tpl);
if (!isset($tpl->compiled)) {
$tpl->compiled = $this->compiled;
}
$tpl->compiled->nocache_hash = $hash;
if ($this->smarty->debugging) {
Smarty_Internal_Debug::start_template($tpl);

View File

@@ -457,6 +457,9 @@ abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
$status = true;
} elseif (property_exists($this->smarty, $property_name)) {
$status = false;
} elseif (in_array($name, $this->smarty->obsoleteProperties)) {
return null;
} else {
}
$_resolved_property_source[$property_name] = $status;
}

View File

@@ -319,6 +319,13 @@ abstract class Smarty_Internal_TemplateCompilerBase
*/
public $variable_filter_stack = array();
/**
* Loop nesting count
*
* @var int
*/
public $loopNesting = 0;
/**
* Strip preg pattern
*