Navigation Menu

Skip to content

Commit

Permalink
Expand abilities of Cache::config()
Browse files Browse the repository at this point in the history
Allow various types of configuration to be provided. The new variants
allow for simpler dependency injection and still afford configuration
files.

Reconfiguring an adapter is now not allowed, as the configuration
changes were never propagated to the adapter. This results in a silent
failure that is hard to detect.

Move/remove tests around to make sense with the new API's
  • Loading branch information
markstory committed Aug 17, 2013
1 parent 89f2855 commit f074fd8
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 147 deletions.
60 changes: 46 additions & 14 deletions lib/Cake/Cache/Cache.php
Expand Up @@ -125,25 +125,61 @@ class Cache {

/**
* This method can be used to define cache adapters for an application
* during the bootstrapping process. You can use this method to add new cache adapters
* at runtime as well. New cache configurations will be constructed upon the next write.
* or read existing configuration.
*
* To change an adapter's configuration at runtime, first drop the adapter and then
* reconfigure it.
*
* Adapters will not be constructed until the first operation is done.
*
* ### Usage
*
* Reading config data back:
*
* `Cache::config('default');`
*
* Setting a cache engine up.
*
* `Cache::config('default', $settings);`
*
* Injecting a constructed adapter in:
*
* `Cache::config('default', $instance);`
*
* Using a factory function to get an adapter:
*
* `Cache::config('default', function () { return new FileEngine(); });`
*
* Configure multiple adapters at once:
*
* `Cache::config($arrayOfConfig);`
*
* @param string|array $key The name of the cache config, or an array of multiple configs.
* @param array $config An array of name => config data for adapter.
* @return void
* @return mixed null when adding configuration and an array of configuration data when reading.
* @throws Cake\Error\Exception When trying to modify an existing config.
*/
public static function config($key, $config = null) {
if ($config !== null && is_string($key)) {
static::$_config[$key] = $config;
// Read config.
if ($config === null && is_string($key)) {
return isset(static::$_config[$key]) ? static::$_config[$key] : null;
}
if ($config === null && is_array($key)) {
foreach ($key as $name => $settings) {
static::config($name, $settings);
}
return;
}

static::$_config = array_merge(static::$_config, $key);
if (isset(static::$_config[$key])) {
throw new Error\Exception(__d('cake_dev', 'Cannot reconfigure existing adapter "%s"', $key));
}
if (is_object($config)) {
$config = ['className' => $config];
}
if (isset($config['engine']) && empty($config['className'])) {
$config['className'] = $config['engine'];
}
static::$_config[$key] = $config;
}

/**
Expand All @@ -156,13 +192,11 @@ protected static function _buildEngine($name) {
if (empty(static::$_registry)) {
static::$_registry = new CacheRegistry();
}
if (empty(static::$_config[$name]['engine'])) {
if (empty(static::$_config[$name]['className'])) {
throw new Error\Exception(__d('cake_dev', 'The "%s" cache configuration does not exist.', $name));
}

$config = static::$_config[$name];
$config['className'] = $config['engine'];

static::$_registry->load($name, $config);

if (!empty($config['groups'])) {
Expand Down Expand Up @@ -193,11 +227,9 @@ public static function configured() {
* @return boolean success of the removal, returns false when the config does not exist.
*/
public static function drop($config) {
if (!isset(static::$_registry->{$config})) {
return false;
if (isset(static::$_registry->{$config})) {
static::$_registry->unload($config);
}

static::$_registry->unload($config);
unset(static::$_config[$config], static::$_restore[$config]);
return true;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/Cake/Cache/CacheRegistry.php
Expand Up @@ -65,11 +65,14 @@ protected function _throwMissingClassError($class, $plugin) {
* the correct interface.
*/
protected function _create($class, $settings) {
if (is_object($class)) {
$isObject = is_object($class);
if ($isObject && $class instanceof \Closure) {
$instance = $class();
} elseif ($isObject) {
$instance = $class;
}

unset($settings['engine'], $settings['className']);
unset($settings['className']);
if (!isset($instance)) {
$instance = new $class($settings);
}
Expand Down

0 comments on commit f074fd8

Please sign in to comment.