1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10: * @link http://cakephp.org CakePHP(tm) Project
11: * @since CakePHP(tm) v 2.2
12: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
13: */
14:
15: App::uses('DispatcherFilter', 'Routing');
16:
17: /**
18: * This filter will check wheter the response was previously cached in the file system
19: * and served it back to the client if appropriate.
20: *
21: * @package Cake.Routing.Filter
22: */
23: class CacheDispatcher extends DispatcherFilter {
24:
25: /**
26: * Default priority for all methods in this filter
27: * This filter should run before the request gets parsed by router
28: *
29: * @var int
30: */
31: public $priority = 9;
32:
33: /**
34: * Checks whether the response was cached and set the body accordingly.
35: *
36: * @param CakeEvent $event containing the request and response object
37: * @return CakeResponse with cached content if found, null otherwise
38: */
39: public function beforeDispatch($event) {
40: if (Configure::read('Cache.check') !== true) {
41: return;
42: }
43:
44: $path = $event->data['request']->here();
45: if ($path == '/') {
46: $path = 'home';
47: }
48: $path = strtolower(Inflector::slug($path));
49:
50: $filename = CACHE . 'views' . DS . $path . '.php';
51:
52: if (!file_exists($filename)) {
53: $filename = CACHE . 'views' . DS . $path . '_index.php';
54: }
55: if (file_exists($filename)) {
56: $controller = null;
57: $view = new View($controller);
58: $result = $view->renderCache($filename, microtime(true));
59: if ($result !== false) {
60: $event->stopPropagation();
61: $event->data['response']->body($result);
62: return $event->data['response'];
63: }
64: }
65: }
66:
67: }
68: