Permalink
Browse files

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.
  • Loading branch information...
1 parent 3c349dd commit b6978ab25f5b1a1db4fc544ea2c98bc4ba1b8dbd @markstory markstory committed Nov 14, 2009
Showing with 47 additions and 17 deletions.
  1. +27 −17 cake/libs/cache.php
  2. +20 −0 cake/tests/cases/libs/cache.test.php
View
@@ -75,20 +75,6 @@ function &getInstance() {
}
/**
- * 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
*
* @see app/config/core.php for configuration settings
@@ -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();
@@ -166,6 +157,25 @@ function engine($name = 'File', $settings = array()) {
}
/**
+ * 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 @@ function settings($engine = null) {
* @package cake
* @subpackage cake.cake.libs
*/
-class CacheEngine extends Object {
+class CacheEngine {
/**
* settings of current engine instance
@@ -72,6 +72,26 @@ function testConfig() {
}
/**
+ * 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
*
* Test that the cache class doesn't cause fatal errors with a partial path

0 comments on commit b6978ab

Please sign in to comment.