Skip to content
Permalink
Browse files

Add ability to directly inject an engine instance.

This can be helpful when doing testing, or if you have a CacheEngine
that requires setup that is not feasible with the standard process.
  • Loading branch information...
markstory committed Sep 3, 2012
1 parent 4f325a7 commit 83b4e2577104731a851d864df7d4c8de57ab62cc
Showing with 24 additions and 7 deletions.
  1. +12 −6 lib/Cake/Cache/Cache.php
  2. +12 −1 lib/Cake/Test/TestCase/Cache/CacheTest.php
@@ -156,11 +156,12 @@ protected static function _buildEngine($name) {
if (!is_subclass_of($cacheClass, 'Cake\Cache\CacheEngine')) {
throw new Error\CacheException(__d('cake_dev', 'Cache engines must use Cake\Cache\CacheEngine as a base class.'));
}
static::$_engines[$name] = new $cacheClass();
if (static::$_engines[$name]->init($config)) {
if (static::$_engines[$name]->settings['probability'] && time() % static::$_engines[$name]->settings['probability'] === 0) {
static::$_engines[$name]->gc();
$engine = new $cacheClass();
if ($engine->init($config)) {
if ($engine->settings['probability'] && time() % $engine->settings['probability'] === 0) {
$engine->gc();
}
static::$_engines[$name] = $engine;
return true;
}
return false;
@@ -204,16 +205,18 @@ public static function drop($config) {
* triggered.
*
* @param string $config The configuration name you want an engine.
* @param Cake\Cache\CacheEngine $engine An engine instance if you are manually
* injecting a cache engine.
* @return Cake\Cache\Engine
*/
public static function engine($config) {
public static function engine($config, CacheEngine $engine = null) {
if (Configure::read('Cache.disable')) {
return false;
}
if (isset(static::$_engines[$config])) {
return static::$_engines[$config];
}
if (!static::_buildEngine($config)) {
if (!$engine && !static::_buildEngine($config, $engine)) {
$message = __d(
'cake_dev',
'The "%s" cache configuration does not exist, nor could configuration be found at "Cache.%s".',
@@ -222,6 +225,9 @@ public static function engine($config) {
);
trigger_error($message, E_USER_WARNING);
}
if ($engine) {
static::$_engines[$config] = $engine;
}
return static::$_engines[$config];
}
@@ -154,11 +154,22 @@ public function testDecrementNonExistingConfig() {
public function testAttemptingToConfigureANonCacheEngineClass() {
$this->getMock('\StdClass', array(), array(), 'RubbishEngine');
Configure::write('Cache.wrong', array(
'engine' => __NAMESPACE__ . '\Rubbish'
'engine' => '\RubbishEngine'
));
Cache::engine('wrong');
}
/**
* Test that engine() can be used to inject instances.
*
* @return void
*/
public function testSetEngineValid() {
$engine = $this->getMockForAbstractClass('\Cake\Cache\CacheEngine');
Cache::engine('test', $engine);
$this->assertSame($engine, Cache::engine('test'));
}
/**
* test that calling config() sets the 'default' configuration up.
*

0 comments on commit 83b4e25

Please sign in to comment.
You can’t perform that action at this time.