1<?php
2/**
3 * Smarty Internal Plugin Compile Assign
4 * Compiles the {assign} tag
5 *
6 * @package    Smarty
7 * @subpackage Compiler
8 * @author     Uwe Tews
9 */
10
11/**
12 * Smarty Internal Plugin Compile Assign Class
13 *
14 * @package    Smarty
15 * @subpackage Compiler
16 */
17class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
18{
19    /**
20     * Attribute definition: Overwrites base class.
21     *
22     * @var array
23     * @see Smarty_Internal_CompileBase
24     */
25    public $option_flags = array('nocache', 'noscope');
26
27    /**
28     * Valid scope names
29     *
30     * @var array
31     */
32    public $valid_scopes = array(
33        'local'    => Smarty::SCOPE_LOCAL, 'parent' => Smarty::SCOPE_PARENT,
34        'root'     => Smarty::SCOPE_ROOT, 'global' => Smarty::SCOPE_GLOBAL,
35        'tpl_root' => Smarty::SCOPE_TPL_ROOT, 'smarty' => Smarty::SCOPE_SMARTY
36    );
37
38    /**
39     * Compiles code for the {assign} tag
40     *
41     * @param array                                 $args      array with attributes from parser
42     * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
43     * @param array                                 $parameter array with compilation parameter
44     *
45     * @return string compiled code
46     * @throws \SmartyCompilerException
47     */
48    public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
49    {
50        // the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append
51        $this->required_attributes = array('var', 'value');
52        $this->shorttag_order = array('var', 'value');
53        $this->optional_attributes = array('scope');
54        $this->mapCache = array();
55        $_nocache = false;
56        // check and get attributes
57        $_attr = $this->getAttributes($compiler, $args);
58        // nocache ?
59        if ($_var = $compiler->getId($_attr[ 'var' ])) {
60            $_var = "'{$_var}'";
61        } else {
62            $_var = $_attr[ 'var' ];
63        }
64        if ($compiler->tag_nocache || $compiler->nocache) {
65            $_nocache = true;
66            // create nocache var to make it know for further compiling
67            $compiler->setNocacheInVariable($_attr[ 'var' ]);
68        }
69        // scope setup
70        if ($_attr[ 'noscope' ]) {
71            $_scope = -1;
72        } else {
73            $_scope = $compiler->convertScope($_attr, $this->valid_scopes);
74        }
75        // optional parameter
76        $_params = '';
77        if ($_nocache || $_scope) {
78            $_params .= ' ,' . var_export($_nocache, true);
79        }
80        if ($_scope) {
81            $_params .= ' ,' . $_scope;
82        }
83        if (isset($parameter[ 'smarty_internal_index' ])) {
84            $output =
85                "<?php \$_tmp_array = isset(\$_smarty_tpl->tpl_vars[{$_var}]) ? \$_smarty_tpl->tpl_vars[{$_var}]->value : array();\n";
86            $output .= "if (!is_array(\$_tmp_array) || \$_tmp_array instanceof ArrayAccess) {\n";
87            $output .= "settype(\$_tmp_array, 'array');\n";
88            $output .= "}\n";
89            $output .= "\$_tmp_array{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
90            $output .= "\$_smarty_tpl->_assignInScope({$_var}, \$_tmp_array{$_params});?>";
91        } else {
92            $output = "<?php \$_smarty_tpl->_assignInScope({$_var}, {$_attr['value']}{$_params});?>";
93        }
94        return $output;
95    }
96}
97