PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/mariuz/firetube
PHP | 329 lines | 200 code | 28 blank | 101 comment | 35 complexity | 6243e68fe227653a26f05509c1a2143c 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-2011, 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-2011, 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. * Counter used for counting nocache section tags.
  59. *
  60. * @var integer
  61. */
  62. var $_counter = 0;
  63. /**
  64. * Main method used to cache a view
  65. *
  66. * @param string $file File to cache
  67. * @param string $out output to cache
  68. * @param boolean $cache
  69. * @return view ouput
  70. */
  71. function cache($file, $out, $cache = false) {
  72. $cacheTime = 0;
  73. $useCallbacks = false;
  74. if (is_array($this->cacheAction)) {
  75. $controller = Inflector::underscore($this->controllerName);
  76. $controllerAlternate = Inflector::variable($this->controllerName);
  77. $check = str_replace('/', '_', $this->here);
  78. $basePath = str_replace('/', '_', $this->base);
  79. $search = '/' . preg_quote($this->base, '/') . '/';
  80. $match = preg_replace($search, '', $this->here, 1);
  81. $match = str_replace('//', '/', $match);
  82. $match = str_replace('/' . $controller . '/', '', $match);
  83. $match = str_replace('/' . $controllerAlternate . '/', '', $match);
  84. $match = str_replace('/' . $this->controllerName . '/', '', $match);
  85. $check = str_replace($basePath, '', $check);
  86. $check = str_replace('_' . $controller . '_', '', $check);
  87. $check = str_replace('_' . $this->controllerName . '_', '', $check);
  88. $check = str_replace('_' . $controllerAlternate . '_', '', $match);
  89. $check = Inflector::slug($check);
  90. $check = trim($check, '_');
  91. $keys = str_replace('/', '_', array_keys($this->cacheAction));
  92. $found = array_keys($this->cacheAction);
  93. $index = null;
  94. $count = 0;
  95. foreach ($keys as $key => $value) {
  96. if (strpos($check, rtrim($value, '_')) === 0) {
  97. $index = $found[$count];
  98. break;
  99. }
  100. $count++;
  101. }
  102. if (isset($index)) {
  103. $pos1 = strrpos($match, '/');
  104. $char = strlen($match) - 1;
  105. if ($pos1 == $char) {
  106. $match = substr($match, 0, $char);
  107. }
  108. $key = $match;
  109. } elseif ($this->action == 'index') {
  110. $index = 'index';
  111. }
  112. $options = $this->cacheAction;
  113. if (isset($this->cacheAction[$index])) {
  114. if (is_array($this->cacheAction[$index])) {
  115. $options = array_merge(array('duration'=> 0, 'callbacks' => false), $this->cacheAction[$index]);
  116. } else {
  117. $cacheTime = $this->cacheAction[$index];
  118. }
  119. }
  120. if (array_key_exists('duration', $options)) {
  121. $cacheTime = $options['duration'];
  122. }
  123. if (array_key_exists('callbacks', $options)) {
  124. $useCallbacks = $options['callbacks'];
  125. }
  126. } else {
  127. $cacheTime = $this->cacheAction;
  128. }
  129. if ($cacheTime != '' && $cacheTime > 0) {
  130. $out = preg_replace_callback('/<cake\:nocache>/', array($this, '_replaceSection'), $out);
  131. $this->__parseFile($file, $out);
  132. if ($cache === true) {
  133. $cached = $this->__parseOutput($out);
  134. $this->__writeFile($cached, $cacheTime, $useCallbacks);
  135. $out = $this->_stripTags($out);
  136. }
  137. return $out;
  138. } else {
  139. return $out;
  140. }
  141. }
  142. /**
  143. * Parse file searching for no cache tags
  144. *
  145. * @param string $file
  146. * @param boolean $cache
  147. * @access private
  148. */
  149. function __parseFile($file, $cache) {
  150. if (is_file($file)) {
  151. $file = file_get_contents($file);
  152. } elseif ($file = fileExistsInPath($file)) {
  153. $file = file_get_contents($file);
  154. }
  155. preg_match_all('/(<cake:nocache:\d{3}>(?<=<cake:nocache:\d{3}>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
  156. preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER);
  157. $fileResult = $fileResult[0];
  158. $outputResult = $outputResult[0];
  159. if (!empty($this->__replace)) {
  160. foreach ($outputResult as $i => $element) {
  161. $index = array_search($element, $this->__match);
  162. if ($index !== false) {
  163. unset($outputResult[$i]);
  164. }
  165. }
  166. $outputResult = array_values($outputResult);
  167. }
  168. if (!empty($fileResult)) {
  169. $i = 0;
  170. foreach ($fileResult as $cacheBlock) {
  171. if (isset($outputResult[$i])) {
  172. $this->__replace[] = $cacheBlock;
  173. $this->__match[] = $outputResult[$i];
  174. }
  175. $i++;
  176. }
  177. }
  178. }
  179. /**
  180. * Munges the output from a view with cache tags, and numbers the sections.
  181. * This helps solve issues with empty/duplicate content.
  182. *
  183. * @param string $content The content to munge.
  184. * @return string The content with cake:nocache tags replaced.
  185. */
  186. function _replaceSection($matches) {
  187. $this->_counter += 1;
  188. return sprintf('<cake:nocache:%03d>', $this->_counter);
  189. }
  190. /**
  191. * Strip cake:nocache tags from a string. Since View::render()
  192. * only removes un-numbered nocache tags, remove all the numbered ones.
  193. * This is the complement to _replaceSection.
  194. *
  195. * @param string $content String to remove tags from.
  196. * @return string String with tags removed.
  197. */
  198. function _stripTags($content) {
  199. return preg_replace('#<\/?cake\:nocache(\:\d{3})?>#', '', $content);
  200. }
  201. /**
  202. * Parse the output and replace cache tags
  203. *
  204. * @param sting $cache
  205. * @return string with all replacements made to <cake:nocache><cake:nocache>
  206. * @access private
  207. */
  208. function __parseOutput($cache) {
  209. $count = 0;
  210. if (!empty($this->__match)) {
  211. foreach ($this->__match as $found) {
  212. $original = $cache;
  213. $length = strlen($found);
  214. $position = 0;
  215. for ($i = 1; $i <= 1; $i++) {
  216. $position = strpos($cache, $found, $position);
  217. if ($position !== false) {
  218. $cache = substr($original, 0, $position);
  219. $cache .= $this->__replace[$count];
  220. $cache .= substr($original, $position + $length);
  221. } else {
  222. break;
  223. }
  224. }
  225. $count++;
  226. }
  227. return $cache;
  228. }
  229. return $cache;
  230. }
  231. /**
  232. * Write a cached version of the file
  233. *
  234. * @param string $file
  235. * @param sting $timestamp
  236. * @return cached view
  237. * @access private
  238. */
  239. function __writeFile($content, $timestamp, $useCallbacks = false) {
  240. $now = time();
  241. if (is_numeric($timestamp)) {
  242. $cacheTime = $now + $timestamp;
  243. } else {
  244. $cacheTime = strtotime($timestamp, $now);
  245. }
  246. $path = $this->here;
  247. if ($this->here == '/') {
  248. $path = 'home';
  249. }
  250. $cache = strtolower(Inflector::slug($path));
  251. if (empty($cache)) {
  252. return;
  253. }
  254. $cache = $cache . '.php';
  255. $file = '<!--cachetime:' . $cacheTime . '--><?php';
  256. if (empty($this->plugin)) {
  257. $file .= '
  258. App::import(\'Controller\', \'' . $this->controllerName. '\');
  259. ';
  260. } else {
  261. $file .= '
  262. App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\');
  263. ';
  264. }
  265. $file .= '$controller =& new ' . $this->controllerName . 'Controller();
  266. $controller->plugin = $this->plugin = \''.$this->plugin.'\';
  267. $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\');
  268. $controller->base = $this->base = \'' . $this->base . '\';
  269. $controller->layout = $this->layout = \'' . $this->layout. '\';
  270. $controller->webroot = $this->webroot = \'' . $this->webroot . '\';
  271. $controller->here = $this->here = \'' . $this->here . '\';
  272. $controller->namedArgs = $this->namedArgs = \'' . $this->namedArgs . '\';
  273. $controller->argSeparator = $this->argSeparator = \'' . $this->argSeparator . '\';
  274. $controller->params = $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->params)) . '\'));
  275. $controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
  276. $controller->data = $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->data)) . '\'));
  277. $controller->themeWeb = $this->themeWeb = \'' . $this->themeWeb . '\';
  278. Router::setRequestInfo(array($this->params, array(\'base\' => $this->base, \'webroot\' => $this->webroot)));';
  279. if ($useCallbacks == true) {
  280. $file .= '
  281. $controller->constructClasses();
  282. $controller->Component->initialize($controller);
  283. $controller->beforeFilter();
  284. $controller->Component->startup($controller);
  285. $this->params = $controller->params;';
  286. }
  287. $file .= '
  288. $loadedHelpers = array();
  289. $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers);
  290. foreach (array_keys($loadedHelpers) as $helper) {
  291. $camelBackedHelper = Inflector::variable($helper);
  292. ${$camelBackedHelper} =& $loadedHelpers[$helper];
  293. $this->loaded[$camelBackedHelper] =& ${$camelBackedHelper};
  294. }
  295. ?>';
  296. $content = preg_replace("/(<\\?xml)/", "<?php echo '$1';?>",$content);
  297. $file .= $content;
  298. return cache('views' . DS . $cache, $file, $timestamp);
  299. }
  300. }
  301. ?>