mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-03 18:04:26 +02:00
Introduce formatParamsArray method for recurring code fragment
This commit is contained in:
@@ -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
|
||||
|
@@ -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];
|
||||
}
|
||||
}
|
||||
|
@@ -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) .
|
||||
|
@@ -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) . ")";
|
||||
|
@@ -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}')"];
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
@@ -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
|
||||
|
@@ -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";
|
||||
|
Reference in New Issue
Block a user