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_method_clearcompiledtemplate.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Smarty Method ClearCompiledTemplate
  4. *
  5. * Smarty::clearCompiledTemplate() method
  6. *
  7. * @package Smarty
  8. * @subpackage PluginsInternal
  9. * @author Uwe Tews
  10. */
  11. class Smarty_Internal_Method_ClearCompiledTemplate
  12. {
  13. /**
  14. * Valid for Smarty object
  15. *
  16. * @var int
  17. */
  18. public $objMap = 1;
  19. /**
  20. * Delete compiled template file
  21. *
  22. * @api Smarty::clearCompiledTemplate()
  23. * @link http://www.smarty.net/docs/en/api.clear.compiled.template.tpl
  24. *
  25. * @param \Smarty $smarty
  26. * @param string $resource_name template name
  27. * @param string $compile_id compile id
  28. * @param integer $exp_time expiration time
  29. *
  30. * @return integer number of template files deleted
  31. */
  32. public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $compile_id = null, $exp_time = null)
  33. {
  34. $_compile_dir = $smarty->getCompileDir();
  35. if ($_compile_dir == '/') { //We should never want to delete this!
  36. return 0;
  37. }
  38. $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
  39. $_dir_sep = $smarty->use_sub_dirs ? DS : '^';
  40. if (isset($resource_name)) {
  41. $_save_stat = $smarty->caching;
  42. $smarty->caching = false;
  43. /* @var Smarty_Internal_Template $tpl */
  44. $tpl = new $smarty->template_class($resource_name, $smarty);
  45. $smarty->caching = $_save_stat;
  46. if ($tpl->source->exists) {
  47. // remove from compileds cache
  48. $tpl->source->compileds = array();
  49. $_resource_part_1 = basename(str_replace('^', DS, $tpl->compiled->filepath));
  50. $_resource_part_1_length = strlen($_resource_part_1);
  51. } else {
  52. return 0;
  53. }
  54. $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1);
  55. $_resource_part_2_length = strlen($_resource_part_2);
  56. }
  57. $_dir = $_compile_dir;
  58. if ($smarty->use_sub_dirs && isset($_compile_id)) {
  59. $_dir .= $_compile_id . $_dir_sep;
  60. }
  61. if (isset($_compile_id)) {
  62. $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep;
  63. $_compile_id_part_length = strlen($_compile_id_part);
  64. }
  65. $_count = 0;
  66. try {
  67. $_compileDirs = new RecursiveDirectoryIterator($_dir);
  68. // NOTE: UnexpectedValueException thrown for PHP >= 5.3
  69. }
  70. catch (Exception $e) {
  71. return 0;
  72. }
  73. $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
  74. foreach ($_compile as $_file) {
  75. if (substr(basename($_file->getPathname()), 0, 1) == '.' || strpos($_file, '.svn') !== false) {
  76. continue;
  77. }
  78. $_filepath = (string) $_file;
  79. if ($_file->isDir()) {
  80. if (!$_compile->isDot()) {
  81. // delete folder if empty
  82. @rmdir($_file->getPathname());
  83. }
  84. } else {
  85. $unlink = false;
  86. if ((!isset($_compile_id) || (isset($_filepath[$_compile_id_part_length]) &&
  87. $a = !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) &&
  88. (!isset($resource_name) || (isset($_filepath[$_resource_part_1_length]) &&
  89. substr_compare($_filepath, $_resource_part_1, - $_resource_part_1_length,
  90. $_resource_part_1_length) == 0) ||
  91. (isset($_filepath[$_resource_part_2_length]) &&
  92. substr_compare($_filepath, $_resource_part_2, - $_resource_part_2_length,
  93. $_resource_part_2_length) == 0))
  94. ) {
  95. if (isset($exp_time)) {
  96. if (time() - @filemtime($_filepath) >= $exp_time) {
  97. $unlink = true;
  98. }
  99. } else {
  100. $unlink = true;
  101. }
  102. }
  103. if ($unlink && @unlink($_filepath)) {
  104. $_count ++;
  105. if (function_exists('opcache_invalidate')) {
  106. opcache_invalidate($_filepath);
  107. }
  108. }
  109. }
  110. }
  111. // clear template objects cache
  112. $smarty->_cache['isCached'] = array();
  113. if (isset($smarty->ext->_subtemplate)) {
  114. $smarty->ext->_subtemplate->tplObjects = array();
  115. }
  116. return $_count;
  117. }
  118. }