PageRenderTime 28ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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