CachedConfig

Class
Final: Yes

Class CachedConfig.

Description

Provides a cached implementation of a configuration source. This class MUST cache the configuration output of the decorated ConfigInterface instance. It SHALL lazily initialize and retrieve cached configuration data upon invocation.

Table of Contents

Interfaces

ConfigInterface

Interface ConfigInterface.

Properties

Methods

__construct()

Constructs a CachedConfig wrapper.

 : mixed
__invoke()

Invokes the configuration and returns the cached configuration data.

 : ConfigInterface
get()

Retrieves a configuration value by key.

 : mixed
getIterator()

Retrieves an iterator for traversing the configuration data.

 : Traversable
has()

Checks for existence of a configuration key.

 : bool
remove()

Retrieves a configuration value by key.

 : mixed
set()

Sets configuration data.

 : void
toArray()

Exports the entire configuration to an array.

 : array<string|int, mixed>
getConfig()

Retrieves or initializes the configuration instance.

 : ConfigInterface
Properties

$cache

Private Read-only
private CacheInterface $cache

$cacheKey

Private
private string|null $cacheKey = null

$persistent

Private Read-only
private bool $persistent = false
Methods

__construct()

Public

Constructs a CachedConfig wrapper.

public __construct(CacheInterface  $cache, ConfigInterface  $defaultConfig[, bool  $persistent = false][, string|null  $cacheKey = null]) : mixed

Description

This constructor SHALL accept a PSR-16 cache implementation and a configuration instance to be cached. It MUST defer reading and writing the configuration until invoked.

Parameters
$cache : CacheInterface

Description

the cache implementation used for storing configuration data

$defaultConfig : ConfigInterface

Description

the configuration source to be cached

$persistent : bool = false

Description

whether the cache should be persistent or not

$cacheKey : string|null = null

Description

the cache key to use for storing the configuration data

__invoke()

Public

Invokes the configuration and returns the cached configuration data.

public __invoke() : ConfigInterface

Description

If the configuration has not yet been cached, it MUST be stored in the cache upon first invocation. This method MUST return a ConfigInterface implementation containing the cached configuration data.

Tags
throws
InvalidArgumentException

Description

if the cache key is invalid

Return values

Description

a ConfigInterface implementation containing the cached configuration data

get()

Public

Retrieves a configuration value by key.

public get( string  $key[, mixed|null  $default = null]) : mixed
Parameters
$key : string

Description

the configuration key to retrieve

$default : mixed|null = null

Description

the default value if the key is not found

Return values

Description

the value of the configuration key or the default

getIterator()

Public

Retrieves an iterator for traversing the configuration data.

public getIterator() : Traversable
Return values
Traversable

Description

an iterator over the configuration

has()

Public

Checks for existence of a configuration key.

public has( string  $key) : bool
Parameters
$key : string

Description

the configuration key to check

Return values
bool

Description

TRUE if the key exists, FALSE otherwise

remove()

Public

Retrieves a configuration value by key.

public remove( string  $key) : mixed

Description

This method MUST return the cached value if it exists, or the default value if not found.

Parameters
$key : string

Description

the configuration key to retrieve

Return values

Description

the configuration value or the default value

set()

Public

Sets configuration data.

public set( array<string|int, mixed>|ConfigInterface|string  $key[, mixed  $value = null]) : void

Description

This method MUST update the cached configuration data in the cache if the persistent flag is set to true.

Parameters
$key : array<string|int, mixed>|ConfigInterface|string

Description

the configuration key or an array of key-value pairs to set

$value : mixed = null

Description

the value to set for the specified key

Tags
throws
InvalidArgumentException

Description

if the key is invalid

toArray()

Public

Exports the entire configuration to an array.

public toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

Description

the configuration as an associative array