/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_compile_function.php

http://zoop.googlecode.com/ · PHP · 100 lines · 77 code · 1 blank · 22 comment · 2 complexity · e7b5a1d176b4f158845385e943febb35 MD5 · raw file

  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. }
  48. /**
  49. * Smarty Internal Plugin Compile Functionclose Class
  50. */
  51. class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase {
  52. /**
  53. * Compiles code for the {/function} tag
  54. *
  55. * @param array $args array with attributes from parser
  56. * @param object $compiler compiler object
  57. * @return boolean true
  58. */
  59. public function compile($args, $compiler)
  60. {
  61. $this->compiler = $compiler;
  62. $this->compiler->has_code = false;
  63. $_attr = $this->_get_attributes($args);
  64. $saved_data = $this->_close_tag(array('function'));
  65. $_name = trim($saved_data[0]['name'], "'");
  66. // build plugin include code
  67. $plugins_string = '';
  68. if (!empty($compiler->template->required_plugins['compiled'])) {
  69. $plugins_string = '<?php ';
  70. foreach($compiler->template->required_plugins['compiled'] as $tmp) {
  71. foreach($tmp as $data) {
  72. $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
  73. }
  74. }
  75. $plugins_string .= '?>';
  76. }
  77. if (!empty($compiler->template->required_plugins['cache'])) {
  78. $plugins_string .= "<?php echo '/*%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/<?php ";
  79. foreach($compiler->template->required_plugins['nocache'] as $tmp) {
  80. foreach($tmp as $data) {
  81. $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$data['file']}';\n";
  82. }
  83. }
  84. $plugins_string .= "?>/*/%%SmartyNocache:{$compiler->template->properties['nocache_hash']}%%*/';?>\n";
  85. }
  86. $compiler->template->properties['function'][$_name]['compiled'] = $plugins_string . $compiler->template->extracted_compiled_code;
  87. $compiler->template->properties['function'][$_name]['nocache_hash'] = $compiler->template->properties['nocache_hash'];
  88. $compiler->template->properties['function'][$_name]['has_nocache_code'] = $compiler->template->has_nocache_code;
  89. $this->compiler->smarty->template_functions[$_name] = $compiler->template->properties['function'][$_name];
  90. // restore old compiler status
  91. $compiler->template->extracted_compiled_code = $saved_data[1];
  92. $compiler->template->extract_code = $saved_data[2];
  93. $compiler->template->has_nocache_code = $saved_data[3];
  94. $compiler->template->required_plugins = $saved_data[4];
  95. return true;
  96. }
  97. }
  98. ?>