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