diff --git a/src/Compile/Base.php b/src/Compile/Base.php index 5a2a58d1..12f3c3a5 100644 --- a/src/Compile/Base.php +++ b/src/Compile/Base.php @@ -64,6 +64,27 @@ abstract class Base implements CompilerInterface { return $this->cacheable; } + /** + * Converts attributes into parameter array string + * @param array $_attr + * + * @return array + */ + protected function formatParamsArray($_attr, array $cacheAttributes = []) { + $_paramsArray = []; + foreach ($_attr as $_key => $_value) { + if (is_int($_key)) { + $_paramsArray[] = "$_key=>$_value"; + } elseif (in_array($_key, $cacheAttributes)) { + $_value = str_replace('\'', "^#^", $_value); + $_paramsArray[] = "'$_key'=>^#^.var_export($_value,true).^#^"; + } else { + $_paramsArray[] = "'$_key'=>$_value"; + } + } + return $_paramsArray; + } + /** * 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 diff --git a/src/Compile/BlockCompiler.php b/src/Compile/BlockCompiler.php index 3855d52b..0e66dfac 100644 --- a/src/Compile/BlockCompiler.php +++ b/src/Compile/BlockCompiler.php @@ -10,8 +10,6 @@ namespace Smarty\Compile; -use Smarty\Compile\Base; - /** * Smarty Internal Plugin Compile Block Plugin Class * @@ -106,14 +104,7 @@ class BlockCompiler extends Base { * @return array */ protected function setup(\Smarty\Compiler\Template $compiler, $_attr, $tag, $function) { - $_paramsArray = []; - foreach ($_attr as $_key => $_value) { - if (is_int($_key)) { - $_paramsArray[] = "$_key=>$_value"; - } else { - $_paramsArray[] = "'$_key'=>$_value"; - } - } + $_paramsArray = $this->formatParamsArray($_attr); return [$function, $_paramsArray, null]; } } diff --git a/src/Compile/DefaultHandlerFunctionCallCompiler.php b/src/Compile/DefaultHandlerFunctionCallCompiler.php index bc88648d..54344541 100644 --- a/src/Compile/DefaultHandlerFunctionCallCompiler.php +++ b/src/Compile/DefaultHandlerFunctionCallCompiler.php @@ -32,16 +32,7 @@ class DefaultHandlerFunctionCallCompiler extends Base { $_attr = $this->getAttributes($compiler, $args); unset($_attr['nocache']); - // convert attributes into parameter array string - - $_paramsArray = []; - foreach ($_attr as $_key => $_value) { - if (is_int($_key)) { - $_paramsArray[] = "$_key=>$_value"; - } else { - $_paramsArray[] = "'$_key'=>$_value"; - } - } + $_paramsArray = $this->formatParamsArray($_attr); $_params = 'array(' . implode(',', $_paramsArray) . ')'; $output = "\$_smarty_tpl->smarty->getRuntime('DefaultPluginHandler')->runPlugin(" . var_export($function, true) . diff --git a/src/Compile/FunctionCallCompiler.php b/src/Compile/FunctionCallCompiler.php index 65906535..89340c7d 100644 --- a/src/Compile/FunctionCallCompiler.php +++ b/src/Compile/FunctionCallCompiler.php @@ -51,18 +51,12 @@ class FunctionCallCompiler extends Base { // not cacheable? $compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable(); - // convert attributes into parameter array string - $_paramsArray = []; - foreach ($_attr as $_key => $_value) { - if (is_int($_key)) { - $_paramsArray[] = "$_key=>$_value"; - } elseif ($compiler->template->caching && in_array($_key, $functionHandler->getCacheAttributes())) { - $_value = str_replace('\'', "^#^", $_value); - $_paramsArray[] = "'$_key'=>^#^.var_export($_value,true).^#^"; - } else { - $_paramsArray[] = "'$_key'=>$_value"; - } - } + + $_paramsArray = $this->formatParamsArray( + $_attr, + $compiler->template->caching ? $functionHandler->getCacheAttributes() : [] + ); + $_params = 'array(' . implode(',', $_paramsArray) . ')'; $output = "\$_smarty_tpl->smarty->getFunctionHandler(" . var_export($function, true) . ")"; diff --git a/src/Compile/ObjectMethodBlockCompiler.php b/src/Compile/ObjectMethodBlockCompiler.php index 3334deae..71853639 100644 --- a/src/Compile/ObjectMethodBlockCompiler.php +++ b/src/Compile/ObjectMethodBlockCompiler.php @@ -31,14 +31,7 @@ class ObjectMethodBlockCompiler extends BlockCompiler { * @return array */ protected function setup(Template $compiler, $_attr, $tag, $function) { - $_paramsArray = []; - foreach ($_attr as $_key => $_value) { - if (is_int($_key)) { - $_paramsArray[] = "$_key=>$_value"; - } else { - $_paramsArray[] = "'$_key'=>$_value"; - } - } + $_paramsArray = $this->formatParamsArray($_attr); $callback = ["\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]", "->{$function}"]; return [$callback, $_paramsArray, "array(\$_block_plugin{$this->nesting}, '{$function}')"]; } diff --git a/src/Compile/ObjectMethodCallCompiler.php b/src/Compile/ObjectMethodCallCompiler.php index 76ada5c3..ce1c53c2 100644 --- a/src/Compile/ObjectMethodCallCompiler.php +++ b/src/Compile/ObjectMethodCallCompiler.php @@ -54,14 +54,7 @@ class ObjectMethodCallCompiler extends Base { if (is_callable([$compiler->smarty->registered_objects[$tag][0], $function])) { // convert attributes into parameter array string if ($compiler->smarty->registered_objects[$tag][2]) { - $_paramsArray = []; - foreach ($_attr as $_key => $_value) { - if (is_int($_key)) { - $_paramsArray[] = "$_key=>$_value"; - } else { - $_paramsArray[] = "'$_key'=>$_value"; - } - } + $_paramsArray = $this->formatParamsArray($_attr); $_params = 'array(' . implode(',', $_paramsArray) . ')'; $output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}({$_params},\$_smarty_tpl)"; } else { diff --git a/src/Compile/Tag/Call.php b/src/Compile/Tag/Call.php index af0ff75f..3a1112d7 100644 --- a/src/Compile/Tag/Call.php +++ b/src/Compile/Tag/Call.php @@ -69,14 +69,7 @@ class Call extends Base { } else { $_nocache = 'false'; } - $_paramsArray = []; - foreach ($_attr as $_key => $_value) { - if (is_int($_key)) { - $_paramsArray[] = "$_key=>$_value"; - } else { - $_paramsArray[] = "'$_key'=>$_value"; - } - } + $_paramsArray = $this->formatParamsArray($_attr); $_params = 'array(' . implode(',', $_paramsArray) . ')'; //$compiler->suppressNocacheProcessing = true; // was there an assign attribute diff --git a/src/Compile/Tag/FunctionClose.php b/src/Compile/Tag/FunctionClose.php index 9cee992f..e55526f1 100644 --- a/src/Compile/Tag/FunctionClose.php +++ b/src/Compile/Tag/FunctionClose.php @@ -46,14 +46,7 @@ class FunctionClose extends Base { $_parameter = $_attr; unset($_parameter['name']); // default parameter - $_paramsArray = []; - foreach ($_parameter as $_key => $_value) { - if (is_int($_key)) { - $_paramsArray[] = "$_key=>$_value"; - } else { - $_paramsArray[] = "'$_key'=>$_value"; - } - } + $_paramsArray = $this->formatParamsArray($_attr); if (!empty($_paramsArray)) { $_params = 'array(' . implode(',', $_paramsArray) . ')'; $_paramsCode = "\$params = array_merge($_params, \$params);\n";