Mercurial > hg > Members > shoshi > webvirt
comparison cake/libs/view/helpers/cache.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * CacheHelper helps create full page view caching. | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The MIT License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://cakephp.org CakePHP(tm) Project | |
15 * @package cake | |
16 * @subpackage cake.cake.libs.view.helpers | |
17 * @since CakePHP(tm) v 1.0.0.2277 | |
18 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
19 */ | |
20 | |
21 /** | |
22 * CacheHelper helps create full page view caching. | |
23 * | |
24 * When using CacheHelper you don't call any of its methods, they are all automatically | |
25 * called by View, and use the $cacheAction settings set in the controller. | |
26 * | |
27 * @package cake | |
28 * @subpackage cake.cake.libs.view.helpers | |
29 * @link http://book.cakephp.org/view/1376/Cache | |
30 */ | |
31 class CacheHelper extends AppHelper { | |
32 | |
33 /** | |
34 * Array of strings replaced in cached views. | |
35 * The strings are found between <cake:nocache><cake:nocache> in views | |
36 * | |
37 * @var array | |
38 * @access private | |
39 */ | |
40 var $__replace = array(); | |
41 | |
42 /** | |
43 * Array of string that are replace with there var replace above. | |
44 * The strings are any content inside <cake:nocache><cake:nocache> and includes the tags in views | |
45 * | |
46 * @var array | |
47 * @access private | |
48 */ | |
49 var $__match = array(); | |
50 | |
51 /** | |
52 * cache action time | |
53 * | |
54 * @var object | |
55 * @access public | |
56 */ | |
57 var $cacheAction; | |
58 | |
59 /** | |
60 * Main method used to cache a view | |
61 * | |
62 * @param string $file File to cache | |
63 * @param string $out output to cache | |
64 * @param boolean $cache Whether or not a cache file should be written. | |
65 * @return string view ouput | |
66 */ | |
67 function cache($file, $out, $cache = false) { | |
68 $cacheTime = 0; | |
69 $useCallbacks = false; | |
70 if (is_array($this->cacheAction)) { | |
71 $keys = array_keys($this->cacheAction); | |
72 $index = null; | |
73 | |
74 foreach ($keys as $action) { | |
75 if ($action == $this->params['action']) { | |
76 $index = $action; | |
77 break; | |
78 } | |
79 } | |
80 | |
81 if (!isset($index) && $this->action == 'index') { | |
82 $index = 'index'; | |
83 } | |
84 | |
85 $options = $this->cacheAction; | |
86 if (isset($this->cacheAction[$index])) { | |
87 if (is_array($this->cacheAction[$index])) { | |
88 $options = array_merge(array('duration' => 0, 'callbacks' => false), $this->cacheAction[$index]); | |
89 } else { | |
90 $cacheTime = $this->cacheAction[$index]; | |
91 } | |
92 } | |
93 if (isset($options['duration'])) { | |
94 $cacheTime = $options['duration']; | |
95 } | |
96 if (isset($options['callbacks'])) { | |
97 $useCallbacks = $options['callbacks']; | |
98 } | |
99 } else { | |
100 $cacheTime = $this->cacheAction; | |
101 } | |
102 | |
103 if ($cacheTime != '' && $cacheTime > 0) { | |
104 $this->__parseFile($file, $out); | |
105 if ($cache === true) { | |
106 $cached = $this->__parseOutput($out); | |
107 $this->__writeFile($cached, $cacheTime, $useCallbacks); | |
108 } | |
109 return $out; | |
110 } else { | |
111 return $out; | |
112 } | |
113 } | |
114 | |
115 /** | |
116 * Parse file searching for no cache tags | |
117 * | |
118 * @param string $file The filename that needs to be parsed. | |
119 * @param string $cache The cached content | |
120 * @access private | |
121 */ | |
122 function __parseFile($file, $cache) { | |
123 if (is_file($file)) { | |
124 $file = file_get_contents($file); | |
125 } elseif ($file = fileExistsInPath($file)) { | |
126 $file = file_get_contents($file); | |
127 } | |
128 preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER); | |
129 preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER); | |
130 $fileResult = $fileResult[0]; | |
131 $outputResult = $outputResult[0]; | |
132 | |
133 if (!empty($this->__replace)) { | |
134 foreach ($outputResult as $i => $element) { | |
135 $index = array_search($element, $this->__match); | |
136 if ($index !== false) { | |
137 unset($outputResult[$i]); | |
138 } | |
139 } | |
140 $outputResult = array_values($outputResult); | |
141 } | |
142 | |
143 if (!empty($fileResult)) { | |
144 $i = 0; | |
145 foreach ($fileResult as $cacheBlock) { | |
146 if (isset($outputResult[$i])) { | |
147 $this->__replace[] = $cacheBlock; | |
148 $this->__match[] = $outputResult[$i]; | |
149 } | |
150 $i++; | |
151 } | |
152 } | |
153 } | |
154 | |
155 /** | |
156 * Parse the output and replace cache tags | |
157 * | |
158 * @param string $cache Output to replace content in. | |
159 * @return string with all replacements made to <cake:nocache><cake:nocache> | |
160 * @access private | |
161 */ | |
162 function __parseOutput($cache) { | |
163 $count = 0; | |
164 if (!empty($this->__match)) { | |
165 foreach ($this->__match as $found) { | |
166 $original = $cache; | |
167 $length = strlen($found); | |
168 $position = 0; | |
169 | |
170 for ($i = 1; $i <= 1; $i++) { | |
171 $position = strpos($cache, $found, $position); | |
172 | |
173 if ($position !== false) { | |
174 $cache = substr($original, 0, $position); | |
175 $cache .= $this->__replace[$count]; | |
176 $cache .= substr($original, $position + $length); | |
177 } else { | |
178 break; | |
179 } | |
180 } | |
181 $count++; | |
182 } | |
183 return $cache; | |
184 } | |
185 return $cache; | |
186 } | |
187 | |
188 /** | |
189 * Write a cached version of the file | |
190 * | |
191 * @param string $content view content to write to a cache file. | |
192 * @param sting $timestamp Duration to set for cache file. | |
193 * @return boolean success of caching view. | |
194 * @access private | |
195 */ | |
196 function __writeFile($content, $timestamp, $useCallbacks = false) { | |
197 $now = time(); | |
198 | |
199 if (is_numeric($timestamp)) { | |
200 $cacheTime = $now + $timestamp; | |
201 } else { | |
202 $cacheTime = strtotime($timestamp, $now); | |
203 } | |
204 $path = $this->here; | |
205 if ($this->here == '/') { | |
206 $path = 'home'; | |
207 } | |
208 $cache = strtolower(Inflector::slug($path)); | |
209 | |
210 if (empty($cache)) { | |
211 return; | |
212 } | |
213 $cache = $cache . '.php'; | |
214 $file = '<!--cachetime:' . $cacheTime . '--><?php'; | |
215 | |
216 if (empty($this->plugin)) { | |
217 $file .= ' | |
218 App::import(\'Controller\', \'' . $this->controllerName. '\'); | |
219 '; | |
220 } else { | |
221 $file .= ' | |
222 App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\'); | |
223 '; | |
224 } | |
225 | |
226 $file .= '$controller =& new ' . $this->controllerName . 'Controller(); | |
227 $controller->plugin = $this->plugin = \''.$this->plugin.'\'; | |
228 $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\'); | |
229 $controller->base = $this->base = \'' . $this->base . '\'; | |
230 $controller->layout = $this->layout = \'' . $this->layout. '\'; | |
231 $controller->webroot = $this->webroot = \'' . $this->webroot . '\'; | |
232 $controller->here = $this->here = \'' . $this->here . '\'; | |
233 $controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\')); | |
234 $controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\'); | |
235 $controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\')); | |
236 $controller->viewVars = $this->viewVars = unserialize(base64_decode(\'' . base64_encode(serialize($this->viewVars)) . '\')); | |
237 $controller->theme = $this->theme = \'' . $this->theme . '\'; | |
238 Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));'; | |
239 | |
240 if ($useCallbacks == true) { | |
241 $file .= ' | |
242 $controller->constructClasses(); | |
243 $controller->Component->initialize($controller); | |
244 $controller->beforeFilter(); | |
245 $controller->Component->startup($controller);'; | |
246 } | |
247 | |
248 $file .= ' | |
249 $loadedHelpers = array(); | |
250 $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers); | |
251 foreach (array_keys($loadedHelpers) as $helper) { | |
252 $camelBackedHelper = Inflector::variable($helper); | |
253 ${$camelBackedHelper} =& $loadedHelpers[$helper]; | |
254 $this->loaded[$camelBackedHelper] =& ${$camelBackedHelper}; | |
255 $this->{$helper} =& $loadedHelpers[$helper]; | |
256 } | |
257 extract($this->viewVars, EXTR_SKIP); | |
258 ?>'; | |
259 $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content); | |
260 $file .= $content; | |
261 return cache('views' . DS . $cache, $file, $timestamp); | |
262 } | |
263 } |