Skip to content

Commit

Permalink
Adding the ability to load cache engine classes from plugins and app …
Browse files Browse the repository at this point in the history
…libs.

Also enabled the ability for App cache classes to override core ones.
Test cases added.
Removed unnecessary subclassing of Object.
  • Loading branch information
markstory committed Nov 15, 2009
1 parent 3c349dd commit b6978ab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
44 changes: 27 additions & 17 deletions cake/libs/cache.php
Expand Up @@ -74,20 +74,6 @@ function &getInstance() {
return $instance[0];
}

/**
* Tries to find and include a file for a cache engine and returns object instance
*
* @param $name Name of the engine (without 'Engine')
* @return mixed $engine object or null
* @access private
*/
function __loadEngine($name) {
if (!class_exists($name . 'Engine')) {
require LIBS . DS . 'cache' . DS . strtolower($name) . '.php';
}
return true;
}

/**
* Set the cache configuration to use
*
Expand Down Expand Up @@ -146,10 +132,15 @@ function config($name = null, $settings = array()) {
* @static
*/
function engine($name = 'File', $settings = array()) {
$cacheClass = $name . 'Engine';
$plugin = null;
$class = $name;
if (strpos($name, '.') !== false) {
list($plugin, $class) = explode('.', $name);
}
$cacheClass = $class . 'Engine';
$_this =& Cache::getInstance();
if (!isset($_this->_Engine[$name])) {
if ($_this->__loadEngine($name) === false) {
if ($_this->__loadEngine($class, $plugin) === false) {
return false;
}
$_this->_Engine[$name] =& new $cacheClass();
Expand All @@ -165,6 +156,25 @@ function engine($name = 'File', $settings = array()) {
return false;
}

/**
* Tries to find and include a file for a cache engine and returns object instance
*
* @param $name Name of the engine (without 'Engine')
* @return mixed $engine object or null
* @access private
*/
function __loadEngine($name, $plugin = null) {
if ($plugin) {
App::import('Lib', $plugin . '.cache' . DS . $name);
} else {
if (!App::import('Lib', 'cache' . DS . $name)) {
App::import('Core', 'cache' . DS . $name);
}
}
return true;
}


/**
* Temporarily change settings to current config options. if no params are passed, resets settings if needed
* Cache::write() will reset the configuration changes made
Expand Down Expand Up @@ -417,7 +427,7 @@ function settings($engine = null) {
* @package cake
* @subpackage cake.cake.libs
*/
class CacheEngine extends Object {
class CacheEngine {

/**
* settings of current engine instance
Expand Down
20 changes: 20 additions & 0 deletions cake/tests/cases/libs/cache.test.php
Expand Up @@ -71,6 +71,26 @@ function testConfig() {
$this->assertEqual($results, Cache::config('new'));
}

/**
* test configuring CacheEngines in App/libs
*
* @return void
**/
function testConfigWithLibAndPluginEngines() {
App::build(array(
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
), true);

$settings = array('engine' => 'TestAppCache', 'path' => TMP, 'prefix' => 'cake_test_');
$result = Cache::config('libEngine', $settings);
$this->assertEqual($result, Cache::config('libEngine'));

$settings = array('engine' => 'TestPlugin.TestPluginCache', 'path' => TMP, 'prefix' => 'cake_test_');
$result = Cache::config('pluginLibEngine', $settings);
$this->assertEqual($result, Cache::config('pluginLibEngine'));
}

/**
* testInvalidConfig method
*
Expand Down

0 comments on commit b6978ab

Please sign in to comment.