smarty_internal_compile_function.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * @package Smarty
  15. * @subpackage Compiler
  16. */
  17. class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase {
  18. /**
  19. * Attribute definition: Overwrites base class.
  20. *
  21. * @var array
  22. * @see Smarty_Internal_CompileBase
  23. */
  24. public $required_attributes = array('name');
  25. /**
  26. * Attribute definition: Overwrites base class.
  27. *
  28. * @var array
  29. * @see Smarty_Internal_CompileBase
  30. */
  31. public $shorttag_order = array('name');
  32. /**
  33. * Attribute definition: Overwrites base class.
  34. *
  35. * @var array
  36. * @see Smarty_Internal_CompileBase
  37. */
  38. public $optional_attributes = array('_any');
  39. /**
  40. * Compiles code for the {function} tag
  41. *
  42. * @param array $args array with attributes from parser
  43. * @param object $compiler compiler object
  44. * @param array $parameter array with compilation parameter
  45. * @return boolean true
  46. */
  47. public function compile($args, $compiler, $parameter)
  48. {
  49. // check and get attributes
  50. $_attr = $this->getAttributes($compiler, $args);
  51. if ($_attr['nocache'] === true) {
  52. $compiler->trigger_template_error('nocache option not allowed', $compiler->lex->taglineno);
  53. }
  54. unset($_attr['nocache']);
  55. $save = array($_attr, $compiler->parser->current_buffer,
  56. $compiler->template->has_nocache_code, $compiler->template->required_plugins);
  57. $this->openTag($compiler, 'function', $save);
  58. $_name = trim($_attr['name'], "'\"");
  59. unset($_attr['name']);
  60. // set flag that we are compiling a template function
  61. $compiler->compiles_template_function = true;
  62. $compiler->template->properties['function'][$_name]['parameter'] = array();
  63. $_smarty_tpl = $compiler->template;
  64. foreach ($_attr as $_key => $_data) {
  65. eval ('$tmp='.$_data.';');
  66. $compiler->template->properties['function'][$_name]['parameter'][$_key] = $tmp;
  67. }
  68. $compiler->smarty->template_functions[$_name]['parameter'] = $compiler->template->properties['function'][$_name]['parameter'];
  69. if ($compiler->template->caching) {
  70. $output = '';
  71. } else {
  72. $output = "<?php if (!function_exists('smarty_template_function_{$_name}')) {
  73. function smarty_template_function_{$_name}(\$_smarty_tpl,\$params) {
  74. \$saved_tpl_vars = \$_smarty_tpl->tpl_vars;
  75. foreach (\$_smarty_tpl->smarty->template_functions['{$_name}']['parameter'] as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);};
  76. foreach (\$params as \$key => \$value) {\$_smarty_tpl->tpl_vars[\$key] = new Smarty_variable(\$value);}?>";
  77. }
  78. // Init temporay context
  79. $compiler->template->required_plugins = array('compiled' => array(), 'nocache' => array());
  80. $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
  81. $compiler->parser->current_buffer->append_subtree(new _smarty_tag($compiler->parser, $output));
  82. $compiler->template->has_nocache_code = false;
  83. $compiler->has_code = false;
  84. $compiler->template->properties['function'][$_name]['compiled'] = '';
  85. return true;
  86. }
  87. }
  88. /**
  89. * Smarty Internal Plugin Compile Functionclose Class
  90. *
  91. * @package Smarty
  92. * @subpackage Compiler
  93. */
  94. class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase {
  95. /**
  96. * Compiles code for the {/function} tag
  97. *
  98. * @param array $args array with attributes from parser
  99. * @param object $compiler compiler object
  100. * @param array $parameter array with compilation parameter
  101. * @return boolean true
  102. */
  103. public function compile($args, $compiler, $parameter)
  104. {
  105. $_attr = $this->getAttributes($compiler, $args);
  106. $saved_data = $this->closeTag($compiler, array('function'));
  107. $_name = trim($saved_data[0]['name'], "'\"");
  108. // build plugin include code
  109. $plugins_string = '';
  110. if (!empty($compiler->template->required_plugins['compiled'])) {
  111. $plugins_string = '<?php ';
  112. foreach($compiler->template->required_plugins['compiled'] as $tmp) {
  113. foreach($tmp as $data) {
  114. $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
  115. }
  116. }
  117. $plugins_string .= '?>';
  118. }
  119. if (!empty($compiler->template->required_plugins['nocache'])) {
  120. $plugins_string .= "<?php echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
  121. foreach($compiler->template->required_plugins['nocache'] as $tmp) {
  122. foreach($tmp as $data) {
  123. $plugins_string .= "if (!is_callable(\'{$data['function']}\')) include \'{$data['file']}\';\n";
  124. }
  125. }
  126. $plugins_string .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';?>\n";
  127. }
  128. // remove last line break from function definition
  129. $last = count($compiler->parser->current_buffer->subtrees) - 1;
  130. if ($compiler->parser->current_buffer->subtrees[$last] instanceof _smarty_linebreak) {
  131. unset($compiler->parser->current_buffer->subtrees[$last]);
  132. }
  133. // if caching save template function for possible nocache call
  134. if ($compiler->template->caching) {
  135. $compiler->template->properties['function'][$_name]['compiled'] .= $plugins_string
  136. . $compiler->parser->current_buffer->to_smarty_php();
  137. $compiler->template->properties['function'][$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  138. $compiler->template->properties['function'][$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
  139. $compiler->template->properties['function'][$_name]['called_functions'] = $compiler->called_functions;
  140. $compiler->called_functions = array();
  141. $compiler->smarty->template_functions[$_name] = $compiler->template->properties['function'][$_name];
  142. $compiler->has_code = false;
  143. $output = true;
  144. } else {
  145. $output = $plugins_string . $compiler->parser->current_buffer->to_smarty_php() . "<?php \$_smarty_tpl->tpl_vars = \$saved_tpl_vars;}}?>\n";
  146. }
  147. // reset flag that we are compiling a template function
  148. $compiler->compiles_template_function = false;
  149. // restore old compiler status
  150. $compiler->parser->current_buffer = $saved_data[1];
  151. $compiler->template->has_nocache_code = $compiler->template->has_nocache_code | $saved_data[2];
  152. $compiler->template->required_plugins = $saved_data[3];
  153. return $output;
  154. }
  155. }
  156. ?>