Trait StaticConfigTrait

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 $_dsnClassMap property.

Direct Users

Namespace: Cake\Core
Location: Core/StaticConfigTrait.php

Properties summary

  • $_config protected static
    array
    Configuration sets.

Method Summary

  • config() public static

    This method can be used to define configuration adapters for an application or read existing configuration.

  • configured() public static
    Returns an array containing the named configurations
  • drop() public static
    Drops a constructed adapter.
  • dsnClassMap() public static
    Returns or updates the DSN class map for this class.
  • getConfig() public static
    Reads existing configuration.
  • getDsnClassMap() public static
    Returns the DSN class map for this class.
  • parseDsn() public static
    Parses a DSN into a valid connection configuration
  • setConfig() public static
    This method can be used to define configuration adapters for an application.
  • setDsnClassMap() public static
    Updates the DSN class map for this class.

Method Detail

config()source public static

config( string|array $key , array|null $config 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.

Parameters

string|array $key
The name of the configuration, or an array of multiple configs.
array|null $config optional null
An array of name => configuration data for adapter.

Returns

array|null
Null when adding configuration or an array of configuration data when reading.

Throws

BadMethodCallException
When trying to modify an existing config.

configured()source public static

configured( )

Returns an array containing the named configurations

Returns

array
Array of configurations.

drop()source public static

drop( string $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.

Parameters

string $config
An existing configuration you wish to remove.

Returns

boolean
Success of the removal, returns false when the config does not exist.

dsnClassMap()source public static

dsnClassMap( array $map null )

Returns or updates the DSN class map for this class.

Deprecated

3.4.0 Use setDsnClassMap()/getDsnClassMap() instead.

Parameters

array $map optional null
Additions/edits to the class map to apply.

Returns

array

getConfig()source public static

getConfig( string $key )

Reads existing configuration.

Parameters

string $key
The name of the configuration.

Returns

array|null
Array of configuration data.

getDsnClassMap()source public static

getDsnClassMap( )

Returns the DSN class map for this class.

Returns

array

parseDsn()source public static

parseDsn( string $dsn )

Parses a DSN into a valid connection configuration

This method allows setting a DSN using formatting similar to that used by PEAR::DB. The following is an example of its usage:

$dsn = 'mysql://user:pass@localhost/database?';
$config = ConnectionManager::parseDsn($dsn);

$dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS';
$config = Log::parseDsn($dsn);

$dsn = 'smtp://user:secret@localhost:25?timeout=30&client=null&tls=null';
$config = Email::parseDsn($dsn);

$dsn = 'file:///?className=\My\Cache\Engine\FileEngine';
$config = Cache::parseDsn($dsn);

$dsn = 'File://?prefix=myapp_cake_core_&serialize=true&duration=+2 minutes&path=/tmp/persistent/';
$config = Cache::parseDsn($dsn);

For all classes, the value of scheme is set as the value of both the className unless they have been otherwise specified.

Note that querystring arguments are also parsed and set as values in the returned configuration.

Parameters

string $dsn
The DSN string to convert to a configuration array

Returns

array
The configuration array to be stored after parsing the DSN

Throws

InvalidArgumentException
If not passed a string, or passed an invalid string

setConfig()source public static

setConfig( string|array $key , array $config null )

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.

Adapters will not be constructed until the first operation is done.

Usage

Assuming that the class' name is Cache the following scenarios are supported:

Setting a cache engine up.

Cache::setConfig('default', $settings);

Injecting a constructed adapter in:

Cache::setConfig('default', $instance);

Configure multiple adapters at once:

Cache::setConfig($arrayOfConfig);

Parameters

string|array $key
The name of the configuration, or an array of multiple configs.
array $config optional null
An array of name => configuration data for adapter.

Throws

BadMethodCallException
When trying to modify an existing config.
LogicException
When trying to store an invalid structured config array.

setDsnClassMap()source public static

setDsnClassMap( array $map )

Updates the DSN class map for this class.

Parameters

array $map
Additions/edits to the class map to apply.

Properties detail

$_configsource

protected static array

Configuration sets.

[]

© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Core.StaticConfigTrait.html