]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_compile_function.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_compile_function.php
1 <?php
2 /**
3  * Smarty Internal Plugin Compile Function
4  * Compiles the {function} {/function} tags
5  *
6  * @package    Smarty
7  * @subpackage Compiler
8  * @author     Uwe Tews
9  */
10
11 /**
12  * Smarty Internal Plugin Compile Function Class
13  *
14  * @package    Smarty
15  * @subpackage Compiler
16  */
17 class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
18 {
19
20     /**
21      * Attribute definition: Overwrites base class.
22      *
23      * @var array
24      * @see Smarty_Internal_CompileBase
25      */
26     public $required_attributes = array('name');
27
28     /**
29      * Attribute definition: Overwrites base class.
30      *
31      * @var array
32      * @see Smarty_Internal_CompileBase
33      */
34     public $shorttag_order = array('name');
35
36     /**
37      * Attribute definition: Overwrites base class.
38      *
39      * @var array
40      * @see Smarty_Internal_CompileBase
41      */
42     public $optional_attributes = array('_any');
43
44     /**
45      * Compiles code for the {function} tag
46      *
47      * @param  array                                $args      array with attributes from parser
48      * @param \Smarty_Internal_TemplateCompilerBase $compiler  compiler object
49      * @param  array                                $parameter array with compilation parameter
50      *
51      * @return bool true
52      * @throws \SmartyCompilerException
53      */
54     public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
55     {
56         // check and get attributes
57         $_attr = $this->getAttributes($compiler, $args);
58
59         if ($_attr[ 'nocache' ] === true) {
60             $compiler->trigger_template_error('nocache option not allowed', null, true);
61         }
62         unset($_attr[ 'nocache' ]);
63         $_name = trim($_attr[ 'name' ], "'\"");
64         $compiler->parent_compiler->tpl_function[ $_name ] = array();
65         $save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
66                       $compiler->template->caching);
67         $this->openTag($compiler, 'function', $save);
68         // Init temporary context
69         $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
70         $compiler->template->compiled->has_nocache_code = false;
71         return true;
72     }
73 }
74
75 /**
76  * Smarty Internal Plugin Compile Functionclose Class
77  *
78  * @package    Smarty
79  * @subpackage Compiler
80  */
81 class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
82 {
83
84     /**
85      * Compiler object
86      *
87      * @var object
88      */
89     private $compiler = null;
90
91     /**
92      * Compiles code for the {/function} tag
93      *
94      * @param  array                                       $args      array with attributes from parser
95      * @param object|\Smarty_Internal_TemplateCompilerBase $compiler  compiler object
96      * @param  array                                       $parameter array with compilation parameter
97      *
98      * @return bool true
99      */
100     public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
101     {
102         $this->compiler = $compiler;
103         $saved_data = $this->closeTag($compiler, array('function'));
104         $_attr = $saved_data[ 0 ];
105         $_name = trim($_attr[ 'name' ], "'\"");
106         $compiler->parent_compiler->tpl_function[ $_name ][ 'compiled_filepath' ] =
107             $compiler->parent_compiler->template->compiled->filepath;
108         $compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
109         $_parameter = $_attr;
110         unset($_parameter[ 'name' ]);
111         // default parameter
112         $_paramsArray = array();
113         foreach ($_parameter as $_key => $_value) {
114             if (is_int($_key)) {
115                 $_paramsArray[] = "$_key=>$_value";
116             } else {
117                 $_paramsArray[] = "'$_key'=>$_value";
118             }
119         }
120         if (!empty($_paramsArray)) {
121             $_params = 'array(' . implode(",", $_paramsArray) . ')';
122             $_paramsCode = "\$params = array_merge($_params, \$params);\n";
123         } else {
124             $_paramsCode = '';
125         }
126         $_functionCode = $compiler->parser->current_buffer;
127         // setup buffer for template function code
128         $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template();
129
130         $_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
131         $_funcNameCaching = $_funcName . '_nocache';
132         if ($compiler->template->compiled->has_nocache_code) {
133             $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
134             $output = "<?php\n";
135             $output .= "/* {$_funcNameCaching} */\n";
136             $output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
137             $output .= "function {$_funcNameCaching} (\$_smarty_tpl,\$params) {\n";
138             $output .= "ob_start();\n";
139             $output .= "\$_smarty_tpl->compiled->has_nocache_code = true;\n";
140             $output .= $_paramsCode;
141             $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}";
142             $output .= "\$params = var_export(\$params, true);\n";
143             $output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
144             $output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value, \\\$_smarty_tpl->isRenderingCache);\n}\n?>";
145             $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>";
146             $compiler->parser->current_buffer->append_subtree($compiler->parser,
147                                                               new Smarty_Internal_ParseTree_Tag($compiler->parser,
148                                                                                                 $output));
149             $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
150             $output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
151             $output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
152             $output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
153             $output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
154             $output .= "}\n}\n";
155             $output .= "/*/ {$_funcName}_nocache */\n\n";
156             $output .= "?>\n";
157             $compiler->parser->current_buffer->append_subtree($compiler->parser,
158                                                               new Smarty_Internal_ParseTree_Tag($compiler->parser,
159                                                                                                 $output));
160             $_functionCode = new Smarty_Internal_ParseTree_Tag($compiler->parser,
161                                                                preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
162                                                                                      array($this, 'removeNocache'),
163                                                                                      $_functionCode->to_smarty_php($compiler->parser)));
164         }
165         $compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
166         $output = "<?php\n";
167         $output .= "/* {$_funcName} */\n";
168         $output .= "if (!function_exists('{$_funcName}')) {\n";
169         $output .= "function {$_funcName}(\$_smarty_tpl,\$params) {\n";
170         $output .= $_paramsCode;
171         $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}?>";
172         $compiler->parser->current_buffer->append_subtree($compiler->parser,
173                                                           new Smarty_Internal_ParseTree_Tag($compiler->parser,
174                                                                                             $output));
175         $compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
176         $output = "<?php\n}}\n";
177         $output .= "/*/ {$_funcName} */\n\n";
178         $output .= "?>\n";
179         $compiler->parser->current_buffer->append_subtree($compiler->parser,
180                                                           new Smarty_Internal_ParseTree_Tag($compiler->parser,
181                                                                                             $output));
182         $compiler->parent_compiler->blockOrFunctionCode .= $compiler->parser->current_buffer->to_smarty_php($compiler->parser);
183         // nocache plugins must be copied
184         if (!empty($compiler->template->compiled->required_plugins[ 'nocache' ])) {
185             foreach ($compiler->template->compiled->required_plugins[ 'nocache' ] as $plugin => $tmp) {
186                 foreach ($tmp as $type => $data) {
187                     $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ $plugin ][ $type ] =
188                         $data;
189                 }
190             }
191         }
192         // restore old buffer
193
194         $compiler->parser->current_buffer = $saved_data[ 1 ];
195         // restore old status
196         $compiler->template->compiled->has_nocache_code = $saved_data[ 2 ];
197         $compiler->template->caching = $saved_data[ 3 ];
198         return true;
199     }
200
201     /**
202      * Remove nocache code
203      * 
204      * @param $match
205      *
206      * @return string
207      */
208     function removeNocache($match)
209     {
210         $code =
211             preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->compiled->nocache_hash}%%\*\/';(\?>\n)?)/",
212                          '', $match[ 0 ]);
213         $code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
214         return $code;
215     }
216 }