Хранилища Subversion ant

Редакция

Содержимое файла | Последнее изменение | Открыть журнал | RSS

Редакция Автор № строки Строка
304 alex-w 1
<?php
2
 
3
/**
4
* Smarty Internal Plugin CacheResource File
5
*
6
* Implements the file system as resource for the HTML cache
7
* Version ussing nocache inserts
8
*
9
* @package Smarty
10
* @subpackage Cacher
11
* @author Uwe Tews
12
*/
13
 
14
/**
15
* This class does contain all necessary methods for the HTML cache on file system
16
*/
17
class Smarty_Internal_CacheResource_File {
18
    function __construct($smarty)
19
    {
20
        $this->smarty = $smarty;
21
    }
22
    /**
23
    * Returns the filepath of the cached template output
24
    *
25
    * @param object $template current template
26
    * @return string the cache filepath
27
    */
28
    public function getCachedFilepath($template)
29
    {
30
        return $this->buildCachedFilepath ($template->resource_name, $template->cache_id, $template->compile_id);
31
    }
32
 
33
    /**
34
    * Returns the timpestamp of the cached template output
35
    *
36
    * @param object $template current template
37
    * @return integer |booelan the template timestamp or false if the file does not exist
38
    */
39
    public function getCachedTimestamp($template)
40
    {
41
        return ($template->getCachedFilepath() && file_exists($template->getCachedFilepath())) ? filemtime($template->getCachedFilepath()) : false ;
42
    }
43
 
44
    /**
45
    * Returns the cached template output
46
    *
47
    * @param object $template current template
48
    * @return string |booelan the template content or false if the file does not exist
49
    */
50
    public function getCachedContents($template)
51
    {
52
        ob_start();
53
        $_smarty_tpl = $template;
54
        include $template->getCachedFilepath();
55
        return ob_get_clean();
56
    }
57
 
58
    /**
59
    * Writes the rendered template output to cache file
60
    *
61
    * @param object $template current template
62
    * @return boolean status
63
    */
64
    public function writeCachedContent($template, $content)
65
    {
66
        if (!$template->isEvaluated()) {
67
            if (!is_object($this->smarty->write_file_object)) {
68
                require_once(SMARTY_SYSPLUGINS_DIR . 'internal.write_file.php');
69
                // $this->smarty->loadPlugin("Smarty_Internal_Write_File");
70
                $this->smarty->write_file_object = new Smarty_Internal_Write_File;
71
            }
72
            return $this->smarty->write_file_object->writeFile($template->getCachedFilepath(), $content);
73
        } else {
74
            return false;
75
        }
76
    }
77
 
78
    /**
79
    * Empty cache folder
80
    *
81
    * @param integer $exp_time expiration time
82
    * @return integer number of cache files deleted
83
    */
84
    public function clearAll($exp_time = null)
85
    {
86
        return $this->clear(null, null, null, $exp_time);
87
    }
88
    /**
89
    * Empty cache for a specific template
90
    *
91
    * @param string $resource_name template name
92
    * @param string $cache_id cache id
93
    * @param string $compile_id compile id
94
    * @param integer $exp_time expiration time
95
    * @return integer number of cache files deleted
96
    */
97
    public function clear($resource_name, $cache_id, $compile_id, $exp_time)
98
    {
99
        $_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
100
        if (isset($resource_name)) {
101
            $_resource_part = (string)abs(crc32($resource_name)) . '.' . $resource_name . $this->smarty->php_ext;
102
        } else {
103
            $_resource_part = null;
104
        }
105
        $_dir = $this->smarty->cache_dir;
106
        if (strpos('/\\', substr($_dir, -1)) === false) {
107
            $_dir .= DS;
108
        }
109
        if ($this->smarty->use_sub_dirs && isset($cache_id)) {
110
            $_dir .= str_replace('|', $_dir_sep, $cache_id) . $_dir_sep;
111
        }
112
        $_compile_pos = $this->smarty->use_sub_dirs ? 5 : 2;
113
        $_count = 0;
114
        $_cacheDirs = new RecursiveDirectoryIterator($_dir);
115
        $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);
116
        foreach ($_cache as $_file) {
117
            if (strpos($_file, '.svn') !== false) continue;
118
            if ($_file->isDir()) {
119
                if (!$_cache->isDot()) {
120
                    // delete folder if empty
121
                    @rmdir($_file->getPathname());
122
                }
123
            } else {
124
                $_parts = explode($_dir_sep, $_file);
125
                $_parts_count = count($_parts);
126
                $_parts_compile_pos = $_parts_count - $_compile_pos;
127
                if ($_parts_compile_pos < 0) {
128
                    $_parts_compile_pos = 0;
129
                }
130
                if ((substr_compare((string)$_file, $_dir, 0, strlen($_dir)) == 0 &&
131
                            (!isset($resource_name) || $_parts[$_parts_count-1] == $_resource_part) &&
132
                            (!isset($compile_id) || $_parts[$_parts_compile_pos] == $compile_id)) ||
133
                        (isset($resource_name) && (string)$_file == $_dir . $_resource_part)) {
134
                    if (isset($exp_time)) {
135
                        if (time() - @filemtime($_file) >= $exp_time) {
136
                            $_count += unlink((string) $_file) ? 1 : 0;
137
                        }
138
                    } else {
139
                        $_count += unlink((string) $_file) ? 1 : 0;
140
                    }
141
                }
142
            }
143
        }
144
        return $_count;
145
    }
146
    /**
147
    * Get system filepath to cached file
148
    *
149
    * @param string $resource_name template name
150
    * @param string $cache_id cache id
151
    * @param string $compile_id compile id
152
    * @return string filepath of cache file
153
    */
154
    private function buildCachedFilepath ($resource_name, $cache_id, $compile_id)
155
    {
156
        $_files = explode('|', $resource_name);
157
        $_filepath = (string)abs(crc32($resource_name));
158
        // if use_sub_dirs, break file into directories
159
        if ($this->smarty->use_sub_dirs) {
160
            $_filepath = substr($_filepath, 0, 2) . DS
161
             . substr($_filepath, 2, 2) . DS
162
             . substr($_filepath, 4, 2) . DS
163
             . $_filepath;
164
        }
165
        $_compile_dir_sep = $this->smarty->use_sub_dirs ? DS : '^';
166
        if (isset($cache_id)) {
167
            $_cache_id = str_replace('|', $_compile_dir_sep, $cache_id) . $_compile_dir_sep;
168
        } else {
169
            $_cache_id = '';
170
        }
171
        if (isset($compile_id)) {
172
            $_compile_id = $compile_id . $_compile_dir_sep;
173
        } else {
174
            $_compile_id = '';
175
        }
176
        $_cache_dir = $this->smarty->cache_dir;
177
        if (strpos('/\\', substr($_cache_dir, -1)) === false) {
178
            $_cache_dir .= DS;
179
        }
180
 
181
        return $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_files[count($_files)-1]) . $this->smarty->php_ext;
182
    }
183
}
184
 
185
?>