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

View file

@ -418,7 +418,7 @@ class View extends Object {
); );
if ($isCached) { if ($isCached) {
$replace = array('<cake:nocache>', '</cake:nocache>'); $replace = array('<!--nocache-->', '<!--/nocache-->');
$this->output = str_replace($replace, '', $this->output); $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 * @return void
*/ */

View file

@ -1,5 +1,5 @@
<h2>Cache Me</h2> <h2>Cache Me</h2>
<cake:nocache> <!--nocache-->
<p>F. In Element With No Cache Tags</p> <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') ?>
</cake:nocache> <!--/nocache-->

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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