1 <?php
3 /**
4 * Smarty method Clear_Compiled_Tpl
5 *
6 * Deletes compiled template files
7 *
8 * @package Smarty
9 * @subpackage SmartyMethod
10 * @author Uwe Tews
11 */
13 /**
14 * Delete compiled template file
15 *
16 * @param string $resource_name template name
17 * @param string $compile_id compile id
18 * @param integer $exp_time expiration time
19 * @return integer number of template files deleted
20 */
21 function Smarty_Method_Clear_Compiled_Tpl($smarty, $resource_name = null, $compile_id = null, $exp_time = null)
22 {
23 $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!','_',$compile_id) : null;
24 $_dir_sep = $smarty->use_sub_dirs ? DS : '^';
25 if (isset($resource_name)) {
26 $_resource_part_1 = $resource_name . '.php';
27 $_resource_part_2 = $resource_name . '.cache' . '.php';
28 } else {
29 $_resource_part = '';
30 }
31 $_dir = $smarty->compile_dir;
32 if ($smarty->use_sub_dirs && isset($_compile_id)) {
33 $_dir .= $_compile_id . $_dir_sep;
34 }
35 if (isset($_compile_id)) {
36 $_compile_id_part = $smarty->compile_dir . $_compile_id . $_dir_sep;
37 }
38 $_count = 0;
39 $_compileDirs = new RecursiveDirectoryIterator($_dir);
40 $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
41 foreach ($_compile as $_file) {
42 if (strpos($_file, '.svn') !== false) continue;
43 if ($_file->isDir()) {
44 if (!$_compile->isDot()) {
45 // delete folder if empty
46 @rmdir($_file->getPathname());
47 }
48 } else {
49 if ((!isset($_compile_id) || (strlen((string)$_file) > strlen($_compile_id_part) && substr_compare((string)$_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0)) &&
50 (!isset($resource_name) || (strlen((string)$_file) > strlen($_resource_part_1) && substr_compare((string)$_file, $_resource_part_1, - strlen($_resource_part_1), strlen($_resource_part_1)) == 0) ||
51 (strlen((string)$_file) > strlen($_resource_part_2) && substr_compare((string)$_file, $_resource_part_2, - strlen($_resource_part_2), strlen($_resource_part_2)) == 0))) {
52 if (isset($exp_time)) {
53 if (time() - @filemtime($_file) >= $exp_time) {
54 $_count += @unlink((string) $_file) ? 1 : 0;
55 }
56 } else {
57 $_count += @unlink((string) $_file) ? 1 : 0;
58 }
59 }
60 }
61 }
62 return $_count;
63 }
65 ?>