1 <?php
2 /**
3 * Smarty Internal Plugin Compile Function
4 *
5 * Compiles the {function} {/function} tags
6 *
7 * @package Smarty
8 * @subpackage Compiler
9 * @author Uwe Tews
10 */
11 /**
12 * Smarty Internal Plugin Compile Function Class
13 */
14 class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase {
15 /**
16 * Compiles code for the {function} tag
17 *
18 * @param array $args array with attributes from parser
19 * @param object $compiler compiler object
20 * @return boolean true
21 */
22 public function compile($args, $compiler)
23 {
24 $this->compiler = $compiler;
25 $this->required_attributes = array('name');
26 $this->optional_attributes = array('_any');
27 // check and get attributes
28 $_attr = $this->_get_attributes($args);
29 $save = array($_attr, $compiler->template->extracted_compiled_code, $compiler->template->extract_code,
30 $compiler->template->has_nocache_code, $compiler->template->required_plugins);
31 $this->_open_tag('function', $save);
32 $_name = trim($_attr['name'], "'\"");
33 unset($_attr['name']);
34 foreach ($_attr as $_key => $_data) {
35 $compiler->template->properties['function'][$_name]['parameter'][$_key] = $_data;
36 }
37 // make function known for recursive calls
38 $this->compiler->smarty->template_functions[$_name]['compiled'] = '';
39 // Init temporay context
40 $compiler->template->required_plugins = array('compiled' => array(), 'cache' => array());
41 $compiler->template->extract_code = true;
42 $compiler->template->extracted_compiled_code = '';
43 $compiler->template->has_nocache_code = false;
44 $compiler->has_code = false;
45 return true;
46 }
47 }
49 /**
50 * Smarty Internal Plugin Compile Functionclose Class
51 */
52 class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase {
53 /**
54 * Compiles code for the {/function} tag
55 *
56 * @param array $args array with attributes from parser
57 * @param object $compiler compiler object
58 * @return boolean true
59 */
60 public function compile($args, $compiler)
61 {
62 $this->compiler = $compiler;
63 $this->compiler->has_code = false;
64 $_attr = $this->_get_attributes($args);
65 $saved_data = $this->_close_tag(array('function'));
66 $_name = trim($saved_data[0]['name'], "'");
67 // build plugin include code
68 $plugins_string = '';
69 if (!empty($compiler->template->required_plugins['compiled'])) {
70 $plugins_string = '<?php ';
71 foreach($compiler->template->required_plugins['compiled'] as $tmp) {
72 foreach($tmp as $data) {
73 $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
74 }
75 }
76 $plugins_string .= '?>';
77 }
78 if (!empty($compiler->template->required_plugins['cache'])) {
79 $plugins_string .= "<?php echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
80 foreach($compiler->template->required_plugins['nocache'] as $tmp) {
81 foreach($tmp as $data) {
82 $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
83 }
84 }
85 $plugins_string .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';?>\n";
86 }
87 $compiler->template->properties['function'][$_name]['compiled'] = $plugins_string . $compiler->template->extracted_compiled_code;
88 $compiler->template->properties['function'][$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
89 $compiler->template->properties['function'][$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
90 $this->compiler->smarty->template_functions[$_name] = $compiler->template->properties['function'][$_name];
91 // restore old compiler status
92 $compiler->template->extracted_compiled_code = $saved_data[1];
93 $compiler->template->extract_code = $saved_data[2];
94 $compiler->template->has_nocache_code = $saved_data[3];
95 $compiler->template->required_plugins = $saved_data[4];
96 return true;
97 }
98 }
100 ?>