PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://code.google.com/
PHP | 293 lines | 189 code | 23 blank | 81 comment | 35 complexity | beef431a1b58b740728ef48eee105bc4 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * CacheHelper helps create full page view caching.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package cake
  17. * @subpackage cake.cake.libs.view.helpers
  18. * @since CakePHP(tm) v 1.0.0.2277
  19. * @version $Revision$
  20. * @modifiedby $LastChangedBy$
  21. * @lastmodified $Date$
  22. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  23. */
  24. /**
  25. * CacheHelper helps create full page view caching.
  26. *
  27. * When using CacheHelper you don't call any of its methods, they are all automatically
  28. * called by View, and use the $cacheAction settings set in the controller.
  29. *
  30. * @package cake
  31. * @subpackage cake.cake.libs.view.helpers
  32. */
  33. class CacheHelper extends AppHelper {
  34. /**
  35. * Array of strings replaced in cached views.
  36. * The strings are found between <cake:nocache><cake:nocache> in views
  37. *
  38. * @var array
  39. * @access private
  40. */
  41. var $__replace = array();
  42. /**
  43. * Array of string that are replace with there var replace above.
  44. * The strings are any content inside <cake:nocache><cake:nocache> and includes the tags in views
  45. *
  46. * @var array
  47. * @access private
  48. */
  49. var $__match = array();
  50. /**
  51. * cache action time
  52. *
  53. * @var object
  54. * @access public
  55. */
  56. var $cacheAction;
  57. /**
  58. * Main method used to cache a view
  59. *
  60. * @param string $file File to cache
  61. * @param string $out output to cache
  62. * @param boolean $cache
  63. * @return view ouput
  64. */
  65. function cache($file, $out, $cache = false) {
  66. $cacheTime = 0;
  67. $useCallbacks = false;
  68. if (is_array($this->cacheAction)) {
  69. $controller = Inflector::underscore($this->controllerName);
  70. $controllerAlternate = Inflector::variable($this->controllerName);
  71. $check = str_replace('/', '_', $this->here);
  72. $basePath = str_replace('/', '_', $this->base);
  73. $search = '/' . preg_quote($this->base, '/') . '/';
  74. $match = preg_replace($search, '', $this->here, 1);
  75. $match = str_replace('//', '/', $match);
  76. $match = str_replace('/' . $controller . '/', '', $match);
  77. $match = str_replace('/' . $controllerAlternate . '/', '', $match);
  78. $match = str_replace('/' . $this->controllerName . '/', '', $match);
  79. $check = str_replace($basePath, '', $check);
  80. $check = str_replace('_' . $controller . '_', '', $check);
  81. $check = str_replace('_' . $this->controllerName . '_', '', $check);
  82. $check = str_replace('_' . $controllerAlternate . '_', '', $match);
  83. $check = Inflector::slug($check);
  84. $check = trim($check, '_');
  85. $keys = str_replace('/', '_', array_keys($this->cacheAction));
  86. $found = array_keys($this->cacheAction);
  87. $index = null;
  88. $count = 0;
  89. foreach ($keys as $key => $value) {
  90. if (strpos($check, rtrim($value, '_')) === 0) {
  91. $index = $found[$count];
  92. break;
  93. }
  94. $count++;
  95. }
  96. if (isset($index)) {
  97. $pos1 = strrpos($match, '/');
  98. $char = strlen($match) - 1;
  99. if ($pos1 == $char) {
  100. $match = substr($match, 0, $char);
  101. }
  102. $key = $match;
  103. } elseif ($this->action == 'index') {
  104. $index = 'index';
  105. }
  106. $options = $this->cacheAction;
  107. if (isset($this->cacheAction[$index])) {
  108. if (is_array($this->cacheAction[$index])) {
  109. $options = array_merge(array('duration'=> 0, 'callbacks' => false), $this->cacheAction[$index]);
  110. } else {
  111. $cacheTime = $this->cacheAction[$index];
  112. }
  113. }
  114. if (array_key_exists('duration', $options)) {
  115. $cacheTime = $options['duration'];
  116. }
  117. if (array_key_exists('callbacks', $options)) {
  118. $useCallbacks = $options['callbacks'];
  119. }
  120. } else {
  121. $cacheTime = $this->cacheAction;
  122. }
  123. if ($cacheTime != '' && $cacheTime > 0) {
  124. $this->__parseFile($file, $out);
  125. if ($cache === true) {
  126. $cached = $this->__parseOutput($out);
  127. $this->__writeFile($cached, $cacheTime, $useCallbacks);
  128. }
  129. return $out;
  130. } else {
  131. return $out;
  132. }
  133. }
  134. /**
  135. * Parse file searching for no cache tags
  136. *
  137. * @param string $file
  138. * @param boolean $cache
  139. * @access private
  140. */
  141. function __parseFile($file, $cache) {
  142. if (is_file($file)) {
  143. $file = file_get_contents($file);
  144. } elseif ($file = fileExistsInPath($file)) {
  145. $file = file_get_contents($file);
  146. }
  147. preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
  148. preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER);
  149. $fileResult = $fileResult[0];
  150. $outputResult = $outputResult[0];
  151. if (!empty($this->__replace)) {
  152. foreach ($outputResult as $i => $element) {
  153. $index = array_search($element, $this->__match);
  154. if ($index !== false) {
  155. unset($outputResult[$i]);
  156. }
  157. }
  158. $outputResult = array_values($outputResult);
  159. }
  160. if (!empty($fileResult)) {
  161. $i = 0;
  162. foreach ($fileResult as $cacheBlock) {
  163. if (isset($outputResult[$i])) {
  164. $this->__replace[] = $cacheBlock;
  165. $this->__match[] = $outputResult[$i];
  166. }
  167. $i++;
  168. }
  169. }
  170. }
  171. /**
  172. * Parse the output and replace cache tags
  173. *
  174. * @param sting $cache
  175. * @return string with all replacements made to <cake:nocache><cake:nocache>
  176. * @access private
  177. */
  178. function __parseOutput($cache) {
  179. $count = 0;
  180. if (!empty($this->__match)) {
  181. foreach ($this->__match as $found) {
  182. $original = $cache;
  183. $length = strlen($found);
  184. $position = 0;
  185. for ($i = 1; $i <= 1; $i++) {
  186. $position = strpos($cache, $found, $position);
  187. if ($position !== false) {
  188. $cache = substr($original, 0, $position);
  189. $cache .= $this->__replace[$count];
  190. $cache .= substr($original, $position + $length);
  191. } else {
  192. break;
  193. }
  194. }
  195. $count++;
  196. }
  197. return $cache;
  198. }
  199. return $cache;
  200. }
  201. /**
  202. * Write a cached version of the file
  203. *
  204. * @param string $file
  205. * @param sting $timestamp
  206. * @return cached view
  207. * @access private
  208. */
  209. function __writeFile($content, $timestamp, $useCallbacks = false) {
  210. $now = time();
  211. if (is_numeric($timestamp)) {
  212. $cacheTime = $now + $timestamp;
  213. } else {
  214. $cacheTime = strtotime($timestamp, $now);
  215. }
  216. $path = $this->here;
  217. if ($this->here == '/') {
  218. $path = 'home';
  219. }
  220. $cache = strtolower(Inflector::slug($path));
  221. if (empty($cache)) {
  222. return;
  223. }
  224. $cache = $cache . '.php';
  225. $file = '<!--cachetime:' . $cacheTime . '--><?php';
  226. if (empty($this->plugin)) {
  227. $file .= '
  228. App::import(\'Controller\', \'' . $this->controllerName. '\');
  229. ';
  230. } else {
  231. $file .= '
  232. App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\');
  233. ';
  234. }
  235. $file .= '$controller =& new ' . $this->controllerName . 'Controller();
  236. $controller->plugin = $this->plugin = \''.$this->plugin.'\';
  237. $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\');
  238. $controller->base = $this->base = \'' . $this->base . '\';
  239. $controller->layout = $this->layout = \'' . $this->layout. '\';
  240. $controller->webroot = $this->webroot = \'' . $this->webroot . '\';
  241. $controller->here = $this->here = \'' . $this->here . '\';
  242. $controller->namedArgs = $this->namedArgs = \'' . $this->namedArgs . '\';
  243. $controller->argSeparator = $this->argSeparator = \'' . $this->argSeparator . '\';
  244. $controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\'));
  245. $controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
  246. $controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\'));
  247. $controller->themeWeb = $this->themeWeb = \'' . $this->themeWeb . '\';
  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. }
  256. $file .= '
  257. $loadedHelpers = array();
  258. $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
  259. foreach (array_keys($loadedHelpers) as $helper) {
  260. $camelBackedHelper = Inflector::variable($helper);
  261. ${$camelBackedHelper} =& $loadedHelpers[$helper];
  262. $this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
  263. }
  264. ?>';
  265. $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
  266. $file .= $content;
  267. return cache('views' . DS . $cache, $file, $timestamp);
  268. }
  269. }
  270. ?>