PageRenderTime 78ms CodeModel.GetById 45ms RepoModel.GetById 1ms app.codeStats 0ms

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

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