]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_compile_assign.php
Merge pull request #4233 from MrPetovan/task/4116-move-smarty-to-composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_compile_assign.php
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  */
17 class 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('local' => Smarty::SCOPE_LOCAL, 'parent' => Smarty::SCOPE_PARENT,
33                                  'root' => Smarty::SCOPE_ROOT, 'global' => Smarty::SCOPE_GLOBAL,
34                                  'tpl_root' => Smarty::SCOPE_TPL_ROOT, 'smarty' => Smarty::SCOPE_SMARTY);
35
36     /**
37      * Compiles code for the {assign} tag
38      *
39      * @param  array                                $args      array with attributes from parser
40      * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
41      * @param  array                                $parameter array with compilation parameter
42      *
43      * @return string compiled code
44      * @throws \SmartyCompilerException
45      */
46     public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
47     {
48         // the following must be assigned at runtime because it will be overwritten in Smarty_Internal_Compile_Append
49         $this->required_attributes = array('var', 'value');
50         $this->shorttag_order = array('var', 'value');
51         $this->optional_attributes = array('scope');
52         $this->mapCache = array();
53         $_nocache = false;
54         // check and get attributes
55         $_attr = $this->getAttributes($compiler, $args);
56         // nocache ?
57         if ($_var = $compiler->getId($_attr[ 'var' ])) {
58             $_var = "'{$_var}'";
59         } else {
60             $_var = $_attr[ 'var' ];
61         }
62         if ($compiler->tag_nocache || $compiler->nocache) {
63             $_nocache = true;
64             // create nocache var to make it know for further compiling
65             $compiler->setNocacheInVariable($_attr[ 'var' ]);
66         }
67         // scope setup
68         if ($_attr[ 'noscope' ]) {
69             $_scope = - 1;
70         } else {
71             $_scope = $compiler->convertScope($_attr, $this->valid_scopes);
72         }
73         // optional parameter
74         $_params = "";
75         if ($_nocache || $_scope) {
76             $_params .= ' ,' . var_export($_nocache, true);
77         }
78         if ($_scope) {
79             $_params .= ' ,' . $_scope;
80         }
81         if (isset($parameter[ 'smarty_internal_index' ])) {
82             $output =
83                 "<?php \$_tmp_array = isset(\$_smarty_tpl->tpl_vars[{$_var}]) ? \$_smarty_tpl->tpl_vars[{$_var}]->value : array();\n";
84             $output .= "if (!is_array(\$_tmp_array) || \$_tmp_array instanceof ArrayAccess) {\n";
85             $output .= "settype(\$_tmp_array, 'array');\n";
86             $output .= "}\n";
87             $output .= "\$_tmp_array{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
88             $output .= "\$_smarty_tpl->_assignInScope({$_var}, \$_tmp_array{$_params});\n?>";
89         } else {
90             $output = "<?php \$_smarty_tpl->_assignInScope({$_var}, {$_attr['value']}{$_params});\n?>";
91         }
92         return $output;
93     }
94 }