1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: App::uses('AppHelper', 'View/Helper');
18:
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
30: class CacheHelper extends AppHelper {
31:
32: 33: 34: 35: 36: 37:
38: protected $_replace = array();
39:
40: 41: 42: 43: 44: 45:
46: protected $_match = array();
47:
48: 49: 50: 51: 52:
53: protected $_counter = 0;
54:
55: 56: 57: 58: 59:
60: protected function _enabled() {
61: return $this->_View->cacheAction && (Configure::read('Cache.check') === true);
62: }
63:
64: 65: 66: 67: 68: 69: 70:
71: public function afterRenderFile($viewFile, $output) {
72: if ($this->_enabled()) {
73: return $this->_parseContent($viewFile, $output);
74: }
75: }
76:
77: 78: 79: 80: 81: 82:
83: public function afterLayout($layoutFile) {
84: if ($this->_enabled()) {
85: $this->_View->output = $this->cache($layoutFile, $this->_View->output);
86: }
87: $this->_View->output = preg_replace('/<!--\/?nocache-->/', '', $this->_View->output);
88: }
89:
90: 91: 92: 93: 94: 95: 96: 97: 98:
99: protected function _parseContent($file, $out) {
100: $out = preg_replace_callback('/<!--nocache-->/', array($this, '_replaceSection'), $out);
101: $this->_parseFile($file, $out);
102: return $out;
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113:
114: public function cache($file, $out) {
115: $cacheTime = 0;
116: $useCallbacks = false;
117: $cacheAction = $this->_View->cacheAction;
118:
119: if (is_array($cacheAction)) {
120: $keys = array_keys($cacheAction);
121: $index = null;
122:
123: foreach ($keys as $action) {
124: if ($action === $this->request->params['action']) {
125: $index = $action;
126: break;
127: }
128: }
129:
130: if (!isset($index) && $this->request->params['action'] === 'index') {
131: $index = 'index';
132: }
133:
134: $options = $cacheAction;
135: if (isset($cacheAction[$index])) {
136: if (is_array($cacheAction[$index])) {
137: $options = $cacheAction[$index] + array('duration' => 0, 'callbacks' => false);
138: } else {
139: $cacheTime = $cacheAction[$index];
140: }
141: }
142: if (isset($options['duration'])) {
143: $cacheTime = $options['duration'];
144: }
145: if (isset($options['callbacks'])) {
146: $useCallbacks = $options['callbacks'];
147: }
148: } else {
149: $cacheTime = $cacheAction;
150: }
151:
152: if ($cacheTime && $cacheTime > 0) {
153: $cached = $this->_parseOutput($out);
154: try {
155: $this->_writeFile($cached, $cacheTime, $useCallbacks);
156: } catch (Exception $e) {
157: if (Configure::read('debug')) {
158: throw $e;
159: }
160:
161: $message = __d(
162: 'cake_dev',
163: 'Unable to write view cache file: "%s" for "%s"',
164: $e->getMessage(),
165: $this->request->here
166: );
167: $this->log($message, 'error');
168: }
169: $out = $this->_stripTags($out);
170: }
171: return $out;
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: protected function _parseFile($file, $cache) {
182: if (is_file($file)) {
183: $file = file_get_contents($file);
184: } elseif ($file = fileExistsInPath($file)) {
185: $file = file_get_contents($file);
186: }
187: preg_match_all('/(<!--nocache:\d{3}-->(?<=<!--nocache:\d{3}-->)[\\s\\S]*?(?=<!--\/nocache-->)<!--\/nocache-->)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
188: preg_match_all('/(?<=<!--nocache-->)([\\s\\S]*?)(?=<!--\/nocache-->)/i', $file, $fileResult, PREG_PATTERN_ORDER);
189: $fileResult = $fileResult[0];
190: $outputResult = $outputResult[0];
191:
192: if (!empty($this->_replace)) {
193: foreach ($outputResult as $i => $element) {
194: $index = array_search($element, $this->_match);
195: if ($index !== false) {
196: unset($outputResult[$i]);
197: }
198: }
199: $outputResult = array_values($outputResult);
200: }
201:
202: if (!empty($fileResult)) {
203: $i = 0;
204: foreach ($fileResult as $cacheBlock) {
205: if (isset($outputResult[$i])) {
206: $this->_replace[] = $cacheBlock;
207: $this->_match[] = $outputResult[$i];
208: }
209: $i++;
210: }
211: }
212: }
213:
214: 215: 216: 217: 218: 219:
220: protected function _replaceSection() {
221: $this->_counter += 1;
222: return sprintf('<!--nocache:%03d-->', $this->_counter);
223: }
224:
225: 226: 227: 228: 229: 230: 231: 232:
233: protected function _stripTags($content) {
234: return preg_replace('#<!--/?nocache(\:\d{3})?-->#', '', $content);
235: }
236:
237: 238: 239: 240: 241: 242:
243: protected function _parseOutput($cache) {
244: $count = 0;
245: if (!empty($this->_match)) {
246: foreach ($this->_match as $found) {
247: $original = $cache;
248: $length = strlen($found);
249: $position = 0;
250:
251: for ($i = 1; $i <= 1; $i++) {
252: $position = strpos($cache, $found, $position);
253:
254: if ($position !== false) {
255: $cache = substr($original, 0, $position);
256: $cache .= $this->_replace[$count];
257: $cache .= substr($original, $position + $length);
258: } else {
259: break;
260: }
261: }
262: $count++;
263: }
264: return $cache;
265: }
266: return $cache;
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276: 277:
278: protected function _writeFile($content, $timestamp, $useCallbacks = false) {
279: $now = time();
280:
281: if (is_numeric($timestamp)) {
282: $cacheTime = $now + $timestamp;
283: } else {
284: $cacheTime = strtotime($timestamp, $now);
285: }
286: $path = $this->request->here();
287: if ($path === '/') {
288: $path = 'home';
289: }
290: $prefix = Configure::read('Cache.viewPrefix');
291: if ($prefix) {
292: $path = $prefix . '_' . $path;
293: }
294: $cache = strtolower(Inflector::slug($path));
295:
296: if (empty($cache)) {
297: return null;
298: }
299: $cache = $cache . '.php';
300: $file = '<!--cachetime:' . $cacheTime . '--><?php';
301:
302: if (empty($this->_View->plugin)) {
303: $file .= "
304: App::uses('{$this->_View->name}Controller', 'Controller');
305: ";
306: } else {
307: $file .= "
308: App::uses('{$this->_View->plugin}AppController', '{$this->_View->plugin}.Controller');
309: App::uses('{$this->_View->name}Controller', '{$this->_View->plugin}.Controller');
310: ";
311: }
312:
313: $file .= '
314: $request = unserialize(base64_decode(\'' . base64_encode(serialize($this->request)) . '\'));
315: $response->type(\'' . $this->_View->response->type() . '\');
316: $controller = new ' . $this->_View->name . 'Controller($request, $response);
317: $controller->plugin = $this->plugin = \'' . $this->_View->plugin . '\';
318: $controller->helpers = $this->helpers = unserialize(base64_decode(\'' . base64_encode(serialize($this->_View->helpers)) . '\'));
319: $controller->layout = $this->layout = \'' . $this->_View->layout . '\';
320: $controller->theme = $this->theme = \'' . $this->_View->theme . '\';
321: $controller->viewVars = unserialize(base64_decode(\'' . base64_encode(serialize($this->_View->viewVars)) . '\'));
322: Router::setRequestInfo($controller->request);
323: $this->request = $request;';
324:
325: if ($useCallbacks) {
326: $file .= '
327: $controller->constructClasses();
328: $controller->startupProcess();';
329: }
330:
331: $file .= '
332: $this->viewVars = $controller->viewVars;
333: $this->loadHelpers();
334: extract($this->viewVars, EXTR_SKIP);
335: ?>';
336: $content = preg_replace("/(<\\?xml)/", "<?php echo '$1'; ?>", $content);
337: $file .= $content;
338: return cache('views' . DS . $cache, $file, $timestamp);
339: }
340:
341: }
342: