Fr3nch13/CakePHP Utilities

Configure
in package

Configuration class. Used for managing runtime configuration information.

Provides features for reading and writing to the runtime configuration, as well as methods for loading additional configuration files or storing runtime configuration for future use.

Tags
link
https://book.cakephp.org/4/en/development/configuration.html

Table of Contents

Properties

$_engines  : array<string|int, ConfigEngineInterface>
Configured engine classes, used to load config files from resources
$_hasIniSet  : bool|null
Flag to track whether ini_set exists.
$_values  : array<string, mixed>
Array of values currently stored in Configure.

Methods

check()  : bool
Returns true if given variable is set in Configure.
clear()  : void
Clear all values stored in Configure.
config()  : void
Add a new engine to Configure. Engines allow you to read configuration files in various formats/storage locations. CakePHP comes with two built-in engines PhpConfig and IniConfig. You can also implement your own engine classes in your application.
configured()  : array<string|int, string>
Gets the names of the configured Engine objects.
consume()  : array<string|int, mixed>|string|null
Used to read and delete a variable from Configure.
consumeOrFail()  : mixed
Used to consume information stored in Configure. It's not possible to store `null` values in Configure.
delete()  : void
Used to delete a variable from Configure.
drop()  : bool
Remove a configured engine. This will unset the engine and make any future attempts to use it cause an Exception.
dump()  : bool
Dump data currently in Configure into $key. The serialization format is decided by the config engine attached as $config. For example, if the 'default' adapter is a PhpConfig, the generated file will be a PHP configuration file loadable by the PhpConfig.
isConfigured()  : bool
Returns true if the Engine objects is configured.
load()  : bool
Loads stored configuration information from a resource. You can add config file resource engines with `Configure::config()`.
read()  : mixed
Used to read information stored in Configure. It's not possible to store `null` values in Configure.
readOrFail()  : mixed
Used to get information stored in Configure. It's not possible to store `null` values in Configure.
restore()  : bool
Restores configuration data stored in the Cache into configure. Restored values will overwrite existing ones.
store()  : bool
Used to write runtime configuration into Cache. Stored runtime configuration can be restored using `Configure::restore()`. These methods can be used to enable configuration managers frontends, or other GUI type interfaces for configuration.
version()  : string
Used to determine the current version of CakePHP.
write()  : void
Used to store a dynamic variable in Configure.
_getEngine()  : ConfigEngineInterface|null
Get the configured engine. Internally used by `Configure::load()` and `Configure::dump()` Will create new PhpConfig for default if not configured yet.

Properties

$_hasIniSet

Flag to track whether ini_set exists.

protected static bool|null $_hasIniSet

$_values

Array of values currently stored in Configure.

protected static array<string, mixed> $_values = ['debug' => false]

Methods

check()

Returns true if given variable is set in Configure.

public static check(string $var) : bool
Parameters
$var : string

Variable name to check for

Return values
bool

True if variable is there

clear()

Clear all values stored in Configure.

public static clear() : void

config()

Add a new engine to Configure. Engines allow you to read configuration files in various formats/storage locations. CakePHP comes with two built-in engines PhpConfig and IniConfig. You can also implement your own engine classes in your application.

public static config(string $name, ConfigEngineInterface $engine) : void

To add a new engine to Configure:

Configure::config('ini', new IniConfig());
Parameters
$name : string

The name of the engine being configured. This alias is used later to read values from a specific engine.

$engine : ConfigEngineInterface

The engine to append.

configured()

Gets the names of the configured Engine objects.

public static configured() : array<string|int, string>
Return values
array<string|int, string>

consume()

Used to read and delete a variable from Configure.

public static consume(string $var) : array<string|int, mixed>|string|null

This is primarily used during bootstrapping to move configuration data out of configure into the various other classes in CakePHP.

Parameters
$var : string

The key to read and remove.

Return values
array<string|int, mixed>|string|null

consumeOrFail()

Used to consume information stored in Configure. It's not possible to store `null` values in Configure.

public static consumeOrFail(string $var) : mixed

Acts as a wrapper around Configure::consume() and Configure::check(). The configure key/value pair consumed via this method is expected to exist. In case it does not an exception will be thrown.

Parameters
$var : string

Variable to consume. Use '.' to access array elements.

Tags
throws
RuntimeException

if the requested configuration is not set.

since
3.6.0
Return values
mixed

Value stored in configure.

drop()

Remove a configured engine. This will unset the engine and make any future attempts to use it cause an Exception.

public static drop(string $name) : bool
Parameters
$name : string

Name of the engine to drop.

Return values
bool

Success

dump()

Dump data currently in Configure into $key. The serialization format is decided by the config engine attached as $config. For example, if the 'default' adapter is a PhpConfig, the generated file will be a PHP configuration file loadable by the PhpConfig.

public static dump(string $key[, string $config = 'default' ][, array<string|int, string> $keys = [] ]) : bool

Usage

Given that the 'default' engine is an instance of PhpConfig. Save all data in Configure to the file my_config.php:

Configure::dump('my_config', 'default');

Save only the error handling configuration:

Configure::dump('error', 'default', ['Error', 'Exception'];
Parameters
$key : string

The identifier to create in the config adapter. This could be a filename or a cache key depending on the adapter being used.

$config : string = 'default'

The name of the configured adapter to dump data with.

$keys : array<string|int, string> = []

The name of the top-level keys you want to dump. This allows you save only some data stored in Configure.

Tags
throws
CakeException

if the adapter does not implement a dump method.

Return values
bool

Success

isConfigured()

Returns true if the Engine objects is configured.

public static isConfigured(string $name) : bool
Parameters
$name : string

Engine name.

Return values
bool

load()

Loads stored configuration information from a resource. You can add config file resource engines with `Configure::config()`.

public static load(string $key[, string $config = 'default' ][, bool $merge = true ]) : bool

Loaded configuration information will be merged with the current runtime configuration. You can load configuration files from plugins by preceding the filename with the plugin name.

Configure::load('Users.user', 'default')

Would load the 'user' config file using the default config engine. You can load app config files by giving the name of the resource you want loaded.

Configure::load('setup', 'default');

If using default config and no engine has been configured for it yet, one will be automatically created using PhpConfig

Parameters
$key : string

name of configuration resource to load.

$config : string = 'default'

Name of the configured engine to use to read the resource identified by $key.

$merge : bool = true

if config files should be merged instead of simply overridden

Tags
throws
CakeException

if the $config engine is not found

link
https://book.cakephp.org/4/en/development/configuration.html#reading-and-writing-configuration-files
Return values
bool

True if load successful.

read()

Used to read information stored in Configure. It's not possible to store `null` values in Configure.

public static read([string|null $var = null ][, mixed $default = null ]) : mixed

Usage:

Configure::read('Name'); will return all values for Name
Configure::read('Name.key'); will return only the value of Configure::Name[key]
Parameters
$var : string|null = null

Variable to obtain. Use '.' to access array elements.

$default : mixed = null

The return value when the configure does not exist

Tags
link
https://book.cakephp.org/4/en/development/configuration.html#reading-configuration-data
Return values
mixed

Value stored in configure, or null.

readOrFail()

Used to get information stored in Configure. It's not possible to store `null` values in Configure.

public static readOrFail(string $var) : mixed

Acts as a wrapper around Configure::read() and Configure::check(). The configure key/value pair fetched via this method is expected to exist. In case it does not an exception will be thrown.

Usage:

Configure::readOrFail('Name'); will return all values for Name
Configure::readOrFail('Name.key'); will return only the value of Configure::Name[key]
Parameters
$var : string

Variable to obtain. Use '.' to access array elements.

Tags
throws
RuntimeException

if the requested configuration is not set.

link
https://book.cakephp.org/4/en/development/configuration.html#reading-configuration-data
Return values
mixed

Value stored in configure.

restore()

Restores configuration data stored in the Cache into configure. Restored values will overwrite existing ones.

public static restore(string $name[, string $cacheConfig = 'default' ]) : bool
Parameters
$name : string

Name of the stored config file to load.

$cacheConfig : string = 'default'

Name of the Cache configuration to read from.

Tags
throws
RuntimeException
Return values
bool

Success.

store()

Used to write runtime configuration into Cache. Stored runtime configuration can be restored using `Configure::restore()`. These methods can be used to enable configuration managers frontends, or other GUI type interfaces for configuration.

public static store(string $name[, string $cacheConfig = 'default' ][, array<string|int, mixed>|null $data = null ]) : bool
Parameters
$name : string

The storage name for the saved configuration.

$cacheConfig : string = 'default'

The cache configuration to save into. Defaults to 'default'

$data : array<string|int, mixed>|null = null

Either an array of data to store, or leave empty to store all values.

Tags
throws
RuntimeException
Return values
bool

Success

version()

Used to determine the current version of CakePHP.

public static version() : string

Usage

Configure::version();
Return values
string

Current version of CakePHP

write()

Used to store a dynamic variable in Configure.

public static write(array<string, mixed>|string $config[, mixed $value = null ]) : void

Usage:

Configure::write('One.key1', 'value of the Configure::One[key1]');
Configure::write(['One.key1' => 'value of the Configure::One[key1]']);
Configure::write('One', [
    'key1' => 'value of the Configure::One[key1]',
    'key2' => 'value of the Configure::One[key2]'
]);

Configure::write([
    'One.key1' => 'value of the Configure::One[key1]',
    'One.key2' => 'value of the Configure::One[key2]'
]);
Parameters
$config : array<string, mixed>|string

The key to write, can be a dot notation value. Alternatively can be an array containing key(s) and value(s).

$value : mixed = null

Value to set for the given key.

Tags
link
https://book.cakephp.org/4/en/development/configuration.html#writing-configuration-data

_getEngine()

Get the configured engine. Internally used by `Configure::load()` and `Configure::dump()` Will create new PhpConfig for default if not configured yet.

protected static _getEngine(string $config) : ConfigEngineInterface|null
Parameters
$config : string

The name of the configured adapter

Return values
ConfigEngineInterface|null

Engine instance or null


        
On this page

Search results