Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
127 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters