PageRenderTime 530ms CodeModel.GetById 33ms RepoModel.GetById 10ms app.codeStats 0ms

/cake/libs/view/helpers/cache.php

https://github.com/robwilkerson/CakePHP-Boilerplate
PHP | 299 lines | 174 code | 27 blank | 98 comment | 35 complexity | 06151d54f32d3a5a56ebdaf4eb19e8b8 MD5 | raw file
  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-2011, 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-2011, 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 <cake:nocache><cake:nocache> in views
  34. *
  35. * @var array
  36. * @access private
  37. */
  38. var $__replace = array();
  39. /**
  40. * Array of string that are replace with there var replace above.
  41. * The strings are any content inside <cake:nocache><cake:nocache> and includes the tags in views
  42. *
  43. * @var array
  44. * @access private
  45. */
  46. var $__match = array();
  47. /**
  48. * cache action time
  49. *
  50. * @var object
  51. * @access public
  52. */
  53. var $cacheAction;
  54. /**
  55. * Counter used for counting nocache section tags.
  56. *
  57. * @var integer
  58. */
  59. var $_counter = 0;
  60. /**
  61. * Main method used to cache a view
  62. *
  63. * @param string $file File to cache
  64. * @param string $out output to cache
  65. * @param boolean $cache Whether or not a cache file should be written.
  66. * @return string view ouput
  67. */
  68. function cache($file, $out, $cache = false) {
  69. $cacheTime = 0;
  70. $useCallbacks = false;
  71. if (is_array($this->cacheAction)) {
  72. $keys = array_keys($this->cacheAction);
  73. $index = null;
  74. foreach ($keys as $action) {
  75. if ($action == $this->params['action']) {
  76. $index = $action;
  77. break;
  78. }
  79. }
  80. if (!isset($index) && $this->action == 'index') {
  81. $index = 'index';
  82. }
  83. $options = $this->cacheAction;
  84. if (isset($this->cacheAction[$index])) {
  85. if (is_array($this->cacheAction[$index])) {
  86. $options = array_merge(array('duration' => 0, 'callbacks' => false), $this->cacheAction[$index]);
  87. } else {
  88. $cacheTime = $this->cacheAction[$index];
  89. }
  90. }
  91. if (isset($options['duration'])) {
  92. $cacheTime = $options['duration'];
  93. }
  94. if (isset($options['callbacks'])) {
  95. $useCallbacks = $options['callbacks'];
  96. }
  97. } else {
  98. $cacheTime = $this->cacheAction;
  99. }
  100. if ($cacheTime != '' && $cacheTime > 0) {
  101. $out = preg_replace_callback('/<cake\:nocache>/', array($this, '_replaceSection'), $out);
  102. $this->__parseFile($file, $out);
  103. if ($cache === true) {
  104. $cached = $this->__parseOutput($out);
  105. $this->__writeFile($cached, $cacheTime, $useCallbacks);
  106. $out = $this->_stripTags($out);
  107. }
  108. return $out;
  109. } else {
  110. return $out;
  111. }
  112. }
  113. /**
  114. * Parse file searching for no cache tags
  115. *
  116. * @param string $file The filename that needs to be parsed.
  117. * @param string $cache The cached content
  118. * @access private
  119. */
  120. function __parseFile($file, $cache) {
  121. if (is_file($file)) {
  122. $file = file_get_contents($file);
  123. } elseif ($file = fileExistsInPath($file)) {
  124. $file = file_get_contents($file);
  125. }
  126. preg_match_all('/(<cake:nocache:\d{3}>(?<=<cake:nocache:\d{3}>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
  127. preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER);
  128. $fileResult = $fileResult[0];
  129. $outputResult = $outputResult[0];
  130. if (!empty($this->__replace)) {
  131. foreach ($outputResult as $i => $element) {
  132. $index = array_search($element, $this->__match);
  133. if ($index !== false) {
  134. unset($outputResult[$i]);
  135. }
  136. }
  137. $outputResult = array_values($outputResult);
  138. }
  139. if (!empty($fileResult)) {
  140. $i = 0;
  141. foreach ($fileResult as $cacheBlock) {
  142. if (isset($outputResult[$i])) {
  143. $this->__replace[] = $cacheBlock;
  144. $this->__match[] = $outputResult[$i];
  145. }
  146. $i++;
  147. }
  148. }
  149. }
  150. /**
  151. * Munges the output from a view with cache tags, and numbers the sections.
  152. * This helps solve issues with empty/duplicate content.
  153. *
  154. * @param string $content The content to munge.
  155. * @return string The content with cake:nocache tags replaced.
  156. */
  157. function _replaceSection($matches) {
  158. $this->_counter += 1;
  159. return sprintf('<cake:nocache:%03d>', $this->_counter);
  160. }
  161. /**
  162. * Strip cake:nocache tags from a string. Since View::render()
  163. * only removes un-numbered nocache tags, remove all the numbered ones.
  164. * This is the complement to _replaceSection.
  165. *
  166. * @param string $content String to remove tags from.
  167. * @return string String with tags removed.
  168. */
  169. function _stripTags($content) {
  170. return preg_replace('#<\/?cake\:nocache(\:\d{3})?>#', '', $content);
  171. }
  172. /**
  173. * Parse the output and replace cache tags
  174. *
  175. * @param string $cache Output to replace content in.
  176. * @return string with all replacements made to <cake:nocache><cake:nocache>
  177. * @access private
  178. */
  179. function __parseOutput($cache) {
  180. $count = 0;
  181. if (!empty($this->__match)) {
  182. foreach ($this->__match as $found) {
  183. $original = $cache;
  184. $length = strlen($found);
  185. $position = 0;
  186. for ($i = 1; $i <= 1; $i++) {
  187. $position = strpos($cache, $found, $position);
  188. if ($position !== false) {
  189. $cache = substr($original, 0, $position);
  190. $cache .= $this->__replace[$count];
  191. $cache .= substr($original, $position + $length);
  192. } else {
  193. break;
  194. }
  195. }
  196. $count++;
  197. }
  198. return $cache;
  199. }
  200. return $cache;
  201. }
  202. /**
  203. * Write a cached version of the file
  204. *
  205. * @param string $content view content to write to a cache file.
  206. * @param sting $timestamp Duration to set for cache file.
  207. * @return boolean success of caching view.
  208. * @access private
  209. */
  210. function __writeFile($content, $timestamp, $useCallbacks = false) {
  211. $now = time();
  212. if (is_numeric($timestamp)) {
  213. $cacheTime = $now + $timestamp;
  214. } else {
  215. $cacheTime = strtotime($timestamp, $now);
  216. }
  217. $path = $this->here;
  218. if ($this->here == '/') {
  219. $path = 'home';
  220. }
  221. $cache = strtolower(Inflector::slug($path));
  222. if (empty($cache)) {
  223. return;
  224. }
  225. $cache = $cache . '.php';
  226. $file = '<!--cachetime:' . $cacheTime . '--><?php';
  227. if (empty($this->plugin)) {
  228. $file .= '
  229. App::import(\'Controller\', \'' . $this->controllerName. '\');
  230. ';
  231. } else {
  232. $file .= '
  233. App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\');
  234. ';
  235. }
  236. $file .= '$controller =& new ' . $this->controllerName . 'Controller();
  237. $controller->plugin = $this->plugin = \''.$this->plugin.'\';
  238. $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\');
  239. $controller->base = $this->base = \'' . $this->base . '\';
  240. $controller->layout = $this->layout = \'' . $this->layout. '\';
  241. $controller->webroot = $this->webroot = \'' . $this->webroot . '\';
  242. $controller->here = $this->here = \'' . addslashes($this->here) . '\';
  243. $controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\'));
  244. $controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
  245. $controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\'));
  246. $controller->viewVars = $this->viewVars = unserialize(base64_decode(\'' . base64_encode(serialize($this->viewVars)) . '\'));
  247. $controller->theme = $this->theme = \'' . $this->theme . '\';
  248. Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));';
  249. if ($useCallbacks == true) {
  250. $file .= '
  251. $controller->constructClasses();
  252. $controller->Component->initialize($controller);
  253. $controller->beforeFilter();
  254. $controller->Component->startup($controller);
  255. $this->params = $controller->params;';
  256. }
  257. $file .= '
  258. $loadedHelpers = array();
  259. $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
  260. foreach (array_keys($loadedHelpers) as $helper) {
  261. $camelBackedHelper = Inflector::variable($helper);
  262. ${$camelBackedHelper} =& $loadedHelpers[$helper];
  263. $this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
  264. $this->{$helper} =& $loadedHelpers[$helper];
  265. }
  266. extract($this->viewVars, EXTR_SKIP);
  267. ?>';
  268. $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
  269. $file .= $content;
  270. return cache('views' . DS . $cache, $file, $timestamp);
  271. }
  272. }