You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_codeframe.php 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 {$properties[ 'version' ]}, created on " . strftime("%Y-%m-%d %H:%M:%S") .
  44. "\n from '" . str_replace('*/','* /',$_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. $output .= var_export($compiler->tpl_function, true);
  53. $output .= ");\n";
  54. }
  55. if ($cache && isset($_template->smarty->ext->_tplFunction)) {
  56. $output .= "\$_smarty_tpl->smarty->ext->_tplFunction->registerTplFunctions(\$_smarty_tpl, " .
  57. var_export($_template->smarty->ext->_tplFunction->getTplFunction($_template), true) . ");\n";
  58. }
  59. // include code for required plugins
  60. if (!$cache && isset($compiler)) {
  61. $output .= $compiler->compileRequiredPlugins();
  62. }
  63. $output .= "?>";
  64. $output .= $content;
  65. $output .= "<?php }\n?>";
  66. $output .= $functions;
  67. $output .= "<?php }\n";
  68. // remove unneeded PHP tags
  69. if (preg_match('/\s*\?>[\n]?<\?php\s*/', $output)) {
  70. $curr_split = preg_split('/\s*\?>[\n]?<\?php\s*/',
  71. $output);
  72. preg_match_all('/\s*\?>[\n]?<\?php\s*/',
  73. $output,
  74. $curr_parts);
  75. $output = '';
  76. foreach ($curr_split as $idx => $curr_output) {
  77. $output .= $curr_output;
  78. if (isset($curr_parts[ 0 ][ $idx ])) {
  79. $output .= "\n";
  80. }
  81. }
  82. }
  83. if (preg_match('/\?>\s*$/', $output)) {
  84. $curr_split = preg_split('/\?>\s*$/',
  85. $output);
  86. $output = '';
  87. foreach ($curr_split as $idx => $curr_output) {
  88. $output .= $curr_output;
  89. }
  90. }
  91. return $output;
  92. }
  93. }