Skip to content

ConfigRepository

An instance-backed store of configuration directives, with typed accessors that fail loudly when a directive does not hold the shape the caller asked for.

This is where the behaviour lives; Config is a static facade over one default instance of it. Having a real object means a consumer can be handed a repository — through the container, or directly in a test — instead of reaching into process-wide state, and two configurations can exist side by side.

A directive marked read-only cannot be overwritten or removed, and survives ConfigRepository::clear() and ConfigRepository::resetWorkerState().

class ConfigRepository

Since3.2.0
SourceConfig/ConfigRepository.php

public function __construct(array<string|int, mixed> $config = []): mixed

Initial directives.

ParameterTypeDescription
$config`array<string“int, mixed>`

Returns mixed

MethodDescription
clear(): voidDrop every directive except read-only ones that still hold their read-only value.
[`fromArray(array<stringint, mixed> $data): void`](#fromarray)
[`get(stringint $name, mixed $default = null): mixed`](#get)
[`getArray(stringint $name, ?array $default = null): array`](#getarray)
[`getBool(stringint $name, bool $default = false): bool`](#getbool)
[`getFloat(stringint $name, ?float $default = null): float`](#getfloat)
[`getInt(stringint $name, ?int $default = null): int`](#getint)
[`getNullableString(stringint $name, ?string $default = null): ?string`](#getnullablestring)
[`getString(stringint $name, ?string $default = null): string`](#getstring)
[`getStringList(stringint $name, array $default = []): array<int, string>`](#getstringlist)
[`has(stringint $name): bool`](#has)
[`isReadonly(stringint $name): bool`](#isreadonly)
[`remove(stringint $name): bool`](#remove)
resetWorkerState(array<int, string> $preserveKeys = []): voidDrop request-specific directives at a worker request boundary, keeping read-only ones and anything named in $preserveKeys.
[`set(stringint $name, mixed $value, bool $overwrite = true, bool $readonly = false): bool`](#set)
[`toArray(): array<stringint, mixed>`](#toarray)

public function clear(): void

Drop every directive except read-only ones that still hold their read-only value.

Compared with strict equality on matching keys rather than array_intersect_assoc(), which stringifies values and would treat any two array-valued directives as equal.

public function fromArray(array<string|int, mixed> $data): void

Import directives, in precedence order: a read-only directive keeps its value, then the incoming data wins, then anything already set that the data does not mention.

ParameterTypeDescription
$data`array<string“int, mixed>`

public function get(string|int $name, mixed $default = null): mixed

The raw value of a directive, or $default when unset.

Prefer the typed accessors: this cannot be checked at the call site, so a wrongly-shaped value propagates silently instead of failing where it was configured.

ParameterTypeDescription
$name`string““int`
$defaultmixed

Returns mixed

public function getArray(string|int $name, ?array<mixed> $default = null): array<mixed>

ParameterTypeDescription
$name`string““int`
$default?``array``<``mixed``>

Returns array``<``mixed``>

ThrowsWhen
ConfigurationExceptionIf unset with no default, or not holding an array.

public function getBool(string|int $name, bool $default = false): bool

ParameterTypeDescription
$name`string““int`
$defaultbool

Returns bool

ThrowsWhen
ConfigurationExceptionIf the directive is set but does not hold a bool.

public function getFloat(string|int $name, ?float $default = null): float

An int value is widened to float without complaint.

ParameterTypeDescription
$name`string““int`
$default?``float

Returns float

ThrowsWhen
ConfigurationExceptionIf unset with no default, or not holding a float.

public function getInt(string|int $name, ?int $default = null): int

ParameterTypeDescription
$name`string““int`
$default?``int

Returns int

ThrowsWhen
ConfigurationExceptionIf unset with no default, or not holding an int.

public function getNullableString(string|int $name, ?string $default = null): ?string

A directive as a string, or null when it genuinely is not set.

For settings where “unconfigured” is itself meaningful, such as an absent environment override, so a missing directive is not an error.

ParameterTypeDescription
$name`string““int`
$default?``string

Returns ?``string

ThrowsWhen
ConfigurationExceptionIf the directive holds a non-scalar value.

public function getString(string|int $name, ?string $default = null): string

A directive as a string.

Scalars are cast; an array has no sensible string form and is rejected.

ParameterTypeDescription
$name`string““int`
$default?``string

Returns string

ThrowsWhen
ConfigurationExceptionIf unset with no default, or holding an array.

public function getStringList(string|int $name, array<string> $default = []): array<int, string>

A directive configurable as either a single string or an array of strings, normalized to a list.

ParameterTypeDescription
$name`string““int`
$defaultarray``<``string``>

Returns array``<``int``, ``string``>

ThrowsWhen
ConfigurationExceptionIf it holds anything other than a string or an array of scalars.

public function has(string|int $name): bool

Whether a directive with the given name is present.

A directive explicitly set to null still counts as present.

ParameterTypeDescription
$name`string““int`

Returns bool

public function isReadonly(string|int $name): bool

Whether the named directive was set read-only and can no longer be changed.

ConfigRepository::set() refuses to overwrite such a directive and reports false rather than throwing.

ParameterTypeDescription
$name`string““int`

Returns bool

public function remove(string|int $name): bool

Remove a directive, unless it is read-only.

ParameterTypeDescription
$name`string““int`

Returns bool — Whether it was removed.

public function resetWorkerState(array<int, string> $preserveKeys = []): void

Drop request-specific directives at a worker request boundary, keeping read-only ones and anything named in $preserveKeys.

ParameterTypeDescription
$preserveKeysarray``<``int``, ``string``>

public function set(string|int $name, mixed $value, bool $overwrite = true, bool $readonly = false): bool

Set a directive, unless it is read-only, or already set and $overwrite is false.

ParameterTypeDescription
$name`string““int`
$valuemixed
$overwritebool
$readonlybool

Returns bool — Whether the directive was set.

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

Returns array``<``string``|``int``, ``mixed``>