libs/sysplugins/smarty_internal_compile_function.php Quellcode

smarty_internal_compile_function.php
gehe zur Dokumentation dieser Datei
1 <?php
18 {
19 
26  public $required_attributes = array('name');
27 
34  public $shorttag_order = array('name');
35 
42  public $optional_attributes = array('_any');
43 
53  public function compile($args, $compiler, $parameter)
54  {
55  // check and get attributes
56  $_attr = $this->getAttributes($compiler, $args);
57 
58  if ($_attr['nocache'] === true) {
59  $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
60  }
61  unset($_attr['nocache']);
62  $_name = trim($_attr['name'], "'\"");
63  $compiler->parent_compiler->templateProperties['tpl_function'][$_name] = array();
64  $save = array($_attr, $compiler->parser->current_buffer, $compiler->template->has_nocache_code, $compiler->template->required_plugins, $compiler->template->caching);
65  $this->openTag($compiler, 'function', $save);
66  // set flag that we are compiling a template function
67  $compiler->compiles_template_function = true;
68  // Init temporary context
69  $compiler->template->required_plugins = array('compiled' => array(), 'nocache' => array());
70  $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template($compiler->parser);
71  $compiler->template->has_nocache_code = false;
72  $compiler->template->caching = true;
73  return true;
74  }
75 }
76 
84 {
85 
91  private $compiler = null;
92 
102  public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
103  {
104  $this->compiler = $compiler;
105  $saved_data = $this->closeTag($compiler, array('function'));
106  $_attr = $saved_data[0];
107  $_name = trim($_attr['name'], "'\"");
108  // reset flag that we are compiling a template function
109  $compiler->compiles_template_function = false;
110  $compiler->parent_compiler->templateProperties['tpl_function'][$_name]['called_functions'] = $compiler->called_functions;
111  $compiler->parent_compiler->templateProperties['tpl_function'][$_name]['compiled_filepath'] = $compiler->parent_compiler->template->compiled->filepath;
112  $compiler->parent_compiler->templateProperties['tpl_function'][$_name]['uid'] = $compiler->template->source->uid;
113  $compiler->called_functions = array();
114  $_parameter = $_attr;
115  unset($_parameter['name']);
116  // default parameter
117  $_paramsArray = array();
118  foreach ($_parameter as $_key => $_value) {
119  if (is_int($_key)) {
120  $_paramsArray[] = "$_key=>$_value";
121  } else {
122  $_paramsArray[] = "'$_key'=>$_value";
123  }
124  }
125  if (!empty($_paramsArray)) {
126  $_params = 'array(' . implode(",", $_paramsArray) . ')';
127  $_paramsCode = "\$params = array_merge($_params, \$params);\n";
128  } else {
129  $_paramsCode = '';
130  }
131  $_functionCode = $compiler->parser->current_buffer;
132  // setup buffer for template function code
133  $compiler->parser->current_buffer = new Smarty_Internal_ParseTree_Template($compiler->parser);
134 
135  $_funcName = "smarty_template_function_{$_name}_{$compiler->template->properties['nocache_hash']}";
136  $_funcNameCaching = $_funcName . '_nocache';
137  if ($compiler->template->has_nocache_code) {
138  $compiler->parent_compiler->templateProperties['tpl_function'][$_name]['call_name_caching'] = $_funcNameCaching;
139  $output = "<?php\n";
140  $output .= "/* {$_funcNameCaching} */\n";
141  $output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
142  $output .= "function {$_funcNameCaching} (\$_smarty_tpl,\$params) {\n";
143  // build plugin include code
144  if (!empty($compiler->template->required_plugins['compiled'])) {
145  foreach ($compiler->template->required_plugins['compiled'] as $tmp) {
146  foreach ($tmp as $data) {
147  $output .= "if (!is_callable('{$data['function']}')) require_once '{$data['file']}';\n";
148  }
149  }
150  }
151  if (!empty($compiler->template->required_plugins['nocache'])) {
152  $output .= "echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
153  foreach ($compiler->template->required_plugins['nocache'] as $tmp) {
154  foreach ($tmp as $data) {
155  $output .= "if (!is_callable(\'{$data['function']}\')) require_once \'{$data['file']}\';\n";
156  }
157  }
158  $output .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';\n";
159  }
160  $output .= "ob_start();\n";
161  $output .= $_paramsCode;
162  $output .= "\$_smarty_tpl->properties['saved_tpl_vars'][] = \$_smarty_tpl->tpl_vars;\n";
163  $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value);\n}";
164  $output .= "\$params = var_export(\$params, true);\n";
165  $output .= "echo \"/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
166  $output .= "\\\$saved_tpl_vars = \\\$_smarty_tpl->tpl_vars;\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value);\n}\n?>";
167  $output .= "/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/\n\";?>";
168  $compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Tag($compiler->parser, $output));
169  $compiler->parser->current_buffer->append_subtree($_functionCode);
170  $output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
171  $output .= "foreach (Smarty::\\\$global_tpl_vars as \\\$key => \\\$value){\n";
172  $output .= "if (\\\$_smarty_tpl->tpl_vars[\\\$key] === \\\$value) \\\$saved_tpl_vars[\\\$key] = \\\$value;\n}\n";
173  $output .= "\\\$_smarty_tpl->tpl_vars = \\\$saved_tpl_vars;?>\n";
174  $output .= "/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/\";\n?>";
175  $output .= "<?php echo str_replace('{$compiler->template->properties['nocache_hash']}', \$_smarty_tpl->properties['nocache_hash'], ob_get_clean());\n";
176  $output .= "\$_smarty_tpl->tpl_vars = array_pop(\$_smarty_tpl->properties['saved_tpl_vars']);\n}\n}\n";
177  $output .= "/*/ {$_funcName}_nocache */\n\n";
178  $output .= "?>\n";
179  $compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Tag($compiler->parser, $output));
180  $_functionCode = new Smarty_Internal_ParseTree_Tag($compiler->parser, preg_replace_callback("/((<\?php )?echo '\/\*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%\*\/([\S\s]*?)\/\*\/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%\*\/';(\?>\n)?)/", array($this, 'removeNocache'), $_functionCode->to_smarty_php()));
181  }
182  $compiler->parent_compiler->templateProperties['tpl_function'][$_name]['call_name'] = $_funcName;
183  $output = "<?php\n";
184  $output .= "/* {$_funcName} */\n";
185  $output .= "if (!function_exists('{$_funcName}')) {\n";
186  $output .= "function {$_funcName}(\$_smarty_tpl,\$params) {\n";
187  // build plugin include code
188  if (!empty($compiler->template->required_plugins['nocache'])) {
189  $compiler->template->required_plugins['compiled'] = array_merge($compiler->template->required_plugins['compiled'], $compiler->template->required_plugins['nocache']);
190  }
191  if (!empty($compiler->template->required_plugins['compiled'])) {
192  foreach ($compiler->template->required_plugins['compiled'] as $tmp) {
193  foreach ($tmp as $data) {
194  $output .= "if (!is_callable('{$data['function']}')) require_once '{$data['file']}';\n";
195  }
196  }
197  }
198  $output .= "\$saved_tpl_vars = \$_smarty_tpl->tpl_vars;\n";
199  $output .= $_paramsCode;
200  $output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value);\n}?>";
201  $compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Tag($compiler->parser, $output));
202  $compiler->parser->current_buffer->append_subtree($_functionCode);
203  $output = "<?php foreach (Smarty::\$global_tpl_vars as \$key => \$value){\n";
204  $output .= "if (\$_smarty_tpl->tpl_vars[\$key] === \$value) \$saved_tpl_vars[\$key] = \$value;\n}\n";
205  $output .= "\$_smarty_tpl->tpl_vars = \$saved_tpl_vars;\n}\n}\n";
206  $output .= "/*/ {$_funcName} */\n\n";
207  $output .= "?>\n";
208  $compiler->parser->current_buffer->append_subtree(new Smarty_Internal_ParseTree_Tag($compiler->parser, $output));
209  $compiler->parent_compiler->templateFunctionCode .= $compiler->parser->current_buffer->to_smarty_php();
210  // restore old buffer
211  $compiler->parser->current_buffer = $saved_data[1];
212  // restore old status
213  $compiler->template->has_nocache_code = $saved_data[2];
214  $compiler->template->required_plugins = $saved_data[3];
215  $compiler->template->caching = $saved_data[4];
216  return true;
217  }
218 
224  function removeNocache($match)
225  {
226  $code = preg_replace("/((<\?php )?echo '\/\*%%SmartyNocache:{$this->compiler->template->properties['nocache_hash']}%%\*\/)|(\/\*\/%%SmartyNocache:{$this->compiler->template->properties['nocache_hash']}%%\*\/';(\?>\n)?)/", '', $match[0]);
227  $code = str_replace(array('\\\'', '\\\\\''), array('\'', '\\\''), $code);
228  return $code;
229  }
230 }




Korrekturen, Hinweise und Ergänzungen

Bitte scheuen Sie sich nicht und melden Sie, was auf dieser Seite sachlich falsch oder irreführend ist, was ergänzt werden sollte, was fehlt usw. Dazu bitte oben aus dem Menü Seite den Eintrag Support Forum wählen. Es ist eine kostenlose Anmeldung erforderlich, um Anmerkungen zu posten. Unpassende Postings, Spam usw. werden kommentarlos entfernt.