Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Tweak in SSViewer::flush #3549

Merged
merged 2 commits into from Oct 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/control/FlushRequestFilterTest.php
@@ -0,0 +1,26 @@
<?php

class FlushRequestFilterTest extends FunctionalTest {

/**
* Assert that classes that implement flushable are called
*/
public function testImplementorsAreCalled() {
$this->assertFalse(FlushRequestFilterTest_Flushable::$flushed);

$this->get('?flush=1');

$this->assertTrue(FlushRequestFilterTest_Flushable::$flushed);
}

}

class FlushRequestFilterTest_Flushable implements Flushable, TestOnly {

public static $flushed = false;

public static function flush() {
self::$flushed = true;
}

}
9 changes: 8 additions & 1 deletion tests/view/SSViewerCacheBlockTest.php
Expand Up @@ -128,10 +128,17 @@ public function testBlocksCache() {
* Test that the cacheblocks invalidate when a flush occurs.
*/
public function testBlocksInvalidateOnFlush() {
Director::test('/');
Director::test('/?flush=1');
$this->_reset(true);

// Generate cached value for foo = 1
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 1)), '1');

// Test without flush
Director::test('/');
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 3)), '1');

// Test with flush
Director::test('/?flush=1');
$this->assertEquals($this->_runtemplate('<% cached %>$Foo<% end_cached %>', array('Foo' => 2)), '2');
}
Expand Down
18 changes: 12 additions & 6 deletions view/SSViewer.php
Expand Up @@ -652,8 +652,8 @@ public static function get_source_file_comments() {
* Triggered early in the request when someone requests a flush.
*/
public static function flush() {
self::flush_template_cache();
self::flush_cacheblock_cache();
self::flush_template_cache(true);
self::flush_cacheblock_cache(true);
}

/**
Expand Down Expand Up @@ -923,9 +923,12 @@ public static function getTemplateFileByType($identifier, $type) {
* Clears all parsed template files in the cache folder.
*
* Can only be called once per request (there may be multiple SSViewer instances).
*
* @param bool $force Set this to true to force a re-flush. If left to false, flushing
* may only be performed once a request.
*/
public static function flush_template_cache() {
if (!self::$template_cache_flushed) {
public static function flush_template_cache($force = false) {
if (!self::$template_cache_flushed || $force) {
$dir = dir(TEMP_FOLDER);
while (false !== ($file = $dir->read())) {
if (strstr($file, '.cache')) unlink(TEMP_FOLDER . '/' . $file);
Expand All @@ -938,9 +941,12 @@ public static function flush_template_cache() {
* Clears all partial cache blocks.
*
* Can only be called once per request (there may be multiple SSViewer instances).
*
* @param bool $force Set this to true to force a re-flush. If left to false, flushing
* may only be performed once a request.
*/
public static function flush_cacheblock_cache() {
if (!self::$cacheblock_cache_flushed) {
public static function flush_cacheblock_cache($force = false) {
if (!self::$cacheblock_cache_flushed || $force) {
$cache = SS_Cache::factory('cacheblock');
$tags = $cache->getTags();
$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, $tags);
Expand Down