Make CacheHelper able to handle nocache tags in elements.

Using afterRenderFile() instead of afterRender() allows
CacheHelper to parse content in elements and extended views.
This commit is contained in:
mark_story 2011-10-08 22:04:53 -04:00
parent fb20841d25
commit bcb8444ab1
4 changed files with 12 additions and 13 deletions

View file

@ -251,20 +251,20 @@ class CacheHelperTest extends CakeTestCase {
$result = $View->render('sequencial_nocache');
$this->assertNotRegExp('/cake:nocache/', $result);
$this->assertNotRegExp('/php echo/', $result);
$this->assertNotRegExpy('/php echo/', $result);
$this->assertRegExp('/A\. Layout Before Content/', $result);
$this->assertRegExp('/B\. In Plain Element/', $result);
$this->assertRegExp('/C\. Layout After Test Element/', $result);
$this->assertRegExp('/D\. In View File/', $result);
$this->assertRegExp('/E\. Layout After Content/', $result);
//$this->assertRegExp('/F\. In Element With No Cache Tags/', $result);
$this->assertRegExp('/F\. In Element With No Cache Tags/', $result);
$this->assertRegExp('/G\. Layout After Content And After Element With No Cache Tags/', $result);
$this->assertNotRegExp('/1\. layout before content/', $result);
$this->assertNotRegExp('/2\. in plain element/', $result);
$this->assertNotRegExp('/3\. layout after test element/', $result);
$this->assertNotRegExp('/4\. in view file/', $result);
$this->assertNotRegExp('/5\. layout after content/', $result);
//$this->assertNotRegExp('/6\. in element with no cache tags/', $result);
$this->assertNotRegExp('/6\. in element with no cache tags/', $result);
$this->assertNotRegExp('/7\. layout after content and after element with no cache tags/', $result);
$filename = CACHE . 'views' . DS . 'cachetest_cache_complex.php';
@ -277,14 +277,14 @@ class CacheHelperTest extends CakeTestCase {
$this->assertRegExp('/C\. Layout After Test Element/', $contents);
$this->assertRegExp('/D\. In View File/', $contents);
$this->assertRegExp('/E\. Layout After Content/', $contents);
//$this->assertRegExp('/F\. In Element With No Cache Tags/', $contents);
$this->assertRegExp('/F\. In Element With No Cache Tags/', $contents);
$this->assertRegExp('/G\. Layout After Content And After Element With No Cache Tags/', $contents);
$this->assertRegExp('/1\. layout before content/', $contents);
$this->assertNotRegExp('/2\. in plain element/', $contents);
$this->assertRegExp('/3\. layout after test element/', $contents);
$this->assertRegExp('/4\. in view file/', $contents);
$this->assertRegExp('/5\. layout after content/', $contents);
//$this->assertRegExp('/6\. in element with no cache tags/', $contents);
$this->assertRegExp('/6\. in element with no cache tags/', $contents);
$this->assertRegExp('/7\. layout after content and after element with no cache tags/', $contents);
}
@ -551,10 +551,10 @@ class CacheHelperTest extends CakeTestCase {
$Cache = $this->getMock('CacheHelper', array('_parseContent'), array($View));
$Cache->expects($this->once())
->method('_parseContent')
->with('posts/index', $View->output)
->with('posts/index', 'content')
->will($this->returnValue(''));
$Cache->afterRender('posts/index');
$Cache->afterRenderFile('posts/index', 'content');
Configure::write('Cache.check', false);
$Cache->afterRender('posts/index');

View file

@ -1,5 +1,5 @@
<h2>Cache Me</h2>
<!--nocache-->
<p>F. In Element With No Cache Tags</p>
<?php $this->log('6. In element with no cache tags') ?>
<?php $this->log('6. in element with no cache tags') ?>
<!--/nocache-->

View file

@ -32,7 +32,7 @@
<?php $this->log('5. layout after content') ?>
<!--/nocache-->
<p>Additional regular text.</p>
<?php //echo $this->element('nocache/contains_nocache'); stub?>
<?php echo $this->element('nocache/contains_nocache'); ?>
<!--nocache-->
<p>G. Layout After Content And After Element With No Cache Tags</p>
<?php $this->log('7. layout after content and after element with no cache tags') ?>

View file

@ -68,9 +68,9 @@ class CacheHelper extends AppHelper {
* @param string $viewFile
* @return void
*/
public function afterRender($viewFile) {
public function afterRenderFile($viewFile, $output) {
if ($this->_enabled()) {
$this->_View->output = $this->_parseContent($viewFile, $this->_View->output);
return $this->_parseContent($viewFile, $output);
}
}
@ -82,8 +82,7 @@ class CacheHelper extends AppHelper {
*/
public function afterLayout($layoutFile) {
if ($this->_enabled()) {
$output = $this->_parseContent($layoutFile, $this->_View->output);
$this->_View->output = $this->cache($layoutFile, $output);
$this->_View->output = $this->cache($layoutFile, $this->_View->output);
}
$this->_View->output = preg_replace('/<!--\/?nocache-->/', '', $this->_View->output);
}