Skip to content

Commit

Permalink
Merge pull request #45 from creative-commoners/pulls/2.0/new-version-…
Browse files Browse the repository at this point in the history
…feed-v3

FIX: PSR-2 codebase. Formatting via phpcbf
  • Loading branch information
robbieaverill committed Dec 12, 2017
2 parents 17cf3d7 + 67e112f commit d536df0
Show file tree
Hide file tree
Showing 12 changed files with 891 additions and 859 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -4,3 +4,4 @@
/.gitignore export-ignore
/.travis.yml export-ignore
/.scrutinizer.yml export-ignore
/codecov.yml export-ignore
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -22,7 +22,6 @@ before_script:

# Install composer dependencies
- composer validate
- composer require --no-update silverstripe/recipe-core:1.0.x-dev
- if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql 2.0.x-dev; fi
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -12,7 +12,9 @@
],
"require":
{
"silverstripe/recipe-cms": "^1"
"silverstripe/cms": "^4",
"silverstripe/versioned": "^1",
"silverstripe/siteconfig": "^4"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
@@ -1,4 +1,4 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<phpunit bootstrap="vendor/silverstripe/cms/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/</directory>
</testsuite>
Expand Down
60 changes: 30 additions & 30 deletions src/Filters/CachedContentFilter.php
Expand Up @@ -2,39 +2,39 @@

namespace SilverStripe\VersionFeed\Filters;


use SilverStripe\Core\Config\Config;




/**
* Caches results of a callback
*/
class CachedContentFilter extends ContentFilter {

/**
* Enable caching
*
* @config
* @var boolean
*/
private static $cache_enabled = true;

public function getContent($key, $callback) {
$cache = $this->getCache();

// Return cached value if available
$cacheEnabled = Config::inst()->get(get_class(), 'cache_enabled');
$result = (isset($_GET['flush']) || !$cacheEnabled)
? null
: $cache->get($key);
if($result) return $result;

// Fallback to generate result
$result = parent::getContent($key, $callback);
$lifetime = Config::inst()->get(ContentFilter::class, 'cache_lifetime') ?: null;
$cache->set($key, $result, $lifetime);
return $result;
}
class CachedContentFilter extends ContentFilter
{

/**
* Enable caching
*
* @config
* @var boolean
*/
private static $cache_enabled = true;

public function getContent($key, $callback)
{
$cache = $this->getCache();

// Return cached value if available
$cacheEnabled = Config::inst()->get(get_class(), 'cache_enabled');
$result = (isset($_GET['flush']) || !$cacheEnabled)
? null
: $cache->get($key);
if ($result) {
return $result;
}

// Fallback to generate result
$result = parent::getContent($key, $callback);
$lifetime = Config::inst()->get(ContentFilter::class, 'cache_lifetime') ?: null;
$cache->set($key, $result, $lifetime);
return $result;
}
}
98 changes: 50 additions & 48 deletions src/Filters/ContentFilter.php
Expand Up @@ -7,58 +7,60 @@
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Injector\Injector;



/**
* Conditionally executes a given callback, attempting to return the desired results
* of its execution.
*/
abstract class ContentFilter {
abstract class ContentFilter
{

use Configurable;
/**
* Nested content filter
*
* @var ContentFilter
*/
protected $nestedContentFilter;
use Configurable;
/**
* Nested content filter
*
* @var ContentFilter
*/
protected $nestedContentFilter;

/**
* Cache lifetime
*
* @config
* @var int
*/
private static $cache_lifetime = 300;

public function __construct($nestedContentFilter = null) {
$this->nestedContentFilter = $nestedContentFilter;
}

/**
* Gets the cache to use
*
* @return Zend_Cache_Frontend
*/
protected function getCache() {
return Injector::inst()->get(
CacheInterface::class . '.VersionFeedController'
);
}

/**
* 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);
}
}
/**
* Cache lifetime
*
* @config
* @var int
*/
private static $cache_lifetime = 300;

public function __construct($nestedContentFilter = null)
{
$this->nestedContentFilter = $nestedContentFilter;
}

/**
* Gets the cache to use
*
* @return Zend_Cache_Frontend
*/
protected function getCache()
{
return Injector::inst()->get(
CacheInterface::class . '.VersionFeedController'
);
}

/**
* 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);
}
}
}

0 comments on commit d536df0

Please sign in to comment.