2014-04-17 07:44:38 +02:00
|
|
|
<?php
|
|
|
|
|
2014-04-30 02:31:18 +02:00
|
|
|
namespace VersionFeed\Filters;
|
|
|
|
|
2014-04-17 07:44:38 +02:00
|
|
|
/**
|
|
|
|
* Conditionally executes a given callback, attempting to return the desired results
|
|
|
|
* of its execution.
|
|
|
|
*/
|
|
|
|
abstract class ContentFilter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nested content filter
|
|
|
|
*
|
|
|
|
* @var ContentFilter
|
|
|
|
*/
|
|
|
|
protected $nestedContentFilter;
|
2015-10-30 06:08:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache lifetime
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $cache_lifetime = 300;
|
2014-04-17 07:44:38 +02:00
|
|
|
|
|
|
|
public function __construct($nestedContentFilter = null) {
|
|
|
|
$this->nestedContentFilter = $nestedContentFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the cache to use
|
|
|
|
*
|
|
|
|
* @return Zend_Cache_Frontend
|
|
|
|
*/
|
|
|
|
protected function getCache() {
|
2014-04-30 02:31:18 +02:00
|
|
|
$cache = \SS_Cache::factory('VersionFeed_Controller');
|
2014-04-17 07:44:38 +02:00
|
|
|
$cache->setOption('automatic_serialization', true);
|
2015-10-30 06:08:00 +01:00
|
|
|
|
|
|
|
// Map 0 to null for unlimited lifetime
|
|
|
|
$lifetime = \Config::inst()->get(get_class($this), 'cache_lifetime') ?: null;
|
|
|
|
$cache->setLifetime($lifetime);
|
2014-04-17 07:44:38 +02:00
|
|
|
return $cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates the result of the given callback
|
|
|
|
*
|
|
|
|
* @param string $key Unique key for this
|
|
|
|
* @param callable $callback Callback for evaluating the content
|
|
|
|
* @return mixed Result of $callback()
|
|
|
|
*/
|
|
|
|
public function getContent($key, $callback) {
|
|
|
|
if($this->nestedContentFilter) {
|
|
|
|
return $this->nestedContentFilter->getContent($key, $callback);
|
|
|
|
} else {
|
|
|
|
return call_user_func($callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|