Config Contracts
This page covers the behavior other code can rely on when using Quantum\Config\Config.
Load contract
$config->load($setup);- expects a
Quantum\Config\Setup - loads one PHP file resolved from the
Setup - stores the returned array as the root config payload
- does nothing if the store has already been loaded once
Use load() when you control the whole store shape.
Import contract
$config->import($setup);- expects a
Quantum\Config\Setup - reads the setup filename and uses it as the top-level key
- stores the loaded payload as
[$fileName => $data] - throws
ConfigExceptionif that truthy top-level key already exists
Because the imported data is always namespaced by filename, the usual access pattern is:
config()->import(new Setup('config', 'database'));
$driver = config()->get('database.default');Filename caveat
The package only performs collision checks when the setup filename is truthy.
In practice, treat a non-empty filename as required for import(). It gives you predictable access paths and the intended collision guard.
Optional import contract
$config->importIfExists($setup): bool- returns
falseand changes nothing when the file cannot be resolved - otherwise behaves like
import(), including the collision check, and returnstrue
Read contract
get(string $key, $default = null)
- returns the stored value when the key exists
- returns the provided default when the key is missing
Keys use dot notation because the backing store is a dot-accessible data container.
has(string $key): bool
- returns
truewhen the key exists in the current store - returns
falsewhen the store is empty or the key is missing
all(): ?Data
- returns the whole data container
- returns
nullbefore anything has been loaded, imported, or set
Typed reader contract
getBool() and getInt() read through get(). For how to use them, see Reading booleans and numbers.
public function getBool(string $key, bool $default = false): bool;
public function getInt(string $key, int $default = 0): int;- a missing key, or a key whose value is
null, returns the default getBool()returnstruefortrue,1,onandyes, ignoring letter case, andfalsefor every other value, includingfalse,0,off,noand an empty valuegetInt()returns the number for a whole number or whole-number text such as587, and throwsConfigException::invalidValue()for any other value, such asabc,1.5or an empty valueget()is not affected and keeps returning the raw value
The methods belong to the Config class. ConfigInterface does not declare them, so code that depends on the interface alone cannot call them.
Write contract
set(string $key, $value): void
- creates the store if needed
- writes or overwrites the value at the given key
delete(string $key): void
- removes the key when it exists
- does nothing when the store is empty or the key is missing
flush(): void
- clears the entire in-memory store
- makes
all()returnnullagain - allows a later
load()call to run again
Failure behavior
- a missing file in
load()orimport()raisesConfigExceptionwith theSetupexception message (by defaultFile `<pathPrefix>/<fileName>` not found!) - duplicate imported section names raise
ConfigException getInt()raisesConfigException::invalidValue()for a value that is not a whole number- helper resolution can fail if DI registration or resolution fails
