Fixing CacheHelper and multiple cake:nocache tags in a view file, breaking cake:nocache following $content_for_layout. Fixes #136

This commit is contained in:
mark_story 2009-10-01 01:03:47 -04:00
parent 347c175b85
commit f12cbdba38
3 changed files with 58 additions and 12 deletions

View file

@ -156,27 +156,30 @@ class CacheHelper extends AppHelper {
} elseif ($file = fileExistsInPath($file)) { } elseif ($file = fileExistsInPath($file)) {
$file = file_get_contents($file); $file = file_get_contents($file);
} }
preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
preg_match_all('/(<cake:nocache>(?<=<cake:nocache>)[\\s\\S]*?(?=<\/cake:nocache>)<\/cake:nocache>)/i', $cache, $oresult, PREG_PATTERN_ORDER); preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER);
preg_match_all('/(?<=<cake:nocache>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $result, PREG_PATTERN_ORDER); $fileResult = $fileResult[0];
$outputResult = $outputResult[0];
if (!empty($this->__replace)) { if (!empty($this->__replace)) {
foreach ($oresult['0'] as $k => $element) { foreach ($outputResult as $i => $element) {
$index = array_search($element, $this->__match); $index = array_search($element, $this->__match);
if ($index !== false) { if ($index !== false) {
array_splice($oresult[0], $k, 1); unset($outputResult[$i]);
} }
} }
$outputResult = array_values($outputResult);
} }
if (!empty($result['0'])) {
$count = 0; if (!empty($fileResult)) {
foreach ($result['0'] as $block) { $i = 0;
if (isset($oresult['0'][$count])) { foreach ($fileResult as $cacheBlock) {
$this->__replace[] = $block; if (isset($outputResult[$i])) {
$this->__match[] = $oresult['0'][$count]; $this->__replace[] = $cacheBlock;
$this->__match[] = $outputResult[$i];
} }
$count++; $i++;
} }
} }
} }

View file

@ -62,6 +62,8 @@ class CacheTestController extends Controller {
$this->layout = 'cache_layout'; $this->layout = 'cache_layout';
$this->set('variable', 'variableValue'); $this->set('variable', 'variableValue');
$this->set('superman', 'clark kent'); $this->set('superman', 'clark kent');
$this->set('batman', 'bruce wayne');
$this->set('spiderman', 'peter parker');
} }
} }
/** /**
@ -162,9 +164,35 @@ class CacheHelperTest extends CakeTestCase {
$this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents); $this->assertPattern('/if \(is_writable\(TMP\)\)\:/', $contents);
$this->assertPattern('/php echo \$variable/', $contents); $this->assertPattern('/php echo \$variable/', $contents);
$this->assertPattern('/php echo microtime()/', $contents); $this->assertPattern('/php echo microtime()/', $contents);
$this->assertNoPattern('/cake:nocache/', $contents);
@unlink($filename); @unlink($filename);
} }
/**
* test that multiple <cake:nocache> tags function with multiple nocache tags in the layout.
*
* @return void
**/
function testMultipleNoCacheTagsInViewfile() {
$this->Controller->cache_parsing();
$this->Controller->cacheAction = 21600;
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';
$View = new View($this->Controller);
$result = $View->render('multiple_nocache');
$this->assertNoPattern('/cake:nocache/', $result);
$this->assertNoPattern('/php echo/', $result);
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
$this->assertTrue(file_exists($filename));
$contents = file_get_contents($filename);
$this->assertNoPattern('/cake:nocache/', $contents);
@unlink($filename);
}
/** /**
* testComplexNoCache method * testComplexNoCache method
* *

View file

@ -0,0 +1,15 @@
--view start--
<cake:nocache>
<?php echo $batman ?>
</cake:nocache>
this view has 3 nocache blocks
<cake:nocache>
<?php echo $spiderman; ?>
</cake:nocache>
<cake:nocache>
<?php echo 'some string'; ?>
</cake:nocache>
--view end--