Редакция 2 | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 2 | Редакция 87 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | /**
|
2 | /**
|
3 | * Smarty plugin
|
3 | * Smarty plugin
|
4 | * @package Smarty
|
4 | * @package Smarty
|
5 | * @subpackage plugins
|
5 | * @subpackage plugins
|
6 | */
|
6 | */
|
7 | 7 | ||
8 | /**
|
8 | /**
|
9 | * read a cache file, determine if it needs to be
|
9 | * read a cache file, determine if it needs to be
|
10 | * regenerated or not
|
10 | * regenerated or not
|
11 | *
|
11 | *
|
12 | * @param string $tpl_file
|
12 | * @param string $tpl_file
|
13 | * @param string $cache_id
|
13 | * @param string $cache_id
|
14 | * @param string $compile_id
|
14 | * @param string $compile_id
|
15 | * @param string $results
|
15 | * @param string $results
|
16 | * @return boolean
|
16 | * @return boolean
|
17 | */
|
17 | */
|
18 | 18 | ||
19 | // $tpl_file, $cache_id, $compile_id, &$results
|
19 | // $tpl_file, $cache_id, $compile_id, &$results
|
20 | 20 | ||
21 | function smarty_core_read_cache_file(&$params, &$smarty) |
21 | function smarty_core_read_cache_file(&$params, &$smarty) |
22 | {
|
22 | {
|
23 | static $content_cache = array(); |
23 | static $content_cache = array(); |
24 | 24 | ||
25 | if ($smarty->force_compile) { |
25 | if ($smarty->force_compile) { |
26 | // force compile enabled, always regenerate
|
26 | // force compile enabled, always regenerate
|
27 | return false; |
27 | return false; |
28 | }
|
28 | }
|
29 | 29 | ||
30 | if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) { |
30 | if (isset($content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']])) { |
31 | list($params['results'], $smarty->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']]; |
31 | list($params['results'], $smarty->_cache_info) = $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']]; |
32 | return true; |
32 | return true; |
33 | }
|
33 | }
|
34 | 34 | ||
35 | if (!empty($smarty->cache_handler_func)) { |
35 | if (!empty($smarty->cache_handler_func)) { |
36 | // use cache_handler function
|
36 | // use cache_handler function
|
37 | call_user_func_array($smarty->cache_handler_func, |
37 | call_user_func_array($smarty->cache_handler_func, |
38 | array('read', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null)); |
38 | array('read', &$smarty, &$params['results'], $params['tpl_file'], $params['cache_id'], $params['compile_id'], null)); |
39 | } else { |
39 | } else { |
40 | // use local cache file
|
40 | // use local cache file
|
41 | $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']); |
41 | $_auto_id = $smarty->_get_auto_id($params['cache_id'], $params['compile_id']); |
42 | $_cache_file = $smarty->_get_auto_filename($smarty->cache_dir, $params['tpl_file'], $_auto_id); |
42 | $_cache_file = $smarty->_get_auto_filename($smarty->cache_dir, $params['tpl_file'], $_auto_id); |
43 | $params['results'] = $smarty->_read_file($_cache_file); |
43 | $params['results'] = $smarty->_read_file($_cache_file); |
44 | }
|
44 | }
|
45 | 45 | ||
46 | if (empty($params['results'])) { |
46 | if (empty($params['results'])) { |
47 | // nothing to parse (error?), regenerate cache
|
47 | // nothing to parse (error?), regenerate cache
|
48 | return false; |
48 | return false; |
49 | }
|
49 | }
|
50 | 50 | ||
51 | $_contents = $params['results']; |
51 | $_contents = $params['results']; |
52 | $_info_start = strpos($_contents, "\n") + 1; |
52 | $_info_start = strpos($_contents, "\n") + 1; |
53 | $_info_len = (int)substr($_contents, 0, $_info_start - 1); |
53 | $_info_len = (int)substr($_contents, 0, $_info_start - 1); |
54 | $_cache_info = unserialize(substr($_contents, $_info_start, $_info_len)); |
54 | $_cache_info = unserialize(substr($_contents, $_info_start, $_info_len)); |
55 | $params['results'] = substr($_contents, $_info_start + $_info_len); |
55 | $params['results'] = substr($_contents, $_info_start + $_info_len); |
56 | 56 | ||
57 | if ($smarty->caching == 2 && isset ($_cache_info['expires'])){ |
57 | if ($smarty->caching == 2 && isset ($_cache_info['expires'])){ |
58 | // caching by expiration time
|
58 | // caching by expiration time
|
59 | if ($_cache_info['expires'] > -1 && (time() > $_cache_info['expires'])) { |
59 | if ($_cache_info['expires'] > -1 && (time() > $_cache_info['expires'])) { |
60 | // cache expired, regenerate
|
60 | // cache expired, regenerate
|
61 | return false; |
61 | return false; |
62 | }
|
62 | }
|
63 | } else { |
63 | } else { |
64 | // caching by lifetime
|
64 | // caching by lifetime
|
65 | if ($smarty->cache_lifetime > -1 && (time() - $_cache_info['timestamp'] > $smarty->cache_lifetime)) { |
65 | if ($smarty->cache_lifetime > -1 && (time() - $_cache_info['timestamp'] > $smarty->cache_lifetime)) { |
66 | // cache expired, regenerate
|
66 | // cache expired, regenerate
|
67 | return false; |
67 | return false; |
68 | }
|
68 | }
|
69 | }
|
69 | }
|
70 | 70 | ||
71 | if ($smarty->compile_check) { |
71 | if ($smarty->compile_check) { |
72 | $_params = array('get_source' => false, 'quiet'=>true); |
72 | $_params = array('get_source' => false, 'quiet'=>true); |
73 | foreach (array_keys($_cache_info['template']) as $_template_dep) { |
73 | foreach (array_keys($_cache_info['template']) as $_template_dep) { |
74 | $_params['resource_name'] = $_template_dep; |
74 | $_params['resource_name'] = $_template_dep; |
75 | if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) { |
75 | if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) { |
76 | // template file has changed, regenerate cache
|
76 | // template file has changed, regenerate cache
|
77 | return false; |
77 | return false; |
78 | }
|
78 | }
|
79 | }
|
79 | }
|
80 | 80 | ||
81 | if (isset($_cache_info['config'])) { |
81 | if (isset($_cache_info['config'])) { |
82 | $_params = array('resource_base_path' => $smarty->config_dir, 'get_source' => false, 'quiet'=>true); |
82 | $_params = array('resource_base_path' => $smarty->config_dir, 'get_source' => false, 'quiet'=>true); |
83 | foreach (array_keys($_cache_info['config']) as $_config_dep) { |
83 | foreach (array_keys($_cache_info['config']) as $_config_dep) { |
84 | $_params['resource_name'] = $_config_dep; |
84 | $_params['resource_name'] = $_config_dep; |
85 | if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) { |
85 | if (!$smarty->_fetch_resource_info($_params) || $_cache_info['timestamp'] < $_params['resource_timestamp']) { |
86 | // config file has changed, regenerate cache
|
86 | // config file has changed, regenerate cache
|
87 | return false; |
87 | return false; |
88 | }
|
88 | }
|
89 | }
|
89 | }
|
90 | }
|
90 | }
|
91 | }
|
91 | }
|
92 | 92 | ||
93 | $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $_cache_info); |
93 | $content_cache[$params['tpl_file'].','.$params['cache_id'].','.$params['compile_id']] = array($params['results'], $_cache_info); |
94 | 94 | ||
95 | $smarty->_cache_info = $_cache_info; |
95 | $smarty->_cache_info = $_cache_info; |
96 | return true; |
96 | return true; |
97 | }
|
97 | }
|
98 | 98 | ||
99 | /* vim: set expandtab: */
|
99 | /* vim: set expandtab: */
|
100 | 100 | ||
101 | ?>
|
101 | ?>
|
102 | 102 |