From 30328d318c74dc28383fad997876276d4cd923ee Mon Sep 17 00:00:00 2001 From: uwetews Date: Sun, 14 Feb 2016 19:58:55 +0100 Subject: [PATCH] - optimization of tag attribute compiling --- change_log.txt | 3 +- libs/Smarty.class.php | 2 +- .../smarty_internal_compile_append.php | 1 + .../smarty_internal_compile_assign.php | 1 + .../smarty_internal_compile_eval.php | 2 - .../smarty_internal_compile_for.php | 1 + .../smarty_internal_compile_include.php | 4 -- ..._internal_compile_private_block_plugin.php | 3 - ...ternal_compile_private_function_plugin.php | 3 - ...ternal_compile_private_object_function.php | 3 - ...ernal_compile_private_print_expression.php | 4 -- ...al_compile_private_registered_function.php | 3 - .../smarty_internal_compilebase.php | 62 ++++++++++--------- .../smarty_internal_templatecompilerbase.php | 16 +++-- 14 files changed, 52 insertions(+), 56 deletions(-) diff --git a/change_log.txt b/change_log.txt index aa3d17ad..c338830c 100644 --- a/change_log.txt +++ b/change_log.txt @@ -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 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 141437dd..338fbf42 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -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 diff --git a/libs/sysplugins/smarty_internal_compile_append.php b/libs/sysplugins/smarty_internal_compile_append.php index 5ec14bc1..8539d6b2 100644 --- a/libs/sysplugins/smarty_internal_compile_append.php +++ b/libs/sysplugins/smarty_internal_compile_append.php @@ -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 diff --git a/libs/sysplugins/smarty_internal_compile_assign.php b/libs/sysplugins/smarty_internal_compile_assign.php index 73e60e5b..b7d4d5ba 100644 --- a/libs/sysplugins/smarty_internal_compile_assign.php +++ b/libs/sysplugins/smarty_internal_compile_assign.php @@ -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); diff --git a/libs/sysplugins/smarty_internal_compile_eval.php b/libs/sysplugins/smarty_internal_compile_eval.php index abdc0f3b..97a3c29c 100644 --- a/libs/sysplugins/smarty_internal_compile_eval.php +++ b/libs/sysplugins/smarty_internal_compile_eval.php @@ -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' ])) { diff --git a/libs/sysplugins/smarty_internal_compile_for.php b/libs/sysplugins/smarty_internal_compile_for.php index 5307e92d..e5e7c61c 100644 --- a/libs/sysplugins/smarty_internal_compile_for.php +++ b/libs/sysplugins/smarty_internal_compile_for.php @@ -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); diff --git a/libs/sysplugins/smarty_internal_compile_include.php b/libs/sysplugins/smarty_internal_compile_include.php index 1759e4b3..c9f608a7 100644 --- a/libs/sysplugins/smarty_internal_compile_include.php +++ b/libs/sysplugins/smarty_internal_compile_include.php @@ -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 diff --git a/libs/sysplugins/smarty_internal_compile_private_block_plugin.php b/libs/sysplugins/smarty_internal_compile_private_block_plugin.php index ef5682a5..165d2118 100644 --- a/libs/sysplugins/smarty_internal_compile_private_block_plugin.php +++ b/libs/sysplugins/smarty_internal_compile_private_block_plugin.php @@ -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) . ')'; diff --git a/libs/sysplugins/smarty_internal_compile_private_function_plugin.php b/libs/sysplugins/smarty_internal_compile_private_function_plugin.php index c0a9b28e..708c2dcf 100644 --- a/libs/sysplugins/smarty_internal_compile_private_function_plugin.php +++ b/libs/sysplugins/smarty_internal_compile_private_function_plugin.php @@ -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(); diff --git a/libs/sysplugins/smarty_internal_compile_private_object_function.php b/libs/sysplugins/smarty_internal_compile_private_object_function.php index 422a6597..a187ca23 100644 --- a/libs/sysplugins/smarty_internal_compile_private_object_function.php +++ b/libs/sysplugins/smarty_internal_compile_private_object_function.php @@ -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' ])) { diff --git a/libs/sysplugins/smarty_internal_compile_private_print_expression.php b/libs/sysplugins/smarty_internal_compile_private_print_expression.php index 13b1e855..e4508cd8 100644 --- a/libs/sysplugins/smarty_internal_compile_private_print_expression.php +++ b/libs/sysplugins/smarty_internal_compile_private_print_expression.php @@ -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 = "assign({$_attr['assign']},{$parameter['value']});?>"; diff --git a/libs/sysplugins/smarty_internal_compile_private_registered_function.php b/libs/sysplugins/smarty_internal_compile_private_registered_function.php index c1cf7086..706dbdf1 100644 --- a/libs/sysplugins/smarty_internal_compile_private_registered_function.php +++ b/libs/sysplugins/smarty_internal_compile_private_registered_function.php @@ -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 ]; diff --git a/libs/sysplugins/smarty_internal_compilebase.php b/libs/sysplugins/smarty_internal_compilebase.php index 95cdd678..390e669e 100644 --- a/libs/sysplugins/smarty_internal_compilebase.php +++ b/libs/sysplugins/smarty_internal_compilebase.php @@ -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; } diff --git a/libs/sysplugins/smarty_internal_templatecompilerbase.php b/libs/sysplugins/smarty_internal_templatecompilerbase.php index 2009d937..778e2be9 100644 --- a/libs/sysplugins/smarty_internal_templatecompilerbase.php +++ b/libs/sysplugins/smarty_internal_templatecompilerbase.php @@ -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) {