|
|
@@ -16,6 +16,7 @@ namespace Cake\Core;
|
|
|
|
|
|
use BadMethodCallException;
|
|
|
use InvalidArgumentException;
|
|
|
+use LogicException;
|
|
|
|
|
|
/**
|
|
|
* A trait that provides a set of static methods to manage configuration
|
|
|
@@ -35,8 +36,7 @@ trait StaticConfigTrait
|
|
|
protected static $_config = [];
|
|
|
|
|
|
/**
|
|
|
- * This method can be used to define configuration adapters for an application
|
|
|
- * or read existing configuration.
|
|
|
+ * This method can be used to define configuration adapters for an application.
|
|
|
*
|
|
|
* To change an adapter's configuration at runtime, first drop the adapter and then
|
|
|
* reconfigure it.
|
|
|
@@ -48,50 +48,41 @@ trait StaticConfigTrait
|
|
|
* 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);
|
|
|
+ * Cache::setConfig('default', $settings);
|
|
|
* ```
|
|
|
*
|
|
|
* Injecting a constructed adapter in:
|
|
|
*
|
|
|
* ```
|
|
|
- * Cache::config('default', $instance);
|
|
|
+ * Cache::setConfig('default', $instance);
|
|
|
* ```
|
|
|
*
|
|
|
* Configure multiple adapters at once:
|
|
|
*
|
|
|
* ```
|
|
|
- * Cache::config($arrayOfConfig);
|
|
|
+ * Cache::setConfig($arrayOfConfig);
|
|
|
* ```
|
|
|
*
|
|
|
* @param string|array $key The name of the configuration, or an array of multiple configs.
|
|
|
- * @param array|null $config An array of name => configuration data for adapter.
|
|
|
- * @return array|null Null when adding configuration or an array of configuration data when reading.
|
|
|
+ * @param array $config An array of name => configuration data for adapter.
|
|
|
* @throws \BadMethodCallException When trying to modify an existing config.
|
|
|
+ * @throws \LogicException When trying to store an invalid structured config array.
|
|
|
+ * @return void
|
|
|
*/
|
|
|
- public static function config($key, $config = null)
|
|
|
+ public static function setConfig($key, $config = null)
|
|
|
{
|
|
|
if ($config === null) {
|
|
|
- // Read config.
|
|
|
- if (is_string($key)) {
|
|
|
- return isset(static::$_config[$key]) ? static::$_config[$key] : null;
|
|
|
+ if (!is_array($key)) {
|
|
|
+ throw new LogicException('If config is null, key must be an array.');
|
|
|
}
|
|
|
-
|
|
|
- if (is_array($key)) {
|
|
|
- foreach ($key as $name => $settings) {
|
|
|
- static::config($name, $settings);
|
|
|
- }
|
|
|
-
|
|
|
- return;
|
|
|
+ foreach ($key as $name => $settings) {
|
|
|
+ static::setConfig($name, $settings);
|
|
|
}
|
|
|
+
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
if (isset(static::$_config[$key])) {
|
|
|
@@ -116,6 +107,72 @@ trait StaticConfigTrait
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Reads existing configuration.
|
|
|
+ *
|
|
|
+ * @param string $key The name of the configuration.
|
|
|
+ * @return array|null Array of configuration data.
|
|
|
+ */
|
|
|
+ public static function getConfig($key)
|
|
|
+ {
|
|
|
+ return isset(static::$_config[$key]) ? static::$_config[$key] : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * This method can be used to define configuration 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);
|
|
|
+ * ```
|
|
|
+ *
|
|
|
+ * @deprecated 3.4.0 Use setConfig()/getConfig() instead.
|
|
|
+ * @param string|array $key The name of the configuration, or an array of multiple configs.
|
|
|
+ * @param array|null $config An array of name => configuration data for adapter.
|
|
|
+ * @return array|null Null when adding configuration or an array of configuration data when reading.
|
|
|
+ * @throws \BadMethodCallException When trying to modify an existing config.
|
|
|
+ */
|
|
|
+ public static function config($key, $config = null)
|
|
|
+ {
|
|
|
+ if ($config !== null || !is_string($key)) {
|
|
|
+ static::setConfig($key, $config);
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return static::getConfig($key);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* Drops a constructed adapter.
|
|
|
*
|
|
|
* If you wish to modify an existing configuration, you should drop it,
|
|
|
@@ -247,17 +304,39 @@ trait StaticConfigTrait
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Returns or updates the DSN class map for this class
|
|
|
+ * Updates the DSN class map for this class.
|
|
|
+ *
|
|
|
+ * @param array $map Additions/edits to the class map to apply.
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public static function setDsnClassMap(array $map)
|
|
|
+ {
|
|
|
+ static::$_dsnClassMap = $map + static::$_dsnClassMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns the DSN class map for this class.
|
|
|
*
|
|
|
- * @param array|null $map Additions/edits to the class map to apply
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public static function getDsnClassMap()
|
|
|
+ {
|
|
|
+ return static::$_dsnClassMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns or updates the DSN class map for this class.
|
|
|
+ *
|
|
|
+ * @deprecated 3.4.0 Use setDsnClassMap()/getDsnClassMap() instead.
|
|
|
+ * @param array|null $map Additions/edits to the class map to apply.
|
|
|
* @return array
|
|
|
*/
|
|
|
public static function dsnClassMap(array $map = null)
|
|
|
{
|
|
|
if ($map !== null) {
|
|
|
- static::$_dsnClassMap = $map + static::$_dsnClassMap;
|
|
|
+ static::setDsnClassMap($map);
|
|
|
}
|
|
|
|
|
|
- return static::$_dsnClassMap;
|
|
|
+ return static::getDsnClassMap();
|
|
|
}
|
|
|
}
|