Skip to content

Commit

Permalink
Add ability to directly inject an engine instance.
Browse files Browse the repository at this point in the history
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 18, 2012
1 parent 4f325a7 commit 83b4e25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
18 changes: 12 additions & 6 deletions lib/Cake/Cache/Cache.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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".',
Expand All @@ -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];
}

Expand Down
13 changes: 12 additions & 1 deletion lib/Cake/Test/TestCase/Cache/CacheTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 83b4e25

Please sign in to comment.