]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_method_clearcompiledtemplate.php
Add Smarty to Composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_method_clearcompiledtemplate.php
1 <?php
2
3 /**
4  * Smarty Method ClearCompiledTemplate
5  *
6  * Smarty::clearCompiledTemplate() method
7  *
8  * @package    Smarty
9  * @subpackage PluginsInternal
10  * @author     Uwe Tews
11  */
12 class Smarty_Internal_Method_ClearCompiledTemplate
13 {
14     /**
15      * Valid for Smarty object
16      *
17      * @var int
18      */
19     public $objMap = 1;
20
21     /**
22      * Delete compiled template file
23      *
24      * @api  Smarty::clearCompiledTemplate()
25      * @link http://www.smarty.net/docs/en/api.clear.compiled.template.tpl
26      *
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
31      *
32      * @return integer number of template files deleted
33      */
34     public function clearCompiledTemplate(Smarty $smarty, $resource_name = null, $compile_id = null, $exp_time = null)
35     {
36         // clear template objects cache
37         $smarty->_clearTemplateCache();
38
39         $_compile_dir = $smarty->getCompileDir();
40         if ($_compile_dir == '/') { //We should never want to delete this!
41             return 0;
42         }
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);
54             } else {
55                 return 0;
56             }
57             $_resource_part_2 = str_replace('.php', '.cache.php', $_resource_part_1);
58             $_resource_part_2_length = strlen($_resource_part_2);
59         }
60         $_dir = $_compile_dir;
61         if ($smarty->use_sub_dirs && isset($_compile_id)) {
62             $_dir .= $_compile_id . $_dir_sep;
63         }
64         if (isset($_compile_id)) {
65             $_compile_id_part = $_compile_dir . $_compile_id . $_dir_sep;
66             $_compile_id_part_length = strlen($_compile_id_part);
67         }
68         $_count = 0;
69         try {
70             $_compileDirs = new RecursiveDirectoryIterator($_dir);
71             // NOTE: UnexpectedValueException thrown for PHP >= 5.3
72         }
73         catch (Exception $e) {
74             return 0;
75         }
76         $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST);
77         foreach ($_compile as $_file) {
78              if (substr(basename($_file->getPathname()), 0, 1) == '.') {
79                 continue;
80             }
81             $_filepath = (string) $_file;
82             if ($_file->isDir()) {
83                 if (!$_compile->isDot()) {
84                     // delete folder if empty
85                     @rmdir($_file->getPathname());
86                 }
87             } else {
88                 // delete only php files
89                 if (substr($_filepath, -4) !== '.php') {
90                     continue;
91                 }
92                 $unlink = false;
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))
102                 ) {
103                     if (isset($exp_time)) {
104                         if (time() - @filemtime($_filepath) >= $exp_time) {
105                             $unlink = true;
106                         }
107                     } else {
108                         $unlink = true;
109                     }
110                 }
111
112                 if ($unlink && @unlink($_filepath)) {
113                     $_count ++;
114                     if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
115                         opcache_invalidate($_filepath, true);
116                     }
117                 }
118             }
119         }
120         return $_count;
121     }
122 }