Skip to content

Commit

Permalink
Initial pass at decoupling CacheHelper from View.
Browse files Browse the repository at this point in the history
CacheHelper does all caching using helper callbacks now.
  • Loading branch information
markstory committed Nov 7, 2010
1 parent 1bc6433 commit c92ecdc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 52 deletions.
70 changes: 40 additions & 30 deletions cake/libs/view/helpers/cache.php
Expand Up @@ -49,12 +49,28 @@ class CacheHelper extends AppHelper {
private $__match = array();

/**
* cache action time
* Parses the view file and stores content for cache file building.
*
* @var object
* @access public
* @return void
*/
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
Expand All @@ -67,8 +83,10 @@ class CacheHelper extends AppHelper {
function cache($file, $out, $cache = false) {
$cacheTime = 0;
$useCallbacks = false;
if (is_array($this->cacheAction)) {
$keys = array_keys($this->cacheAction);
$cacheAction = $this->_View->cacheAction;

if (is_array($cacheAction)) {
$keys = array_keys($cacheAction);
$index = null;

foreach ($keys as $action) {
Expand All @@ -82,12 +100,12 @@ function cache($file, $out, $cache = false) {
$index = 'index';
}

$options = $this->cacheAction;
if (isset($this->cacheAction[$index])) {
if (is_array($this->cacheAction[$index])) {
$options = array_merge(array('duration' => 0, 'callbacks' => false), $this->cacheAction[$index]);
$options = $cacheAction;
if (isset($cacheAction[$index])) {
if (is_array($cacheAction[$index])) {
$options = array_merge(array('duration' => 0, 'callbacks' => false), $cacheAction[$index]);
} else {
$cacheTime = $this->cacheAction[$index];
$cacheTime = $cacheAction[$index];
}
}
if (isset($options['duration'])) {
Expand All @@ -97,7 +115,7 @@ function cache($file, $out, $cache = false) {
$useCallbacks = $options['callbacks'];
}
} else {
$cacheTime = $this->cacheAction;
$cacheTime = $cacheAction;
}

if ($cacheTime != '' && $cacheTime > 0) {
Expand Down Expand Up @@ -213,36 +231,28 @@ function __writeFile($content, $timestamp, $useCallbacks = false) {
$cache = $cache . '.php';
$file = '<!--cachetime:' . $cacheTime . '--><?php';

if (empty($this->plugin)) {
if (empty($this->_View->plugin)) {
$file .= '
App::import(\'Controller\', \'' . $this->controllerName. '\');
App::import(\'Controller\', \'' . $this->_View->name. '\');
';
} else {
$file .= '
App::import(\'Controller\', \'' . $this->plugin . '.' . $this->controllerName. '\');
App::import(\'Controller\', \'' . $this->_View->plugin . '.' . $this->_View->name. '\');
';
}

$file .= '$controller =& new ' . $this->controllerName . 'Controller();
$controller->plugin = $this->plugin = \''.$this->plugin.'\';
$controller->helpers = $this->helpers = unserialize(\'' . serialize($this->helpers) . '\');
$controller->base = $this->base = \'' . $this->base . '\';
$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)) . '\');
$file .= '$controller = new ' . $this->_View->name . 'Controller();
$controller->plugin = $this->plugin = \'' . $this->_View->plugin . '\';
$controller->helpers = $this->helpers = unserialize(\'' . serialize($this->_View->helpers) . '\');
$controller->layout = $this->layout = \'' . $this->_View->layout. '\';
$controller->request = $this->request = unserialize(\'' . str_replace("'", "\\'", serialize($this->request)) . '\');
$controller->action = $this->action = unserialize(\'' . serialize($this->action) . '\');
$controller->data = $this->data = unserialize(\'' . str_replace("'", "\\'", serialize($this->data)) . '\');
$controller->theme = $this->theme = \'' . $this->theme . '\';
Router::setRequestInfo($this->params);';
$controller->theme = $this->theme = \'' . $this->_View->theme . '\';
Router::setRequestInfo($controller->request);';

if ($useCallbacks == true) {
$file .= '
$controller->constructClasses();
$controller->Component->initialize($controller);
$controller->beforeFilter();
$controller->Component->startup($controller);';
$controller->startupProcess();';
}

$file .= '
Expand Down
23 changes: 1 addition & 22 deletions cake/libs/view/view.php
Expand Up @@ -463,7 +463,6 @@ public function renderLayout($content_for_layout, $layout = null) {
}

$this->Helpers->trigger('afterLayout', array($layoutFileName));

return $this->output;
}

Expand Down Expand Up @@ -670,27 +669,7 @@ protected function _render($___viewFn, $___dataForView = array(), $loadHelpers =

include $___viewFn;

$out = 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;
return ob_get_clean();
}

/**
Expand Down

0 comments on commit c92ecdc

Please sign in to comment.