Introduce formatParamsArray method for recurring code fragment

This commit is contained in:
Simon Wisselink
2023-01-04 14:42:30 +01:00
parent 14bbffd584
commit 216347b4ff
8 changed files with 33 additions and 64 deletions

View File

@@ -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

View File

@@ -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];
}
}

View File

@@ -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) .

View File

@@ -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) . ")";

View File

@@ -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}')"];
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -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";