1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: |
|
10: |
|
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: |
|
18: | class Smarty_Internal_CacheResource_File extends Smarty_CacheResource
|
19: | {
|
20: | |
21: | |
22: | |
23: | |
24: | |
25: | |
26: | |
27: |
|
28: | public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
|
29: | {
|
30: | $source = &$_template->source;
|
31: | $smarty = &$_template->smarty;
|
32: | $_compile_dir_sep = $smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^';
|
33: | $_filepath = sha1($source->uid . $smarty->_joined_template_dir);
|
34: | $cached->filepath = $smarty->getCacheDir();
|
35: | if (isset($_template->cache_id)) {
|
36: | $cached->filepath .= preg_replace(
|
37: | array(
|
38: | '![^\w|]+!',
|
39: | '![|]+!'
|
40: | ),
|
41: | array(
|
42: | '_',
|
43: | $_compile_dir_sep
|
44: | ),
|
45: | $_template->cache_id
|
46: | ) . $_compile_dir_sep;
|
47: | }
|
48: | if (isset($_template->compile_id)) {
|
49: | $cached->filepath .= preg_replace('![^\w]+!', '_', $_template->compile_id) . $_compile_dir_sep;
|
50: | }
|
51: |
|
52: | if ($smarty->use_sub_dirs) {
|
53: | $cached->filepath .= $_filepath[ 0 ] . $_filepath[ 1 ] . DIRECTORY_SEPARATOR . $_filepath[ 2 ] .
|
54: | $_filepath[ 3 ] .
|
55: | DIRECTORY_SEPARATOR .
|
56: | $_filepath[ 4 ] . $_filepath[ 5 ] . DIRECTORY_SEPARATOR;
|
57: | }
|
58: | $cached->filepath .= $_filepath;
|
59: | $basename = $source->handler->getBasename($source);
|
60: | if (!empty($basename)) {
|
61: | $cached->filepath .= '.' . $basename;
|
62: | }
|
63: | if ($smarty->cache_locking) {
|
64: | $cached->lock_id = $cached->filepath . '.lock';
|
65: | }
|
66: | $cached->filepath .= '.php';
|
67: | $cached->timestamp = $cached->exists = is_file($cached->filepath);
|
68: | if ($cached->exists) {
|
69: | $cached->timestamp = filemtime($cached->filepath);
|
70: | }
|
71: | }
|
72: |
|
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: |
|
80: | public function populateTimestamp(Smarty_Template_Cached $cached)
|
81: | {
|
82: | $cached->timestamp = $cached->exists = is_file($cached->filepath);
|
83: | if ($cached->exists) {
|
84: | $cached->timestamp = filemtime($cached->filepath);
|
85: | }
|
86: | }
|
87: |
|
88: | |
89: | |
90: | |
91: | |
92: | |
93: | |
94: | |
95: | |
96: |
|
97: | public function process(
|
98: | Smarty_Internal_Template $_smarty_tpl,
|
99: | Smarty_Template_Cached $cached = null,
|
100: | $update = false
|
101: | ) {
|
102: | $_smarty_tpl->cached->valid = false;
|
103: | if ($update && defined('HHVM_VERSION')) {
|
104: | eval('?>' . file_get_contents($_smarty_tpl->cached->filepath));
|
105: | return true;
|
106: | } else {
|
107: | return @include $_smarty_tpl->cached->filepath;
|
108: | }
|
109: | }
|
110: |
|
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: |
|
120: | public function writeCachedContent(Smarty_Internal_Template $_template, $content)
|
121: | {
|
122: | if ($_template->smarty->ext->_writeFile->writeFile(
|
123: | $_template->cached->filepath,
|
124: | $content,
|
125: | $_template->smarty
|
126: | ) === true
|
127: | ) {
|
128: | if (function_exists('opcache_invalidate')
|
129: | && (!function_exists('ini_get') || strlen(ini_get('opcache.restrict_api'))) < 1
|
130: | ) {
|
131: | opcache_invalidate($_template->cached->filepath, true);
|
132: | } elseif (function_exists('apc_compile_file')) {
|
133: | apc_compile_file($_template->cached->filepath);
|
134: | }
|
135: | $cached = $_template->cached;
|
136: | $cached->timestamp = $cached->exists = is_file($cached->filepath);
|
137: | if ($cached->exists) {
|
138: | $cached->timestamp = filemtime($cached->filepath);
|
139: | return true;
|
140: | }
|
141: | }
|
142: | return false;
|
143: | }
|
144: |
|
145: | |
146: | |
147: | |
148: | |
149: | |
150: | |
151: |
|
152: | public function readCachedContent(Smarty_Internal_Template $_template)
|
153: | {
|
154: | if (is_file($_template->cached->filepath)) {
|
155: | return file_get_contents($_template->cached->filepath);
|
156: | }
|
157: | return false;
|
158: | }
|
159: |
|
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: |
|
168: | public function clearAll(Smarty $smarty, $exp_time = null)
|
169: | {
|
170: | return $smarty->ext->_cacheResourceFile->clear($smarty, null, null, null, $exp_time);
|
171: | }
|
172: |
|
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: |
|
184: | public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
|
185: | {
|
186: | return $smarty->ext->_cacheResourceFile->clear($smarty, $resource_name, $cache_id, $compile_id, $exp_time);
|
187: | }
|
188: |
|
189: | |
190: | |
191: | |
192: | |
193: | |
194: | |
195: | |
196: |
|
197: | public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
198: | {
|
199: | if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
|
200: | clearstatcache(true, $cached->lock_id);
|
201: | } else {
|
202: | clearstatcache();
|
203: | }
|
204: | if (is_file($cached->lock_id)) {
|
205: | $t = filemtime($cached->lock_id);
|
206: | return $t && (time() - $t < $smarty->locking_timeout);
|
207: | } else {
|
208: | return false;
|
209: | }
|
210: | }
|
211: |
|
212: | |
213: | |
214: | |
215: | |
216: | |
217: | |
218: | |
219: |
|
220: | public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
221: | {
|
222: | $cached->is_locked = true;
|
223: | touch($cached->lock_id);
|
224: | }
|
225: |
|
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | |
233: |
|
234: | public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
235: | {
|
236: | $cached->is_locked = false;
|
237: | @unlink($cached->lock_id);
|
238: | }
|
239: | }
|
240: | |