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; 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 * 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 * 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; namespace Smarty\Compile;
use Smarty\Compile\Base;
/** /**
* Smarty Internal Plugin Compile Block Plugin Class * Smarty Internal Plugin Compile Block Plugin Class
* *
@@ -106,14 +104,7 @@ class BlockCompiler extends Base {
* @return array * @return array
*/ */
protected function setup(\Smarty\Compiler\Template $compiler, $_attr, $tag, $function) { protected function setup(\Smarty\Compiler\Template $compiler, $_attr, $tag, $function) {
$_paramsArray = []; $_paramsArray = $this->formatParamsArray($_attr);
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
return [$function, $_paramsArray, null]; return [$function, $_paramsArray, null];
} }
} }

View File

@@ -32,16 +32,7 @@ class DefaultHandlerFunctionCallCompiler extends Base {
$_attr = $this->getAttributes($compiler, $args); $_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']); unset($_attr['nocache']);
// convert attributes into parameter array string $_paramsArray = $this->formatParamsArray($_attr);
$_paramsArray = [];
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')'; $_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->smarty->getRuntime('DefaultPluginHandler')->runPlugin(" . var_export($function, true) . $output = "\$_smarty_tpl->smarty->getRuntime('DefaultPluginHandler')->runPlugin(" . var_export($function, true) .

View File

@@ -51,18 +51,12 @@ class FunctionCallCompiler extends Base {
// not cacheable? // not cacheable?
$compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable(); $compiler->tag_nocache = $compiler->tag_nocache || !$functionHandler->isCacheable();
// convert attributes into parameter array string
$_paramsArray = []; $_paramsArray = $this->formatParamsArray(
foreach ($_attr as $_key => $_value) { $_attr,
if (is_int($_key)) { $compiler->template->caching ? $functionHandler->getCacheAttributes() : []
$_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";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')'; $_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->smarty->getFunctionHandler(" . var_export($function, true) . ")"; $output = "\$_smarty_tpl->smarty->getFunctionHandler(" . var_export($function, true) . ")";

View File

@@ -31,14 +31,7 @@ class ObjectMethodBlockCompiler extends BlockCompiler {
* @return array * @return array
*/ */
protected function setup(Template $compiler, $_attr, $tag, $function) { protected function setup(Template $compiler, $_attr, $tag, $function) {
$_paramsArray = []; $_paramsArray = $this->formatParamsArray($_attr);
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$callback = ["\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]", "->{$function}"]; $callback = ["\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]", "->{$function}"];
return [$callback, $_paramsArray, "array(\$_block_plugin{$this->nesting}, '{$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])) { if (is_callable([$compiler->smarty->registered_objects[$tag][0], $function])) {
// convert attributes into parameter array string // convert attributes into parameter array string
if ($compiler->smarty->registered_objects[$tag][2]) { if ($compiler->smarty->registered_objects[$tag][2]) {
$_paramsArray = []; $_paramsArray = $this->formatParamsArray($_attr);
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')'; $_params = 'array(' . implode(',', $_paramsArray) . ')';
$output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}({$_params},\$_smarty_tpl)"; $output = "\$_smarty_tpl->smarty->registered_objects['{$tag}'][0]->{$function}({$_params},\$_smarty_tpl)";
} else { } else {

View File

@@ -69,14 +69,7 @@ class Call extends Base {
} else { } else {
$_nocache = 'false'; $_nocache = 'false';
} }
$_paramsArray = []; $_paramsArray = $this->formatParamsArray($_attr);
foreach ($_attr as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
$_params = 'array(' . implode(',', $_paramsArray) . ')'; $_params = 'array(' . implode(',', $_paramsArray) . ')';
//$compiler->suppressNocacheProcessing = true; //$compiler->suppressNocacheProcessing = true;
// was there an assign attribute // was there an assign attribute

View File

@@ -46,14 +46,7 @@ class FunctionClose extends Base {
$_parameter = $_attr; $_parameter = $_attr;
unset($_parameter['name']); unset($_parameter['name']);
// default parameter // default parameter
$_paramsArray = []; $_paramsArray = $this->formatParamsArray($_attr);
foreach ($_parameter as $_key => $_value) {
if (is_int($_key)) {
$_paramsArray[] = "$_key=>$_value";
} else {
$_paramsArray[] = "'$_key'=>$_value";
}
}
if (!empty($_paramsArray)) { if (!empty($_paramsArray)) {
$_params = 'array(' . implode(',', $_paramsArray) . ')'; $_params = 'array(' . implode(',', $_paramsArray) . ')';
$_paramsCode = "\$params = array_merge($_params, \$params);\n"; $_paramsCode = "\$params = array_merge($_params, \$params);\n";