PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/code/web/public_php/webtt/cake/libs/view/helpers/cache.php

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