- optimization of tag attribute compiling

This commit is contained in:
uwetews
2016-02-14 19:58:55 +01:00
parent 7e36f98c1d
commit 30328d318c
14 changed files with 52 additions and 56 deletions

View File

@@ -3,7 +3,8 @@
- new tag {make_nocache} read NEW_FEATURES.txt https://github.com/smarty-php/smarty/issues/110
- optimization of sub-template processing
- bugfix using extendsall as default resource and {include} inside {block} tags could produce unexpected results https://github.com/smarty-php/smarty/issues/183
- optimization of tag attribute compiling
11.02.2016
- improvement added KnockoutJS comments to trimwhitespace outputfilter https://github.com/smarty-php/smarty/issues/82
https://github.com/smarty-php/smarty/pull/181

View File

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

View File

@@ -31,6 +31,7 @@ class Smarty_Internal_Compile_Append extends Smarty_Internal_Compile_Assign
$this->required_attributes = array('var', 'value');
$this->shorttag_order = array('var', 'value');
$this->optional_attributes = array('scope', 'index');
$this->mapCache = array();
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// map to compile assign attributes

View File

@@ -49,6 +49,7 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
$this->required_attributes = array('var', 'value');
$this->shorttag_order = array('var', 'value');
$this->optional_attributes = array('scope');
$this->mapCache = array();
$_nocache = false;
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);

View File

@@ -50,8 +50,6 @@ class Smarty_Internal_Compile_Eval extends Smarty_Internal_CompileBase
*/
public function compile($args, $compiler)
{
$this->required_attributes = array('var');
$this->optional_attributes = array('assign');
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if (isset($_attr[ 'assign' ])) {

View File

@@ -42,6 +42,7 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
$this->required_attributes = array('start', 'ifexp', 'var', 'step');
$this->optional_attributes = array();
}
$this->mapCache = array();
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);

View File

@@ -128,10 +128,6 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
// assume caching is off
$_caching = Smarty::CACHING_OFF;
if ($_attr[ 'nocache' ] === true) {
$compiler->tag_nocache = true;
}
$call_nocache = $compiler->tag_nocache || $compiler->nocache;
// caching was on and {include} is not in nocache mode

View File

@@ -49,9 +49,6 @@ class Smarty_Internal_Compile_Private_Block_Plugin extends Smarty_Internal_Compi
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$this->nesting ++;
if ($_attr[ 'nocache' ] === true) {
$compiler->tag_nocache = true;
}
unset($_attr[ 'nocache' ]);
list($callback, $_paramsArray, $callable) = $this->setup($compiler, $_attr, $tag, $function);
$_params = 'array(' . implode(",", $_paramsArray) . ')';

View File

@@ -50,9 +50,6 @@ class Smarty_Internal_Compile_Private_Function_Plugin extends Smarty_Internal_Co
//Does tag create output
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
if ($_attr[ 'nocache' ] === true) {
$compiler->tag_nocache = true;
}
unset($_attr[ 'nocache' ]);
// convert attributes into parameter array string
$_paramsArray = array();

View File

@@ -42,9 +42,6 @@ class Smarty_Internal_Compile_Private_Object_Function extends Smarty_Internal_Co
//Does tag create output
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
if ($_attr[ 'nocache' ] === true) {
$compiler->tag_nocache = true;
}
unset($_attr[ 'nocache' ]);
$_assign = null;
if (isset($_attr[ 'assign' ])) {

View File

@@ -46,10 +46,6 @@ class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_C
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// nocache option
if ($_attr[ 'nocache' ] === true) {
$compiler->tag_nocache = true;
}
if (isset($_attr[ 'assign' ])) {
// assign output to variable
$output = "<?php \$_smarty_tpl->assign({$_attr['assign']},{$parameter['value']});?>";

View File

@@ -41,9 +41,6 @@ class Smarty_Internal_Compile_Private_Registered_Function extends Smarty_Interna
//Does tag create output
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
if ($_attr[ 'nocache' ]) {
$compiler->tag_nocache = true;
}
unset($_attr[ 'nocache' ]);
if (isset($compiler->smarty->registered_plugins[ Smarty::PLUGIN_FUNCTION ][ $tag ])) {
$tag_info = $compiler->smarty->registered_plugins[ Smarty::PLUGIN_FUNCTION ][ $tag ];

View File

@@ -44,6 +44,10 @@ abstract class Smarty_Internal_CompileBase
*/
public $option_flags = array('nocache');
public $optionMap = array(1 => true, 0 => false, 'true' => true, 'false' => false);
public $mapCache = array();
/**
* This function checks if the attributes passed are valid
* The attributes passed for the tag to compile are checked against the list of required and
@@ -59,12 +63,14 @@ abstract class Smarty_Internal_CompileBase
public function getAttributes($compiler, $attributes)
{
$_indexed_attr = array();
// loop over attributes
if (!isset($this->mapCache[ 'option' ])) {
$this->mapCache[ 'option' ] = array_fill_keys($this->option_flags, true);
}
foreach ($attributes as $key => $mixed) {
// shorthand ?
if (!is_array($mixed)) {
// option flag ?
if (in_array(trim($mixed, '\'"'), $this->option_flags)) {
if (isset($this->mapCache[ 'option' ][ trim($mixed, '\'"') ])) {
$_indexed_attr[ trim($mixed, '\'"') ] = true;
// shorthand attribute ?
} elseif (isset($this->shorttag_order[ $key ])) {
@@ -75,46 +81,44 @@ abstract class Smarty_Internal_CompileBase
}
// named attribute
} else {
$kv = each($mixed);
// option flag?
if (in_array($kv[ 'key' ], $this->option_flags)) {
if (is_bool($kv[ 'value' ])) {
$_indexed_attr[ $kv[ 'key' ] ] = $kv[ 'value' ];
} elseif (is_string($kv[ 'value' ]) &&
in_array(trim($kv[ 'value' ], '\'"'), array('true', 'false'))
) {
if (trim($kv[ 'value' ]) == 'true') {
$_indexed_attr[ $kv[ 'key' ] ] = true;
foreach ($mixed as $k => $v) {
// option flag?
if (isset($this->mapCache[ 'option' ][ $k ])) {
if (is_bool($v)) {
$_indexed_attr[ $k ] = $v;
} else {
$_indexed_attr[ $kv[ 'key' ] ] = false;
}
} elseif (is_numeric($kv[ 'value' ]) && in_array($kv[ 'value' ], array(0, 1))) {
if ($kv[ 'value' ] == 1) {
$_indexed_attr[ $kv[ 'key' ] ] = true;
} else {
$_indexed_attr[ $kv[ 'key' ] ] = false;
if (is_string($v)) {
$v = trim($v, '\'" ');
}
if (isset($this->optionMap[ $v ])) {
$_indexed_attr[ $k ] = $this->optionMap[ $v ];
} else {
$compiler->trigger_template_error("illegal value '" . var_export($v, true) .
"' for option flag '{$k}'", null, true);
}
}
// must be named attribute
} else {
$compiler->trigger_template_error("illegal value of option flag \"{$kv['key']}\"", null, true);
$_indexed_attr[ $k ] = $v;
}
// must be named attribute
} else {
reset($mixed);
$_indexed_attr[ key($mixed) ] = $mixed[ key($mixed) ];
}
}
}
// check if all required attributes present
foreach ($this->required_attributes as $attr) {
if (!array_key_exists($attr, $_indexed_attr)) {
if (!isset($_indexed_attr[ $attr ])) {
$compiler->trigger_template_error("missing \"" . $attr . "\" attribute", null, true);
}
}
// check for not allowed attributes
if ($this->optional_attributes != array('_any')) {
$tmp_array = array_merge($this->required_attributes, $this->optional_attributes, $this->option_flags);
if (!isset($this->mapCache[ 'all' ])) {
$this->mapCache[ 'all' ] =
array_fill_keys(array_merge($this->required_attributes, $this->optional_attributes,
$this->option_flags), true);
}
foreach ($_indexed_attr as $key => $dummy) {
if (!in_array($key, $tmp_array) && $key !== 0) {
if (!isset($this->mapCache[ 'all' ][ $key ]) && $key !== 0) {
$compiler->trigger_template_error("unexpected \"" . $key . "\" attribute", null, true);
}
}
@@ -125,7 +129,9 @@ abstract class Smarty_Internal_CompileBase
$_indexed_attr[ $flag ] = false;
}
}
if (isset($_indexed_attr[ 'nocache' ]) && $_indexed_attr[ 'nocache' ]) {
$compiler->tag_nocache = true;
}
return $_indexed_attr;
}

View File

@@ -507,10 +507,18 @@ abstract class Smarty_Internal_TemplateCompilerBase
$this->template->_cache[ 'used_tags' ][] = array($tag, $args);
}
// check nocache option flag
if (in_array("'nocache'", $args) || in_array(array('nocache' => 'true'), $args) ||
in_array(array('nocache' => '"true"'), $args) || in_array(array('nocache' => "'true'"), $args)
) {
$this->tag_nocache = true;
foreach ($args as $arg) {
if (!is_array($arg)) {
if ($arg == "'nocache'") {
$this->tag_nocache = true;
}
} else {
foreach ($arg as $k => $v) {
if ($k == "'nocache'" && (trim($v, "'\" ") == 'true')) {
$this->tag_nocache = true;
}
}
}
}
// compile the smarty tag (required compile classes to compile the tag are auto loaded)
if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {