smarty_internal_runtime_codeframe.php 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Smarty Internal Extension
  4. * This file contains the Smarty template extension to create a code frame
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Class Smarty_Internal_Extension_CodeFrame
  12. * Create code frame for compiled and cached templates
  13. */
  14. class Smarty_Internal_Runtime_CodeFrame
  15. {
  16. /**
  17. * Create code frame for compiled and cached templates
  18. *
  19. * @param Smarty_Internal_Template $_template
  20. * @param string $content optional template content
  21. * @param string $functions compiled template function and block code
  22. * @param bool $cache flag for cache file
  23. * @param \Smarty_Internal_TemplateCompilerBase $compiler
  24. *
  25. * @return string
  26. */
  27. public function create(Smarty_Internal_Template $_template, $content = '', $functions = '', $cache = false,
  28. Smarty_Internal_TemplateCompilerBase $compiler = null)
  29. {
  30. // build property code
  31. $properties[ 'version' ] = Smarty::SMARTY_VERSION;
  32. $properties[ 'unifunc' ] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
  33. if (!$cache) {
  34. $properties[ 'has_nocache_code' ] = $_template->compiled->has_nocache_code;
  35. $properties[ 'file_dependency' ] = $_template->compiled->file_dependency;
  36. $properties[ 'includes' ] = $_template->compiled->includes;
  37. } else {
  38. $properties[ 'has_nocache_code' ] = $_template->cached->has_nocache_code;
  39. $properties[ 'file_dependency' ] = $_template->cached->file_dependency;
  40. $properties[ 'cache_lifetime' ] = $_template->cache_lifetime;
  41. }
  42. $output = "<?php\n";
  43. $output .= "/* Smarty version " . Smarty::SMARTY_VERSION . ", created on " . strftime("%Y-%m-%d %H:%M:%S") .
  44. "\n from \"" . $_template->source->filepath . "\" */\n\n";
  45. $output .= "/* @var Smarty_Internal_Template \$_smarty_tpl */\n";
  46. $dec = "\$_smarty_tpl->_decodeProperties(\$_smarty_tpl, " . var_export($properties, true) . ',' .
  47. ($cache ? 'true' : 'false') . ")";
  48. $output .= "if ({$dec}) {\n";
  49. $output .= "function {$properties['unifunc']} (Smarty_Internal_Template \$_smarty_tpl) {\n";
  50. if (!$cache && !empty($compiler->tpl_function)) {
  51. $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
  52. var_export($compiler->tpl_function, true) . ");\n";
  53. }
  54. if ($cache && isset($_template->smarty->ext->_tplFunction)) {
  55. $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
  56. var_export($_template->smarty->ext->_tplFunction->getTplFunction($_template), true) . ");\n";
  57. }
  58. // include code for plugins
  59. if (!$cache) {
  60. if (!empty($_template->compiled->required_plugins[ 'compiled' ])) {
  61. foreach ($_template->compiled->required_plugins[ 'compiled' ] as $tmp) {
  62. foreach ($tmp as $data) {
  63. $file = addslashes($data[ 'file' ]);
  64. if (is_array($data[ 'function' ])) {
  65. $output .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n";
  66. } else {
  67. $output .= "if (!is_callable('{$data['function']}')) require_once '{$file}';\n";
  68. }
  69. }
  70. }
  71. }
  72. if ($_template->caching && !empty($_template->compiled->required_plugins[ 'nocache' ])) {
  73. $_template->compiled->has_nocache_code = true;
  74. $output .= "echo '/*%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
  75. foreach ($_template->compiled->required_plugins[ 'nocache' ] as $tmp) {
  76. foreach ($tmp as $data) {
  77. $file = addslashes($data[ 'file' ]);
  78. if (is_array($data[ 'function' ])) {
  79. $output .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) require_once '{$file}';\n");
  80. } else {
  81. $output .= addslashes("if (!is_callable('{$data['function']}')) require_once '{$file}';\n");
  82. }
  83. }
  84. }
  85. $output .= "?>/*/%%SmartyNocache:{$_template->compiled->nocache_hash}%%*/';\n";
  86. }
  87. }
  88. $output .= "?>\n";
  89. $output .= $content;
  90. $output .= "<?php }\n?>";
  91. $output .= $functions;
  92. $output .= "<?php }\n";
  93. // remove unneeded PHP tags
  94. return preg_replace(array('/\s*\?>[\n]?<\?php\s*/', '/\?>\s*$/'), array("\n", ''), $output);
  95. }
  96. }