4 * Smarty Method ClearCompiledTemplate
6 * Smarty::clearCompiledTemplate() method
9 * @subpackage PluginsInternal
12 class Smarty_Internal_Method_ClearCompiledTemplate
15 * Valid for Smarty object
22 * Delete compiled template file
24 * @api Smarty::clearCompiledTemplate()
25 * @link http://www.smarty.net/docs/en/api.clear.compiled.template.tpl
27 * @param \Smarty $smarty
28 * @param string $resource_name template name
29 * @param string $compile_id compile id
30 * @param integer $exp_time expiration time
32 * @return integer number of template files deleted
34 public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $compile_id = null, $exp_time = null)
36 // clear template objects cache
37 $smarty->_clearTemplateCache();
39 $_compile_dir = $smarty->getCompileDir();
40 if ($_compile_dir == '/') { //We should never want to delete this!
43 $_compile_id = isset($compile_id) ? preg_replace('![^\w]+!', '_', $compile_id) : null;
44 $_dir_sep = $smarty->use_sub_dirs ? $smarty->ds : '^';
45 if (isset($resource_name)) {
46 $_save_stat = $smarty->caching;
47 $smarty->caching = false;
48 /* @var Smarty_Internal_Template $tpl */
49 $tpl = $smarty->createTemplate($resource_name);
50 $smarty->caching = $_save_stat;
51 if (!$tpl->source->handler->uncompiled && !$tpl->source->handler->recompiled && $tpl->source->exists) {
52 $_resource_part_1 = basename(str_replace('^', $smarty->ds, $tpl->compiled->filepath));
53 $_resource_part_1_length = strlen($_resource_part_1);
57 $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1);
58 $_resource_part_2_length = strlen($_resource_part_2);
60 $_dir = $_compile_dir;
61 if ($smarty->use_sub_dirs && isset($_compile_id)) {
62 $_dir .= $_compile_id . $_dir_sep;
64 if (isset($_compile_id)) {
65 $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep;
66 $_compile_id_part_length = strlen($_compile_id_part);
70 $_compileDirs = new RecursiveDirectoryIterator($_dir);
71 // NOTE: UnexpectedValueException thrown for PHP >= 5.3
73 catch (Exception $e) {
76 $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
77 foreach ($_compile as $_file) {
78 if (substr(basename($_file->getPathname()), 0, 1) == '.') {
81 $_filepath = (string) $_file;
82 if ($_file->isDir()) {
83 if (!$_compile->isDot()) {
84 // delete folder if empty
85 @rmdir($_file->getPathname());
88 // delete only php files
89 if (substr($_filepath, -4) !== '.php') {
93 if ((!isset($_compile_id) || (isset($_filepath[ $_compile_id_part_length ]) && $a =
94 !strncmp($_filepath, $_compile_id_part, $_compile_id_part_length))) &&
95 (!isset($resource_name) || (isset($_filepath[ $_resource_part_1_length ]) &&
96 substr_compare($_filepath, $_resource_part_1,
97 - $_resource_part_1_length, $_resource_part_1_length) ==
98 0) || (isset($_filepath[ $_resource_part_2_length ]) &&
99 substr_compare($_filepath, $_resource_part_2,
100 - $_resource_part_2_length,
101 $_resource_part_2_length) == 0))
103 if (isset($exp_time)) {
104 if (time() - @filemtime($_filepath) >= $exp_time) {
112 if ($unlink && @unlink($_filepath)) {
114 if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
115 opcache_invalidate($_filepath, true);