mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Doing some internal re-factoring in CacheHelper.
cache() should only do one thing. It shouldn't do more work than is necessary each time its called. Flatten some of the conditions by extracting methods.
This commit is contained in:
parent
a8f0eb2b49
commit
fb20841d25
2 changed files with 31 additions and 21 deletions
|
@ -548,9 +548,10 @@ class CacheHelperTest extends CakeTestCase {
|
|||
$View->cacheAction = '+1 day';
|
||||
$View->output = 'test';
|
||||
|
||||
$Cache = $this->getMock('CacheHelper', array('cache'), array($View));
|
||||
$Cache->expects($this->once())->method('cache')
|
||||
->with('posts/index', $View->output, false)
|
||||
$Cache = $this->getMock('CacheHelper', array('_parseContent'), array($View));
|
||||
$Cache->expects($this->once())
|
||||
->method('_parseContent')
|
||||
->with('posts/index', $View->output)
|
||||
->will($this->returnValue(''));
|
||||
|
||||
$Cache->afterRender('posts/index');
|
||||
|
@ -575,8 +576,9 @@ class CacheHelperTest extends CakeTestCase {
|
|||
$View->output = 'test';
|
||||
|
||||
$Cache = $this->getMock('CacheHelper', array('cache'), array($View));
|
||||
$Cache->expects($this->once())->method('cache')
|
||||
->with('posts/index', $View->output, true)
|
||||
$Cache->expects($this->once())
|
||||
->method('cache')
|
||||
->with('posts/index', $View->output)
|
||||
->will($this->returnValue(''));
|
||||
|
||||
$Cache->afterLayout('posts/index');
|
||||
|
|
|
@ -70,7 +70,7 @@ class CacheHelper extends AppHelper {
|
|||
*/
|
||||
public function afterRender($viewFile) {
|
||||
if ($this->_enabled()) {
|
||||
$this->_View->output = $this->cache($viewFile, $this->_View->output, false);
|
||||
$this->_View->output = $this->_parseContent($viewFile, $this->_View->output);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,21 +82,36 @@ class CacheHelper extends AppHelper {
|
|||
*/
|
||||
public function afterLayout($layoutFile) {
|
||||
if ($this->_enabled()) {
|
||||
$this->_View->output = $this->cache($layoutFile, $this->_View->output, true);
|
||||
$output = $this->_parseContent($layoutFile, $this->_View->output);
|
||||
$this->_View->output = $this->cache($layoutFile, $output);
|
||||
}
|
||||
$this->_View->output = preg_replace('/<!--\/?nocache-->/', '', $this->_View->output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a file + output. Matches nocache tags between the current output and the current file
|
||||
* stores a reference of the file, so the generated can be swapped back with the file contents when
|
||||
* writing the cache file.
|
||||
*
|
||||
* @param string $file The filename to process.
|
||||
* @param string $out The output for the file.
|
||||
* @return string Updated content.
|
||||
*/
|
||||
protected function _parseContent($file, $out) {
|
||||
$out = preg_replace_callback('/<!--nocache-->/', array($this, '_replaceSection'), $out);
|
||||
$this->_parseFile($file, $out);
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method used to cache a view
|
||||
*
|
||||
* @param string $file File to cache
|
||||
* @param string $out output to cache
|
||||
* @param boolean $cache Whether or not a cache file should be written.
|
||||
* @return string view output
|
||||
* @return string view ouput
|
||||
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html
|
||||
*/
|
||||
public function cache($file, $out, $cache = false) {
|
||||
public function cache($file, $out) {
|
||||
$cacheTime = 0;
|
||||
$useCallbacks = false;
|
||||
$cacheAction = $this->_View->cacheAction;
|
||||
|
@ -135,18 +150,11 @@ class CacheHelper extends AppHelper {
|
|||
}
|
||||
|
||||
if ($cacheTime != '' && $cacheTime > 0) {
|
||||
$out = preg_replace_callback('/<!--nocache-->/', array($this, '_replaceSection'), $out);
|
||||
|
||||
$this->_parseFile($file, $out);
|
||||
if ($cache === true) {
|
||||
$cached = $this->_parseOutput($out);
|
||||
$this->_writeFile($cached, $cacheTime, $useCallbacks);
|
||||
$out = $this->_stripTags($out);
|
||||
}
|
||||
return $out;
|
||||
} else {
|
||||
return $out;
|
||||
$cached = $this->_parseOutput($out);
|
||||
$this->_writeFile($cached, $cacheTime, $useCallbacks);
|
||||
$out = $this->_stripTags($out);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue