Skip to content

Commit

Permalink
Extract StaticConfigTrait.
Browse files Browse the repository at this point in the history
Extract a trait for the config(),drop(),configured() duck type used in
a number of classes. Implement the trait in Cache. This also removes the
Closure factory function. It will not be usuable by Email, and I think
it provides questionable value right now. If it becomes useful in the
future it can always be re-added.
  • Loading branch information
markstory committed Aug 26, 2013
1 parent 94a2e73 commit 42ee554
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 96 deletions.
90 changes: 3 additions & 87 deletions lib/Cake/Cache/Cache.php
Expand Up @@ -9,14 +9,14 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Cache
* @since CakePHP(tm) v 1.2.0.4933
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Cache;

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\StaticConfigTrait;
use Cake\Error;
use Cake\Utility\Inflector;

Expand Down Expand Up @@ -82,6 +82,8 @@
*/
class Cache {

use StaticConfigTrait;

/**
* Flag for tracking whether or not caching is enabled.
*
Expand Down Expand Up @@ -120,66 +122,6 @@ class Cache {
*/
protected static $_registry;

/**
* This method can be used to define cache adapters for an application
* 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 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) {
// 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;
}
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'];
unset($config['engine']);
}
static::$_config[$key] = $config;
}

/**
* Finds and builds the instance of the required engine class.
*
Expand All @@ -206,32 +148,6 @@ protected static function _buildEngine($name) {
}
}

/**
* Returns an array containing the configured Cache engines.
*
* @return array Array of configured Cache config names.
*/
public static function configured() {
return array_keys(static::$_config);
}

/**
* Drops a constructed cache engine.
*
* If you wish to re-configure a cache engine you should drop it,
* change configuration and then re-use it.
*
* @param string $config A currently configured cache config you wish to remove.
* @return boolean success of the removal, returns false when the config does not exist.
*/
public static function drop($config) {
if (isset(static::$_registry->{$config})) {
static::$_registry->unload($config);
}
unset(static::$_config[$config]);
return true;
}

/**
* Fetch the engine attached to a specific configuration name.
*
Expand Down
7 changes: 2 additions & 5 deletions lib/Cake/Cache/CacheRegistry.php
Expand Up @@ -58,17 +58,14 @@ protected function _throwMissingClassError($class, $plugin) {
* Create the cache engine instance.
*
* Part of the template method for Cake\Utility\ObjectRegistry::load()
* @param string|CacheEngine|Closure $class The classname or object to make, or a closure factory.
* @param string|CacheEngine $class The classname or object to make, or a closure factory.
* @param array $settings An array of settings to use for the cache engine.
* @return CacheEngine The constructed CacheEngine class.
* @throws Cake\Error\Exception when an object doesn't implement
* the correct interface.
*/
protected function _create($class, $settings) {
$isObject = is_object($class);
if ($isObject && $class instanceof \Closure) {
$instance = $class();
} elseif ($isObject) {
if (is_object($class)) {
$instance = $class;
}

Expand Down
121 changes: 121 additions & 0 deletions lib/Cake/Core/StaticConfigTrait.php
@@ -0,0 +1,121 @@
<?php
/**
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Core;

use Cake\Error;

/**
* A trait that provides a set of static methods to manage configuration
* for classes that provide an adapter facade or need to have sets of
* configuration data registered and manipulated.
*
* Implementing objects are expected to declare a static `$_config` property.
*/
trait StaticConfigTrait {

/**
* This method can be used to define confguration adapters for an application
* 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
*
* Assuming that the class' name is `Cache` the following scenarios
* are supported:
*
* 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);`
*
* Configure multiple adapters at once:
*
* `Cache::config($arrayOfConfig);`
*
* @param string|array $key The name of the configuration, or an array of multiple configs.
* @param array $config An array of name => configuration data for adapter.
* @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) {
// 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;
}
if (isset(static::$_config[$key])) {
throw new Error\Exception(__d('cake_dev', 'Cannot reconfigure existing key "%s"', $key));
}
if (is_object($config)) {
$config = ['className' => $config];
}
if (isset($config['engine']) && empty($config['className'])) {
$config['className'] = $config['engine'];
unset($config['engine']);
}
static::$_config[$key] = $config;
}

/**
* Drops a constructed adapter.
*
* If you wish to modify an existing configuration, you should drop it,
* change configuration and then re-add it.
*
* If the implementing objects supports a `$_registry` object the named configuration
* will also be unloaded from the registry.
*
* @param string $config An existing configuation you wish to remove.
* @return boolean success of the removal, returns false when the config does not exist.
*/
public static function drop($config) {
if (!isset(static::$_config[$config])) {
return false;
}
if (isset(static::$_registry->{$config})) {
static::$_registry->unload($config);
}
unset(static::$_config[$config]);
return true;
}

/**
* Returns an array containing the named configurations
*
* @return array Array of configurations.
*/
public static function configured() {
return array_keys(static::$_config);
}

}
5 changes: 1 addition & 4 deletions lib/Cake/Test/TestCase/Cache/CacheTest.php
Expand Up @@ -147,9 +147,6 @@ public static function configProvider() {
'prefix' => 'cake_test_'
]],
'Direct instance' => [new FileEngine()],
'Closure factory' => [function () {
return new FileEngine();
}],
];
}

Expand Down Expand Up @@ -308,7 +305,7 @@ public function testDrop() {
Configure::write('App.namespace', 'TestApp');

$result = Cache::drop('some_config_that_does_not_exist');
$this->assertTrue($result, 'Drop should succeed.');
$this->assertFalse($result, 'Drop should not succeed when config is missing.');

Cache::config('unconfigTest', [
'engine' => 'TestAppCache'
Expand Down

0 comments on commit 42ee554

Please sign in to comment.