Changing <cake:nocache></cake:nocache> into <!--nocache--><!--/nocache--> This makes no cache tags valid html/xml at all times, and will not interfere with validation.

This commit is contained in:
mark_story 2010-11-06 01:18:23 -04:00
parent 4739d7f955
commit 4c0fd76a2e
15 changed files with 56 additions and 58 deletions

View file

@ -32,21 +32,19 @@ class CacheHelper extends AppHelper {
/**
* Array of strings replaced in cached views.
* The strings are found between <cake:nocache><cake:nocache> in views
* The strings are found between `<!--nocache--><!--/nocache-->` in views
*
* @var array
* @access private
*/
private $__replace = array();
protected $_replace = array();
/**
* Array of string that are replace with there var replace above.
* The strings are any content inside <cake:nocache><cake:nocache> and includes the tags in views
* The strings are any content inside `<!--nocache--><!--/nocache-->` and includes the tags in views
*
* @var array
* @access private
*/
private $__match = array();
protected $_match = array();
/**
* Parses the view file and stores content for cache file building.
@ -143,14 +141,14 @@ class CacheHelper extends AppHelper {
} elseif ($file = fileExistsInPath($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>)([\\s\\S]*?)(?=<\/cake:nocache>)/i', $file, $fileResult, PREG_PATTERN_ORDER);
preg_match_all('/(<!--nocache-->(?<=<!--nocache-->)[\\s\\S]*?(?=<!--\/nocache-->)<!--\/nocache-->)/i', $cache, $outputResult, PREG_PATTERN_ORDER);
preg_match_all('/(?<=<!--nocache-->)([\\s\\S]*?)(?=<!--\/nocache-->)/i', $file, $fileResult, PREG_PATTERN_ORDER);
$fileResult = $fileResult[0];
$outputResult = $outputResult[0];
if (!empty($this->__replace)) {
if (!empty($this->_replace)) {
foreach ($outputResult as $i => $element) {
$index = array_search($element, $this->__match);
$index = array_search($element, $this->_match);
if ($index !== false) {
unset($outputResult[$i]);
}
@ -162,8 +160,8 @@ class CacheHelper extends AppHelper {
$i = 0;
foreach ($fileResult as $cacheBlock) {
if (isset($outputResult[$i])) {
$this->__replace[] = $cacheBlock;
$this->__match[] = $outputResult[$i];
$this->_replace[] = $cacheBlock;
$this->_match[] = $outputResult[$i];
}
$i++;
}
@ -174,13 +172,13 @@ class CacheHelper extends AppHelper {
* Parse the output and replace cache tags
*
* @param string $cache Output to replace content in.
* @return string with all replacements made to <cake:nocache><cake:nocache>
* @return string with all replacements made to <!--nocache--><!--nocache-->
* @access private
*/
function __parseOutput($cache) {
$count = 0;
if (!empty($this->__match)) {
foreach ($this->__match as $found) {
if (!empty($this->_match)) {
foreach ($this->_match as $found) {
$original = $cache;
$length = strlen($found);
$position = 0;
@ -190,7 +188,7 @@ class CacheHelper extends AppHelper {
if ($position !== false) {
$cache = substr($original, 0, $position);
$cache .= $this->__replace[$count];
$cache .= $this->_replace[$count];
$cache .= substr($original, $position + $length);
} else {
break;

View file

@ -418,7 +418,7 @@ class View extends Object {
);
if ($isCached) {
$replace = array('<cake:nocache>', '</cake:nocache>');
$replace = array('<!--nocache-->', '<!--/nocache-->');
$this->output = str_replace($replace, '', $this->output);
}
}

View file

@ -198,7 +198,7 @@ class CacheHelperTest extends CakeTestCase {
}
/**
* test that multiple <cake:nocache> tags function with multiple nocache tags in the layout.
* test that multiple <!--nocache--> tags function with multiple nocache tags in the layout.
*
* @return void
*/

View file

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

View file

@ -1,8 +1,8 @@
<?php echo $this->element('nocache/sub2'); ?>
<cake:nocache>
<!--nocache-->
<?php $foobar = 'in sub1'; ?>
<?php echo $foobar; ?>
</cake:nocache>
<!--/nocache-->
<?php echo 'printing: "' . $foobar . '"'; ?>

View file

@ -1,6 +1,6 @@
<cake:nocache>
<!--nocache-->
<?php $barfoo = 'in sub2'; ?>
<?php echo $barfoo; ?>
</cake:nocache>
<!--/nocache-->
<?php echo 'printing: "' . $barfoo . '"'; ?>

View file

@ -2,12 +2,12 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo $title_for_layout; ?></title>
<cake:nocache><?php $x = 1; ?></cake:nocache>
<!--nocache--><?php $x = 1; ?><!--/nocache-->
</head>
<body>
<cake:nocache><?php $x++; ?></cake:nocache>
<cake:nocache><?php $x++; ?></cake:nocache>
<!--nocache--><?php $x++; ?><!--/nocache-->
<!--nocache--><?php $x++; ?><!--/nocache-->
<?php echo $content_for_layout; ?>
<cake:nocache><?php echo 'cached count is: ' . $x; ?></cake:nocache>
<!--nocache--><?php echo 'cached count is: ' . $x; ?><!--/nocache-->
</body>
</html>

View file

@ -18,15 +18,15 @@
*/
?>
<p>This is regular text</p>
<cake:nocache>
<!--nocache-->
<?php echo microtime(); ?>
</cake:nocache>
<!--/nocache-->
<?php echo $content_for_layout; ?>
<?php echo $superman; ?>
<cake:nocache>
<!--nocache-->
<?php echo $variable; ?>
</cake:nocache>
<!--/nocache-->
<p>Additional regular text.</p>

View file

@ -18,23 +18,23 @@
*/
?>
<p>This is regular text</p>
<cake:nocache>
<!--nocache-->
<p>A. Layout Before Content</p>
<?php $this->log('1. layout before content') ?>
</cake:nocache>
<cake:nocache><?php echo $this->element('nocache/plain'); ?></cake:nocache>
<cake:nocache>
<!--/nocache-->
<!--nocache--><?php echo $this->element('nocache/plain'); ?><!--/nocache-->
<!--nocache-->
<p>C. Layout After Test Element But Before Content</p>
<?php $this->log('3. layout after test element but before content') ?>
</cake:nocache>
<!--/nocache-->
<?php echo $content_for_layout; ?>
<cake:nocache>
<!--nocache-->
<p>E. Layout After Content</p>
<?php $this->log('5. layout after content') ?>
</cake:nocache>
<!--/nocache-->
<p>Additional regular text.</p>
<?php //echo $this->element('nocache/contains_nocache'); stub?>
<cake: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') ?>
</cake:nocache>
<!--/nocache-->

View file

@ -1,2 +1,2 @@
View Content
<cake:nocache><?php $y = 1; ?></cake:nocache>
<!--nocache--><?php $y = 1; ?><!--/nocache-->

View file

@ -1,5 +1,5 @@
<div class="users form">
<cake:nocache>
<!--nocache-->
<?php echo $this->Form->create('User');?>
<fieldset>
<legend><?php echo __('Add User');?></legend>
@ -10,5 +10,5 @@
?>
</fieldset>
<?php echo $this->Form->end('Submit');?>
</cake:nocache>
<!--/nocache-->
</div>

View file

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

View file

@ -1,9 +1,9 @@
<cake:nocache>
<!--nocache-->
<?php echo $foo; ?>
</cake:nocache>
<!--/nocache-->
<cake:nocache>
<!--nocache-->
<?php echo $bar; ?>
</cake:nocache>
<!--/nocache-->
<?php echo $this->element('nocache/sub1'); ?>

View file

@ -18,7 +18,7 @@
*/
?>
<h1>Content</h1>
<cake:nocache>
<!--nocache-->
<p>D. In View File</p>
<?php $this->log('4. in view file') ?>
</cake:nocache>
<!--/nocache-->

View file

@ -18,7 +18,7 @@
*/
?>
<p>
<cake:nocache>
<!--nocache-->
<span class="notice">
<?php
echo __('Your tmp directory is ');
@ -29,7 +29,7 @@
endif;
?>
</span>
</cake:nocache>
<!--/nocache-->
</p>
<p>
<span class="notice">