summaryrefslogtreecommitdiff
path: root/tests/UnitTests/TemplateSource/X_Scopes/plugins/function.checkvar.php
blob: 71740902975ff37e504049d77b1d3fbc4ab3fbfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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;
}