From b6978ab25f5b1a1db4fc544ea2c98bc4ba1b8dbd Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 14 Nov 2009 02:19:11 -0500 Subject: [PATCH] Adding the ability to load cache engine classes from plugins and app libs. Also enabled the ability for App cache classes to override core ones. Test cases added. Removed unnecessary subclassing of Object. --- cake/libs/cache.php | 44 +++++++++++++++++----------- cake/tests/cases/libs/cache.test.php | 20 +++++++++++++ 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/cake/libs/cache.php b/cake/libs/cache.php index 7744cfd62..99f5cfdd6 100644 --- a/cake/libs/cache.php +++ b/cake/libs/cache.php @@ -74,20 +74,6 @@ class Cache { 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 * @@ -146,10 +132,15 @@ class Cache { * @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(); @@ -165,6 +156,25 @@ class Cache { 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 @@ -417,7 +427,7 @@ class Cache { * @package cake * @subpackage cake.cake.libs */ -class CacheEngine extends Object { +class CacheEngine { /** * settings of current engine instance diff --git a/cake/tests/cases/libs/cache.test.php b/cake/tests/cases/libs/cache.test.php index c858a0f95..e80715629 100644 --- a/cake/tests/cases/libs/cache.test.php +++ b/cake/tests/cases/libs/cache.test.php @@ -71,6 +71,26 @@ class CacheTest extends CakeTestCase { $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 *