Initial pass at decoupling CacheHelper from View.

CacheHelper does all caching using helper callbacks now.
This commit is contained in:
mark_story 2010-11-06 00:07:51 -04:00
parent 1bc6433ecd
commit c92ecdcd1b
2 changed files with 41 additions and 52 deletions

View file

@ -49,12 +49,28 @@ class CacheHelper extends AppHelper {
private $__match = array(); private $__match = array();
/** /**
* cache action time * Parses the view file and stores content for cache file building.
* *
* @var object * @return void
* @access public
*/ */
public $cacheAction; public function afterRender($viewFile) {
$caching = (($this->_View->cacheAction != false)) && (Configure::read('Cache.check') === true);
if ($caching) {
$this->cache($viewFile, $this->_View->output, false);
}
}
/**
* Parses the layout file and stores content for cache file building.
*
* @return void
*/
public function afterLayout($layoutFile) {
$caching = (($this->_View->cacheAction != false)) && (Configure::read('Cache.check') === true);
if ($caching) {
$this->cache($layoutFile, $this->_View->output, true);
}
}
/** /**
* Main method used to cache a view * Main method used to cache a view
@ -67,8 +83,10 @@ class CacheHelper extends AppHelper {
function cache($file, $out, $cache = false) { function cache($file, $out, $cache = false) {
$cacheTime = 0; $cacheTime = 0;
$useCallbacks = false; $useCallbacks = false;
if (is_array($this->cacheAction)) { $cacheAction = $this->_View->cacheAction;
$keys = array_keys($this->cacheAction);
if (is_array($cacheAction)) {
$keys = array_keys($cacheAction);
$index = null; $index = null;
foreach ($keys as $action) { foreach ($keys as $action) {
@ -82,12 +100,12 @@ class CacheHelper extends AppHelper {
$index = 'index'; $index = 'index';
} }
$options = $this->cacheAction; $options = $cacheAction;
if (isset($this->cacheAction[$index])) { if (isset($cacheAction[$index])) {
if (is_array($this->cacheAction[$index])) { if (is_array($cacheAction[$index])) {
$options = array_merge(array('duration' => 0, 'callbacks' => false), $this->cacheAction[$index]); $options = array_merge(array('duration' => 0, 'callbacks' => false), $cacheAction[$index]);
} else { } else {
$cacheTime = $this->cacheAction[$index]; $cacheTime = $cacheAction[$index];
} }
} }
if (isset($options['duration'])) { if (isset($options['duration'])) {
@ -97,7 +115,7 @@ class CacheHelper extends AppHelper {
$useCallbacks = $options['callbacks']; $useCallbacks = $options['callbacks'];
} }
} else { } else {
$cacheTime = $this->cacheAction; $cacheTime = $cacheAction;
} }
if ($cacheTime != '' && $cacheTime > 0) { if ($cacheTime != '' && $cacheTime > 0) {
@ -213,36 +231,28 @@ class CacheHelper extends AppHelper {
$cache = $cache . '.php'; $cache = $cache . '.php';
$file = '<!--cachetime:' . $cacheTime . '--><?php'; $file = '<!--cachetime:' . $cacheTime . '--><?php';
if (empty($this->plugin)) { if (empty($this->_View->plugin)) {
$file .= ' $file .= '
App::import(\'Controller\', \'' . $this->controllerName. '\'); App::import(\'Controller\', \'' . $this->_View->name. '\');
'; ';
} else { } else {
$file .= ' $file .= '
App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\'); App::import(\'Controller\', \'' . $this->_View->plugin . '.' . $this->_View->name. '\');
'; ';
} }
$file .= '$controller =& new ' . $this->controllerName . 'Controller(); $file .= '$controller = new ' . $this->_View->name . 'Controller();
$controller->plugin = $this->plugin = \''.$this->plugin.'\'; $controller->plugin = $this->plugin = \'' . $this->_View->plugin . '\';
$controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\'); $controller->helpers = $this->helpers = unserialize(\'' . serialize($this->_View->helpers) . '\');
$controller->base = $this->base = \'' . $this->base . '\'; $controller->layout = $this->layout = \'' . $this->_View->layout. '\';
$controller->layout = $this->layout = \'' . $this->layout. '\';
$controller->webroot = $this->webroot = \'' . $this->webroot . '\';
$controller->here = $this->here = \'' . $this->here . '\';
$controller->params = $this->params = unserialize(\'' . str_replace("'", "\\'", serialize($this->params)) . '\');
$controller->request = $this->request = unserialize(\'' . str_replace("'", "\\'", serialize($this->request)) . '\'); $controller->request = $this->request = unserialize(\'' . str_replace("'", "\\'", serialize($this->request)) . '\');
$controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\'); $controller->theme = $this->theme = \'' . $this->_View->theme . '\';
$controller->data = $this->data = unserialize(\'' . str_replace("'", "\\'", serialize($this->data)) . '\'); Router::setRequestInfo($controller->request);';
$controller->theme = $this->theme = \'' . $this->theme . '\';
Router::setRequestInfo($this->params);';
if ($useCallbacks == true) { if ($useCallbacks == true) {
$file .= ' $file .= '
$controller->constructClasses(); $controller->constructClasses();
$controller->Component->initialize($controller); $controller->startupProcess();';
$controller->beforeFilter();
$controller->Component->startup($controller);';
} }
$file .= ' $file .= '

View file

@ -463,7 +463,6 @@ class View extends Object {
} }
$this->Helpers->trigger('afterLayout', array($layoutFileName)); $this->Helpers->trigger('afterLayout', array($layoutFileName));
return $this->output; return $this->output;
} }
@ -670,27 +669,7 @@ class View extends Object {
include $___viewFn; include $___viewFn;
$out = ob_get_clean(); return ob_get_clean();
$caching = (
isset($this->Helpers->Cache) &&
(($this->cacheAction != false)) && (Configure::read('Cache.check') === true)
);
if ($caching) {
if (isset($this->Helpers->Cache)) {
$cache =& $this->Helpers->Cache;
$cache->base = $this->request->base;
$cache->here = $this->request->here;
$cache->helpers = $this->helpers;
$cache->action = $this->request->action;
$cache->controllerName = $this->name;
$cache->layout = $this->layout;
$cache->cacheAction = $this->cacheAction;
$cache->cache($___viewFn, $out, $cached);
}
}
return $out;
} }
/** /**