CacheHelper.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * CacheHelper helps create full page view caching.
  4. *
  5. * PHP 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. * CacheHelper helps create full page view caching.
  22. *
  23. * When using CacheHelper you don't call any of its methods, they are all automatically
  24. * called by View, and use the $cacheAction settings set in the controller.
  25. *
  26. * @package cake
  27. * @subpackage cake.cake.libs.view.helpers
  28. * @link http://book.cakephp.org/view/1376/Cache
  29. */
  30. class CacheHelper extends AppHelper {
  31. /**
  32. * Array of strings replaced in cached views.
  33. * The strings are found between `<!--nocache--><!--/nocache-->` in views
  34. *
  35. * @var array
  36. */
  37. protected $_replace = array();
  38. /**
  39. * Array of string that are replace with there var replace above.
  40. * The strings are any content inside `<!--nocache--><!--/nocache-->` and includes the tags in views
  41. *
  42. * @var array
  43. */
  44. protected $_match = array();
  45. /**
  46. * Parses the view file and stores content for cache file building.
  47. *
  48. * @return void
  49. */
  50. public function afterRender($viewFile) {
  51. $caching = (($this->_View->cacheAction != false)) && (Configure::read('Cache.check') === true);
  52. if ($caching) {
  53. $this->cache($viewFile, $this->_View->output, false);
  54. }
  55. }
  56. /**
  57. * Parses the layout file and stores content for cache file building.
  58. *
  59. * @return void
  60. */
  61. public function afterLayout($layoutFile) {
  62. $caching = (($this->_View->cacheAction != false)) && (Configure::read('Cache.check') === true);
  63. if ($caching) {
  64. $this->cache($layoutFile, $this->_View->output, true);
  65. }
  66. $this->_View->output = preg_replace('/<!--\/?nocache-->/', '', $this->_View->output);
  67. }
  68. /**
  69. * Main method used to cache a view
  70. *
  71. * @param string $file File to cache
  72. * @param string $out output to cache
  73. * @param boolean $cache Whether or not a cache file should be written.
  74. * @return string view ouput
  75. */
  76. public function cache($file, $out, $cache = false) {
  77. $cacheTime = 0;
  78. $useCallbacks = false;
  79. $cacheAction = $this->_View->cacheAction;
  80. if (is_array($cacheAction)) {
  81. $keys = array_keys($cacheAction);
  82. $index = null;
  83. foreach ($keys as $action) {
  84. if ($action == $this->request->params['action']) {
  85. $index = $action;
  86. break;
  87. }
  88. }
  89. if (!isset($index) && $this->request->params['action'] == 'index') {
  90. $index = 'index';
  91. }
  92. $options = $cacheAction;
  93. if (isset($cacheAction[$index])) {
  94. if (is_array($cacheAction[$index])) {
  95. $options = array_merge(array('duration' => 0, 'callbacks' => false), $cacheAction[$index]);
  96. } else {
  97. $cacheTime = $cacheAction[$index];
  98. }
  99. }
  100. if (isset($options['duration'])) {
  101. $cacheTime = $options['duration'];
  102. }
  103. if (isset($options['callbacks'])) {
  104. $useCallbacks = $options['callbacks'];
  105. }
  106. } else {
  107. $cacheTime = $cacheAction;
  108. }
  109. if ($cacheTime != '' && $cacheTime > 0) {
  110. $this->_parseFile($file, $out);
  111. if ($cache === true) {
  112. $cached = $this->_parseOutput($out);
  113. $this->_writeFile($cached, $cacheTime, $useCallbacks);
  114. }
  115. return $out;
  116. } else {
  117. return $out;
  118. }
  119. }
  120. /**
  121. * Parse file searching for no cache tags
  122. *
  123. * @param string $file The filename that needs to be parsed.
  124. * @param string $cache The cached content
  125. */
  126. protected function _parseFile($file, $cache) {
  127. if (is_file($file)) {
  128. $file = file_get_contents($file);
  129. } elseif ($file = fileExistsInPath($file)) {
  130. $file = file_get_contents($file);
  131. }
  132. preg_match_all('/(<!--nocache-->(?<=<!--nocache-->)[\\s\\S]*?(?=<!--\/nocache-->)<!--\/nocache-->)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
  133. preg_match_all('/(?<=<!--nocache-->)([\\s\\S]*?)(?=<!--\/nocache-->)/i', $file, $fileResult, PREG_PATTERN_ORDER);
  134. $fileResult = $fileResult[0];
  135. $outputResult = $outputResult[0];
  136. if (!empty($this->_replace)) {
  137. foreach ($outputResult as $i => $element) {
  138. $index = array_search($element, $this->_match);
  139. if ($index !== false) {
  140. unset($outputResult[$i]);
  141. }
  142. }
  143. $outputResult = array_values($outputResult);
  144. }
  145. if (!empty($fileResult)) {
  146. $i = 0;
  147. foreach ($fileResult as $cacheBlock) {
  148. if (isset($outputResult[$i])) {
  149. $this->_replace[] = $cacheBlock;
  150. $this->_match[] = $outputResult[$i];
  151. }
  152. $i++;
  153. }
  154. }
  155. }
  156. /**
  157. * Parse the output and replace cache tags
  158. *
  159. * @param string $cache Output to replace content in.
  160. * @return string with all replacements made to <!--nocache--><!--nocache-->
  161. */
  162. protected 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. for ($i = 1; $i <= 1; $i++) {
  170. $position = strpos($cache, $found, $position);
  171. if ($position !== false) {
  172. $cache = substr($original, 0, $position);
  173. $cache .= $this->_replace[$count];
  174. $cache .= substr($original, $position + $length);
  175. } else {
  176. break;
  177. }
  178. }
  179. $count++;
  180. }
  181. return $cache;
  182. }
  183. return $cache;
  184. }
  185. /**
  186. * Write a cached version of the file
  187. *
  188. * @param string $content view content to write to a cache file.
  189. * @param sting $timestamp Duration to set for cache file.
  190. * @return boolean success of caching view.
  191. */
  192. protected function _writeFile($content, $timestamp, $useCallbacks = false) {
  193. $now = time();
  194. if (is_numeric($timestamp)) {
  195. $cacheTime = $now + $timestamp;
  196. } else {
  197. $cacheTime = strtotime($timestamp, $now);
  198. }
  199. $path = $this->request->here;
  200. if ($this->here == '/') {
  201. $path = 'home';
  202. }
  203. $cache = strtolower(Inflector::slug($path));
  204. if (empty($cache)) {
  205. return;
  206. }
  207. $cache = $cache . '.php';
  208. $file = '<!--cachetime:' . $cacheTime . '--><?php';
  209. if (empty($this->_View->plugin)) {
  210. $file .= '
  211. App::import(\'Controller\', \'' . $this->_View->name. '\');
  212. ';
  213. } else {
  214. $file .= '
  215. App::import(\'Controller\', \'' . $this->_View->plugin . '.' . $this->_View->name. '\');
  216. ';
  217. }
  218. $file .= '$controller = new ' . $this->_View->name . 'Controller();
  219. $controller->plugin = $this->plugin = \'' . $this->_View->plugin . '\';
  220. $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->_View->helpers) . '\');
  221. $controller->layout = $this->layout = \'' . $this->_View->layout. '\';
  222. $controller->request = $this->request = unserialize(\'' . str_replace("'", "\\'", serialize($this->request)) . '\');
  223. $controller->theme = $this->theme = \'' . $this->_View->theme . '\';
  224. Router::setRequestInfo($controller->request);';
  225. if ($useCallbacks == true) {
  226. $file .= '
  227. $controller->constructClasses();
  228. $controller->startupProcess();';
  229. }
  230. $file .= '
  231. $this->loadHelpers();
  232. ?>';
  233. $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
  234. $file .= $content;
  235. return cache('views' . DS . $cache, $file, $timestamp);
  236. }
  237. }