Files
smarty/tests/UnitTests/__shared/PHPunitplugins/function.checkvar.php
Simon Wisselink 82397ec7f0 Fixed that scoped variables would overwrite parent scope. (#954)
* Fixed that scoped variables would overwrite parent scope.
Fixes #952

* Moved variable stack maintenance to methods and private properties in Data class.
2024-03-15 16:10:27 +01:00

48 lines
1.4 KiB
PHP

<?php
/**
* Smarty plugin for testing scopes
*
*/
use Smarty\Template;
/**
* Smarty {checkvar}
*
* @param array $params parameter array
* @param Template $template template object
*
* @return string
*/
function smarty_function_checkvar($params, \Smarty\Template $template)
{
$output = '';
$types = ['template', 'data', 'global'];
if (isset($params['types'])) {
$types = (array)$params['types'];
}
$var = $params['var'];
$ptr = $template;
while ($ptr) {
if (in_array('template', $types) && $ptr instanceof Template) {
$output .= "#{$ptr->getSource()->name}:\${$var} =";
$output .= $ptr->hasVariable($var) ? preg_replace('/\s/', '', var_export($ptr->getValue($var), true)) : '>unassigned<';
$ptr = $ptr->parent;
} elseif (in_array('data', $types) && !($ptr instanceof Template || $ptr instanceof \Smarty\Smarty)) {
$output .= "#data:\${$var} =";
$output .= $ptr->hasVariable($var) ? preg_replace('/\s/', '', var_export($ptr->getValue($var), true)) : '>unassigned<';
$ptr = $ptr->parent;
} else {
$ptr = null;
}
}
if (in_array('global', $types)) {
$output .= "#global:\${$var} =";
$output .= $template->getSmarty()->hasVariable($var) ?
preg_replace('/\s/', '', var_export($template->getSmarty()->getValue($var), true)) : '>unassigned<';
}
return $output;
}