Skip to content

Commit

Permalink
Make full page caching + Themes work.
Browse files Browse the repository at this point in the history
Dispatcher now uses ThemeView for rendering cache files.
This enables nocache blocks to reference theme elements.
Fixes #1858
  • Loading branch information
markstory committed Sep 17, 2011
1 parent 85b86cb commit 0e5797d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/Cake/Routing/Dispatcher.php
Expand Up @@ -218,8 +218,10 @@ public function cached($path) {
}

if (file_exists($filename)) {
App::uses('ThemeView', 'View');

$controller = null;
$view = new View($controller);
$view = new ThemeView($controller);
return $view->renderCache($filename, microtime(true));
}
}
Expand Down
48 changes: 48 additions & 0 deletions lib/Cake/Test/Case/Routing/DispatcherTest.php
Expand Up @@ -442,6 +442,15 @@ public function cache_form() {
$this->cacheAction = 10;
$this->helpers[] = 'Form';
}

/**
* Test cached views with themes.
*/
public function themed() {
$this->cacheAction = 10;
$this->viewClass = 'Theme';
$this->theme = 'TestTheme';
}
}

/**
Expand Down Expand Up @@ -1424,6 +1433,45 @@ public function testFullPageCachingDispatch() {
unlink($filename);
}

/**
* Test full page caching with themes.
*
* @return void
*/
public function testFullPageCachingWithThemes() {
Configure::write('Cache.disable', false);
Configure::write('Cache.check', true);
Configure::write('debug', 2);

Router::reload();
Router::connect('/:controller/:action/*');

App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
), true);

$dispatcher = new TestDispatcher();
$request = new CakeRequest('/test_cached_pages/themed');
$response = new CakeResponse();

ob_start();
$dispatcher->dispatch($request, $response);
$out = ob_get_clean();

ob_start();
$dispatcher->cached($request->here);
$cached = ob_get_clean();

$result = str_replace(array("\t", "\r\n", "\n"), "", $out);
$cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
$expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);

$this->assertEqual($expected, $result);

$filename = $this->__cachePath($request->here);
unlink($filename);
}

/**
* testHttpMethodOverrides method
*
Expand Down
@@ -1 +1,2 @@
default test_theme layout
default test_theme layout
<?php echo $content_for_layout ?>
4 changes: 4 additions & 0 deletions lib/Cake/Test/test_app/View/Themed/TestTheme/Posts/themed.ctp
@@ -0,0 +1,4 @@
posts themed themed file.
<!--nocache-->
<?php echo $this->element('test_element'); ?>
<!--/nocache-->
4 changes: 3 additions & 1 deletion lib/Cake/View/ThemeView.php
Expand Up @@ -38,7 +38,9 @@ class ThemeView extends View {
*/
public function __construct($controller) {
parent::__construct($controller);
$this->theme = $controller->theme;
if ($controller) {
$this->theme = $controller->theme;
}
}

/**
Expand Down

0 comments on commit 0e5797d

Please sign in to comment.