2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-10-01 02:44:15 +00:00
|
|
|
* CacheHelper helps create full page view caching.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2012-03-13 02:46:46 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2012-03-13 02:46:46 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.0.0.2277
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-04 18:10:24 +00:00
|
|
|
App::uses('AppHelper', 'View/Helper');
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-10-01 02:44:15 +00:00
|
|
|
* CacheHelper helps create full page view caching.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-10-01 02:44:15 +00:00
|
|
|
* When using CacheHelper you don't call any of its methods, they are all automatically
|
|
|
|
* called by View, and use the $cacheAction settings set in the controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.View.Helper
|
2011-10-15 17:06:19 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class CacheHelper extends AppHelper {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Array of strings replaced in cached views.
|
2010-11-06 05:18:23 +00:00
|
|
|
* The strings are found between `<!--nocache--><!--/nocache-->` in views
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-11-06 05:18:23 +00:00
|
|
|
protected $_replace = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Array of string that are replace with there var replace above.
|
2010-11-06 05:18:23 +00:00
|
|
|
* The strings are any content inside `<!--nocache--><!--/nocache-->` and includes the tags in views
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-11-06 05:18:23 +00:00
|
|
|
protected $_match = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-08-19 02:16:16 +00:00
|
|
|
/**
|
|
|
|
* Counter used for counting nocache section tags.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $_counter = 0;
|
|
|
|
|
2011-10-09 01:36:41 +00:00
|
|
|
/**
|
|
|
|
* Is CacheHelper enabled? should files + output be parsed.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
protected function _enabled() {
|
|
|
|
return (($this->_View->cacheAction != false)) && (Configure::read('Cache.check') === true);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-11-06 04:07:51 +00:00
|
|
|
* Parses the view file and stores content for cache file building.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param string $viewFile
|
2010-11-06 04:07:51 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-10-09 02:04:53 +00:00
|
|
|
public function afterRenderFile($viewFile, $output) {
|
2011-10-09 01:36:41 +00:00
|
|
|
if ($this->_enabled()) {
|
2011-10-09 02:04:53 +00:00
|
|
|
return $this->_parseContent($viewFile, $output);
|
2010-11-06 04:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses the layout file and stores content for cache file building.
|
|
|
|
*
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param string $layoutFile
|
2010-11-06 04:07:51 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function afterLayout($layoutFile) {
|
2011-10-09 01:36:41 +00:00
|
|
|
if ($this->_enabled()) {
|
2011-10-09 02:04:53 +00:00
|
|
|
$this->_View->output = $this->cache($layoutFile, $this->_View->output);
|
2010-11-06 04:07:51 +00:00
|
|
|
}
|
2010-11-10 04:55:30 +00:00
|
|
|
$this->_View->output = preg_replace('/<!--\/?nocache-->/', '', $this->_View->output);
|
2010-11-06 04:07:51 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-10-09 01:48:16 +00:00
|
|
|
/**
|
|
|
|
* Parse a file + output. Matches nocache tags between the current output and the current file
|
|
|
|
* stores a reference of the file, so the generated can be swapped back with the file contents when
|
|
|
|
* writing the cache file.
|
|
|
|
*
|
|
|
|
* @param string $file The filename to process.
|
|
|
|
* @param string $out The output for the file.
|
|
|
|
* @return string Updated content.
|
|
|
|
*/
|
|
|
|
protected function _parseContent($file, $out) {
|
|
|
|
$out = preg_replace_callback('/<!--nocache-->/', array($this, '_replaceSection'), $out);
|
|
|
|
$this->_parseFile($file, $out);
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Main method used to cache a view
|
|
|
|
*
|
|
|
|
* @param string $file File to cache
|
|
|
|
* @param string $out output to cache
|
2011-10-09 01:48:16 +00:00
|
|
|
* @return string view ouput
|
2011-11-30 05:23:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-10-09 01:48:16 +00:00
|
|
|
public function cache($file, $out) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$cacheTime = 0;
|
2008-06-15 19:48:15 +00:00
|
|
|
$useCallbacks = false;
|
2010-11-06 04:07:51 +00:00
|
|
|
$cacheAction = $this->_View->cacheAction;
|
|
|
|
|
|
|
|
if (is_array($cacheAction)) {
|
|
|
|
$keys = array_keys($cacheAction);
|
2008-05-30 11:40:08 +00:00
|
|
|
$index = null;
|
|
|
|
|
2009-12-29 02:26:51 +00:00
|
|
|
foreach ($keys as $action) {
|
2010-05-14 04:12:29 +00:00
|
|
|
if ($action == $this->request->params['action']) {
|
2009-12-29 02:26:51 +00:00
|
|
|
$index = $action;
|
2008-05-30 11:40:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 04:12:29 +00:00
|
|
|
if (!isset($index) && $this->request->params['action'] == 'index') {
|
2008-05-30 11:40:08 +00:00
|
|
|
$index = 'index';
|
|
|
|
}
|
|
|
|
|
2010-11-06 04:07:51 +00:00
|
|
|
$options = $cacheAction;
|
|
|
|
if (isset($cacheAction[$index])) {
|
|
|
|
if (is_array($cacheAction[$index])) {
|
|
|
|
$options = array_merge(array('duration' => 0, 'callbacks' => false), $cacheAction[$index]);
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2010-11-06 04:07:51 +00:00
|
|
|
$cacheTime = $cacheAction[$index];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-12-29 02:26:51 +00:00
|
|
|
if (isset($options['duration'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$cacheTime = $options['duration'];
|
|
|
|
}
|
2009-12-29 02:26:51 +00:00
|
|
|
if (isset($options['callbacks'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$useCallbacks = $options['callbacks'];
|
|
|
|
}
|
|
|
|
} else {
|
2010-11-06 04:07:51 +00:00
|
|
|
$cacheTime = $cacheAction;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($cacheTime != '' && $cacheTime > 0) {
|
2011-10-09 01:48:16 +00:00
|
|
|
$cached = $this->_parseOutput($out);
|
|
|
|
$this->_writeFile($cached, $cacheTime, $useCallbacks);
|
|
|
|
$out = $this->_stripTags($out);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2011-10-09 01:48:16 +00:00
|
|
|
return $out;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Parse file searching for no cache tags
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $file The filename that needs to be parsed.
|
|
|
|
* @param string $cache The cached content
|
2011-07-29 02:45:47 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-11-06 05:23:08 +00:00
|
|
|
protected function _parseFile($file, $cache) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_file($file)) {
|
|
|
|
$file = file_get_contents($file);
|
|
|
|
} elseif ($file = fileExistsInPath($file)) {
|
|
|
|
$file = file_get_contents($file);
|
|
|
|
}
|
2011-08-14 18:39:49 +00:00
|
|
|
preg_match_all('/(<!--nocache:\d{3}-->(?<=<!--nocache:\d{3}-->)[\\s\\S]*?(?=<!--\/nocache-->)<!--\/nocache-->)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
|
2010-11-06 05:18:23 +00:00
|
|
|
preg_match_all('/(?<=<!--nocache-->)([\\s\\S]*?)(?=<!--\/nocache-->)/i', $file, $fileResult, PREG_PATTERN_ORDER);
|
2009-10-01 05:03:47 +00:00
|
|
|
$fileResult = $fileResult[0];
|
|
|
|
$outputResult = $outputResult[0];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-11-06 05:18:23 +00:00
|
|
|
if (!empty($this->_replace)) {
|
2009-10-01 05:03:47 +00:00
|
|
|
foreach ($outputResult as $i => $element) {
|
2010-11-06 05:18:23 +00:00
|
|
|
$index = array_search($element, $this->_match);
|
2008-09-05 21:41:07 +00:00
|
|
|
if ($index !== false) {
|
2009-10-01 05:03:47 +00:00
|
|
|
unset($outputResult[$i]);
|
2008-09-24 11:52:29 +00:00
|
|
|
}
|
2008-09-05 21:41:07 +00:00
|
|
|
}
|
2009-10-01 05:03:47 +00:00
|
|
|
$outputResult = array_values($outputResult);
|
2008-09-05 21:41:07 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-01 05:03:47 +00:00
|
|
|
if (!empty($fileResult)) {
|
|
|
|
$i = 0;
|
|
|
|
foreach ($fileResult as $cacheBlock) {
|
|
|
|
if (isset($outputResult[$i])) {
|
2010-11-06 05:18:23 +00:00
|
|
|
$this->_replace[] = $cacheBlock;
|
|
|
|
$this->_match[] = $outputResult[$i];
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-10-01 05:03:47 +00:00
|
|
|
$i++;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-08-02 02:54:49 +00:00
|
|
|
/**
|
|
|
|
* Munges the output from a view with cache tags, and numbers the sections.
|
|
|
|
* This helps solve issues with empty/duplicate content.
|
|
|
|
*
|
|
|
|
* @return string The content with cake:nocache tags replaced.
|
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _replaceSection() {
|
2011-08-02 02:54:49 +00:00
|
|
|
$this->_counter += 1;
|
2011-08-14 18:39:49 +00:00
|
|
|
return sprintf('<!--nocache:%03d-->', $this->_counter);
|
2011-08-02 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip cake:nocache tags from a string. Since View::render()
|
|
|
|
* only removes un-numbered nocache tags, remove all the numbered ones.
|
|
|
|
* This is the complement to _replaceSection.
|
|
|
|
*
|
|
|
|
* @param string $content String to remove tags from.
|
|
|
|
* @return string String with tags removed.
|
|
|
|
*/
|
2011-08-20 05:39:30 +00:00
|
|
|
protected function _stripTags($content) {
|
2011-08-14 18:39:49 +00:00
|
|
|
return preg_replace('#<!--/?nocache(\:\d{3})?-->#', '', $content);
|
2011-08-02 02:54:49 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Parse the output and replace cache tags
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $cache Output to replace content in.
|
2010-11-06 05:18:23 +00:00
|
|
|
* @return string with all replacements made to <!--nocache--><!--nocache-->
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-11-06 05:23:08 +00:00
|
|
|
protected function _parseOutput($cache) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$count = 0;
|
2010-11-06 05:18:23 +00:00
|
|
|
if (!empty($this->_match)) {
|
|
|
|
foreach ($this->_match as $found) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$original = $cache;
|
|
|
|
$length = strlen($found);
|
|
|
|
$position = 0;
|
|
|
|
|
2009-10-30 20:42:04 +00:00
|
|
|
for ($i = 1; $i <= 1; $i++) {
|
|
|
|
$position = strpos($cache, $found, $position);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-10-30 20:42:04 +00:00
|
|
|
if ($position !== false) {
|
|
|
|
$cache = substr($original, 0, $position);
|
2010-11-06 05:18:23 +00:00
|
|
|
$cache .= $this->_replace[$count];
|
2009-10-30 20:42:04 +00:00
|
|
|
$cache .= substr($original, $position + $length);
|
|
|
|
} else {
|
|
|
|
break;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-10-30 20:42:04 +00:00
|
|
|
}
|
|
|
|
$count++;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $cache;
|
|
|
|
}
|
|
|
|
return $cache;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Write a cached version of the file
|
|
|
|
*
|
2010-01-25 22:59:05 +00:00
|
|
|
* @param string $content view content to write to a cache file.
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param string $timestamp Duration to set for cache file.
|
2011-07-29 02:45:47 +00:00
|
|
|
* @param boolean $useCallbacks
|
2010-01-25 22:59:05 +00:00
|
|
|
* @return boolean success of caching view.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-11-06 05:23:08 +00:00
|
|
|
protected function _writeFile($content, $timestamp, $useCallbacks = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$now = time();
|
|
|
|
|
|
|
|
if (is_numeric($timestamp)) {
|
|
|
|
$cacheTime = $now + $timestamp;
|
|
|
|
} else {
|
|
|
|
$cacheTime = strtotime($timestamp, $now);
|
|
|
|
}
|
2011-03-03 03:03:21 +00:00
|
|
|
$path = $this->request->here();
|
|
|
|
if ($path == '/') {
|
2008-06-12 18:36:08 +00:00
|
|
|
$path = 'home';
|
|
|
|
}
|
2008-10-26 18:51:38 +00:00
|
|
|
$cache = strtolower(Inflector::slug($path));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (empty($cache)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$cache = $cache . '.php';
|
|
|
|
$file = '<!--cachetime:' . $cacheTime . '--><?php';
|
|
|
|
|
2010-11-06 04:07:51 +00:00
|
|
|
if (empty($this->_View->plugin)) {
|
2011-11-01 02:27:43 +00:00
|
|
|
$file .= "
|
|
|
|
App::uses('{$this->_View->name}Controller', 'Controller');
|
|
|
|
";
|
2008-05-30 11:40:08 +00:00
|
|
|
} else {
|
2011-11-01 02:27:43 +00:00
|
|
|
$file .= "
|
2012-01-29 02:58:30 +00:00
|
|
|
App::uses('{$this->_View->plugin}AppController', '{$this->_View->plugin}.Controller');
|
2011-11-01 02:27:43 +00:00
|
|
|
App::uses('{$this->_View->name}Controller', '{$this->_View->plugin}.Controller');
|
|
|
|
";
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 02:27:43 +00:00
|
|
|
$file .= '
|
2012-03-27 05:27:15 +00:00
|
|
|
$request = unserialize(base64_decode(\'' . base64_encode(serialize($this->request)) . '\'));
|
2011-11-01 02:27:43 +00:00
|
|
|
$response = new CakeResponse(array("charset" => Configure::read("App.encoding")));
|
|
|
|
$controller = new ' . $this->_View->name . 'Controller($request, $response);
|
2010-11-06 04:07:51 +00:00
|
|
|
$controller->plugin = $this->plugin = \'' . $this->_View->plugin . '\';
|
2011-11-28 12:02:39 +00:00
|
|
|
$controller->helpers = $this->helpers = unserialize(base64_decode(\'' . base64_encode(serialize($this->_View->helpers)) . '\'));
|
2011-12-18 17:40:23 +00:00
|
|
|
$controller->layout = $this->layout = \'' . $this->_View->layout . '\';
|
2010-11-06 04:07:51 +00:00
|
|
|
$controller->theme = $this->theme = \'' . $this->_View->theme . '\';
|
2011-12-03 19:45:21 +00:00
|
|
|
$controller->viewVars = unserialize(base64_decode(\'' . base64_encode(serialize($this->_View->viewVars)) . '\'));
|
2011-11-01 02:27:43 +00:00
|
|
|
Router::setRequestInfo($controller->request);
|
|
|
|
$this->request = $request;';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if ($useCallbacks == true) {
|
2008-06-15 19:48:15 +00:00
|
|
|
$file .= '
|
|
|
|
$controller->constructClasses();
|
2010-11-06 04:07:51 +00:00
|
|
|
$controller->startupProcess();';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$file .= '
|
2011-12-03 19:45:21 +00:00
|
|
|
$this->viewVars = $controller->viewVars;
|
2010-08-27 03:24:09 +00:00
|
|
|
$this->loadHelpers();
|
2010-12-18 21:31:10 +00:00
|
|
|
extract($this->viewVars, EXTR_SKIP);
|
2008-05-30 11:40:08 +00:00
|
|
|
?>';
|
2012-05-19 14:48:15 +00:00
|
|
|
$content = preg_replace("/(<\\?xml)/", "<?php echo '$1'; ?>", $content);
|
2008-06-20 11:57:56 +00:00
|
|
|
$file .= $content;
|
|
|
|
return cache('views' . DS . $cache, $file, $timestamp);
|
|
|
|
}
|
2012-03-03 22:10:12 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|