core.write_compiled_include.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * Extract non-cacheable parts out of compiled template and write it
  9. *
  10. * @param string $compile_path
  11. * @param string $template_compiled
  12. * @return boolean
  13. */
  14. function smarty_core_write_compiled_include($params, &$smarty)
  15. {
  16. $_tag_start = 'if \(\$this->caching && \!\$this->_cache_including\) \{ echo \'\{nocache\:('.$params['cache_serial'].')#(\d+)\}\'; \};';
  17. $_tag_end = 'if \(\$this->caching && \!\$this->_cache_including\) \{ echo \'\{/nocache\:(\\2)#(\\3)\}\'; \};';
  18. preg_match_all('!('.$_tag_start.'(.*)'.$_tag_end.')!Us',
  19. $params['compiled_content'], $_match_source, PREG_SET_ORDER);
  20. // no nocache-parts found: done
  21. if (count($_match_source)==0) return;
  22. // convert the matched php-code to functions
  23. $_include_compiled = "<?php /* Smarty version ".$smarty->_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n";
  24. $_include_compiled .= " compiled from " . strtr(urlencode($params['resource_name']), array('%2F'=>'/', '%3A'=>':')) . " */\n\n";
  25. $_compile_path = $params['include_file_path'];
  26. $smarty->_cache_serials[$_compile_path] = $params['cache_serial'];
  27. $_include_compiled .= "\$this->_cache_serials['".$_compile_path."'] = '".$params['cache_serial']."';\n\n?>";
  28. $_include_compiled .= $params['plugins_code'];
  29. $_include_compiled .= "<?php";
  30. $this_varname = ((double)phpversion() >= 5.0) ? '_smarty' : 'this';
  31. for ($_i = 0, $_for_max = count($_match_source); $_i < $_for_max; $_i++) {
  32. $_match =& $_match_source[$_i];
  33. $source = $_match[4];
  34. if ($this_varname == '_smarty') {
  35. /* rename $this to $_smarty in the sourcecode */
  36. $tokens = token_get_all('<?php ' . $_match[4]);
  37. /* remove trailing <?php */
  38. $open_tag = '';
  39. while ($tokens) {
  40. $token = array_shift($tokens);
  41. if (is_array($token)) {
  42. $open_tag .= $token[1];
  43. } else {
  44. $open_tag .= $token;
  45. }
  46. if ($open_tag == '<?php ') break;
  47. }
  48. for ($i=0, $count = count($tokens); $i < $count; $i++) {
  49. if (is_array($tokens[$i])) {
  50. if ($tokens[$i][0] == T_VARIABLE && $tokens[$i][1] == '$this') {
  51. $tokens[$i] = '$' . $this_varname;
  52. } else {
  53. $tokens[$i] = $tokens[$i][1];
  54. }
  55. }
  56. }
  57. $source = implode('', $tokens);
  58. }
  59. /* add function to compiled include */
  60. $_include_compiled .= "
  61. function _smarty_tplfunc_$_match[2]_$_match[3](&\$$this_varname)
  62. {
  63. $source
  64. }
  65. ";
  66. }
  67. $_include_compiled .= "\n\n?>\n";
  68. $_params = array('filename' => $_compile_path,
  69. 'contents' => $_include_compiled, 'create_dirs' => true);
  70. require_once(SMARTY_CORE_DIR . 'core.write_file.php');
  71. smarty_core_write_file($_params, $smarty);
  72. return true;
  73. }
  74. ?>