smarty_internal_method_clearcompiledtemplate.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // clear template objects cache
  35. $smarty->_clearTemplateCache();
  36. $_compile_dir = $smarty->getCompileDir();
  37. if ($_compile_dir == '/') { //We should never want to delete this!
  38. return 0;
  39. }
  40. $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
  41. $_dir_sep = $smarty->use_sub_dirs ? DS : '^';
  42. if (isset($resource_name)) {
  43. $_save_stat = $smarty->caching;
  44. $smarty->caching = false;
  45. /* @var Smarty_Internal_Template $tpl */
  46. $tpl = new $smarty->template_class($resource_name, $smarty);
  47. $smarty->caching = $_save_stat;
  48. if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) {
  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 ]) && $a =
  87. !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,
  90. - $_resource_part_1_length, $_resource_part_1_length) ==
  91. 0) || (isset($_filepath[ $_resource_part_2_length ]) &&
  92. substr_compare($_filepath, $_resource_part_2,
  93. - $_resource_part_2_length,
  94. $_resource_part_2_length) == 0))
  95. ) {
  96. if (isset($exp_time)) {
  97. if (time() - @filemtime($_filepath) >= $exp_time) {
  98. $unlink = true;
  99. }
  100. } else {
  101. $unlink = true;
  102. }
  103. }
  104. if ($unlink && @unlink($_filepath)) {
  105. $_count ++;
  106. if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
  107. opcache_invalidate($_filepath, true);
  108. }
  109. }
  110. }
  111. }
  112. return $_count;
  113. }
  114. }