forked from silverstripe/silverstripe-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Provide a consistent way of triggering flush
Provides an interface for classes to implement their own flush() functionality. This function gets called early in a request on all implementations of Flushable when flush=1|all is requested in the URL. This fix came out of an issue where Requirements combined files were not being cleaned up after dev/build?flush=1, due to the fact that flush would only occur when you called it while on a page that used those combined files, but not in any other contexts. This will now call flush on any implementors of Flushable regardless of the context of where flush was called.
- Loading branch information
Sean Harvey
committed
Aug 21, 2014
1 parent
66bacc6
commit 2b316e7
Showing
15 changed files
with
244 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ Injector: | |
RequestProcessor: | ||
properties: | ||
filters: | ||
- '%$FlushRequestFilter' | ||
- '%$VersionedRequestFilter' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
/** | ||
* Triggers a call to flush() on all implementors of Flushable. | ||
* | ||
* @package framework | ||
* @subpackage control | ||
*/ | ||
class FlushRequestFilter implements RequestFilter { | ||
|
||
public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model) { | ||
if($request->getVar('flush')) { | ||
foreach(ClassInfo::implementorsOf('Flushable') as $class) { | ||
$class::flush(); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model) { | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,4 +45,4 @@ public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, | |
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* Provides an interface for classes to implement their own flushing functionality | ||
* whenever flush=1 is requested. | ||
* | ||
* @package framework | ||
* @subpackage core | ||
*/ | ||
interface Flushable { | ||
|
||
/** | ||
* This function is triggered early in the request if the "flush" query | ||
* parameter has been set. Each class that implements Flushable implements | ||
* this function which looks after it's own specific flushing functionality. | ||
* | ||
* @see FlushRequestFilter | ||
*/ | ||
public static function flush(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Flushable | ||
|
||
## Introduction | ||
|
||
Allows a class to define it's own flush functionality, which is triggered when `flush=1` is requested in the URL. | ||
|
||
`[api:FlushRequestFilter]` is run before a request is made, calling `flush()` statically on all | ||
implementors of `[api:Flushable]`. | ||
|
||
## Usage | ||
|
||
To use this API, you need to make your class implement `[api:Flushable]`, and define a `flush()` static function, | ||
this defines the actions that need to be executed on a flush request. | ||
|
||
### Using with SS_Cache | ||
|
||
This example uses `[api:SS_Cache]` in some custom code, and the same cache is cleaned on flush: | ||
|
||
:::php | ||
<?php | ||
class MyClass extends DataObject implements Flushable { | ||
|
||
public static function flush() { | ||
SS_Cache::factory('mycache')->clean(Zend_Cache::CLEANING_MODE_ALL); | ||
} | ||
|
||
public function MyCachedContent() { | ||
$cache = SS_Cache::factory('mycache') | ||
$something = $cache->get('mykey'); | ||
if(!$something) { | ||
$something = 'value to be cached'; | ||
$cache->save($something); | ||
} | ||
return $something; | ||
} | ||
|
||
} | ||
|
||
### Using with filesystem | ||
|
||
Another example, some temporary files are created in a directory in assets, and are deleted on flush. This would be | ||
useful in an example like `GD` or `Imagick` generating resampled images, but we want to delete any cached images on | ||
flush so they are re-created on demand. | ||
|
||
:::php | ||
<?php | ||
class MyClass extends DataObject implements Flushable { | ||
|
||
public static function flush() { | ||
foreach(glob(ASSETS_PATH . '/_tempfiles/*.jpg') as $file) { | ||
unlink($file); | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.