Configuration objects
- Created
- Decided
- Versions
- History
Abstract
I will give the panel a configuration component in which each configuration is an object of a class unique to it,
registered against a module and a name, held in an immutable catalogue and injected by its
class. Environment variables will be available during bootstrap through a static
Env class, read from the process environment or from an env file, with typed accessors that cast their values.
Motivation
The panel needs a configuration system to configure the engine's components, and to provide additional configuration for modules.
Proposal
One class for each configuration
Each configuration will be a configuration object: an instance of a class unique to that configuration, as ADR-0005 decides. Its values will be typed properties, and it may contain child objects, but the configuration itself will be contained within its class. Each configuration will be registered against a module and a name, and its class will map to exactly one module and name. The engine's own configuration will be registered against a module like any other.
Every configuration object will implement ConfigObject, which requires __set_state(). PHP calls __set_state()
when it restores an object exported with var_export(), so a configuration object can be written to a cache and read
back from it.
final readonly class MailConfig implements ConfigObject
{
public function __construct(
public string $host,
public int $port,
) {}
public static function __set_state(array $data): static
{
return new static($data['host'], $data['port']);
}
}
Finding a configuration
ConfigCatalogue will hold every configuration object and will be immutable. It
will be constructed with the objects keyed by module and then by name, and will build a map
from each object's class to its module and name as it is constructed. Nothing will collect registrations: the registry
that seals into the catalogue arrives with RFC-0004.
A configuration will be found in one of two ways. get(string $module, string $config): ?ConfigObject will return the
object registered under a module and name, and has() will say whether there is one.
for(string $class): ?ConfigObject will return the object of a given class. Both lookups will return null when there
is nothing to find.
$mail = $catalogue->get('engine', 'mail');
$mail = $catalogue->for(MailConfig::class);
Injection
Because each configuration object has a class of its own, a component will receive the configuration it needs by its type. The configuration component will bind every configuration object into the container as a shared instance under its own class name, so that a parameter typed as a configuration class resolves to that object through an ordinary binding, as described in RFC-0001.
public function __construct(
private MailConfig $mail,
) {}
Environment variables
Env will hold environment variables for the length of bootstrap, as
ADR-0006 decides. It will be a static singleton.
At the start of bootstrap, Env will be initialised once, from one of two sources: the $_ENV superglobal, through
Env::createFromSuperglobal(), or the .env file in a directory, through Env::createFromFile(string $path). Values
read from a file will be held by Env alone and never written into $_ENV. Initialising it a second time will throw.
While configuration is built, values will be read from it, and reading before it has been initialised will throw.
get() will return a variable's value, or a default when the variable is missing or null, and has() will say
whether a variable is present, including when its value is null. Four typed accessors will read a value as a
particular type. Each takes the key and an optional default of that type, which is null unless one is given. It
returns the default when the variable is missing or null, so an accessor called without a default returns null, and
it throws when the value cannot be cast:
| Accessor | Casts |
|---|---|
string() |
A string, number or boolean. |
int() |
An integer, numeric string or boolean. |
float() |
A float, numeric string or boolean. |
bool() |
A boolean or integer. The strings true, 1 and yes are true, and false, 0 and no are false. Any other string throws. |
Env::createFromFile('/path/to/directory');
$port = Env::int('MAIL_PORT', 25);
$debug = Env::bool('APP_DEBUG', false);
Env::destroy();
Once bootstrap is over, Env::destroy() will remove the instance, and with it any values read from a file. Nothing will
read environment variables directly after that.
Errors
Every exception the component throws will implement ConfigException, a marker contract. Reading Env before it has
been initialised, or initialising it a second time, will throw EnvInitialisationException. A typed accessor whose
value cannot be cast to its type will throw InvalidEnvException.
Out of scope
- Loading configuration. Reading configuration files and building the catalogue from them belongs to bootstrap.
- Caching. Writing configuration objects to a cache and restoring them from it belongs to whatever loads configuration.
Alternatives considered
This design rests on ADR-0005, which holds configuration in typed objects, and ADR-0006, which reads environment variables only during bootstrap.
Backwards compatibility
Nothing breaks. No configuration component exists before this change.
Open questions
Changelog
Sources
- Issue #22, Engine - Config, and not edited since: the motivation, configuration objects registered
against a name and a module, environment variables from
$_ENVor a local env file, and injection because each configuration is a unique class. This iscreated. - PR #25, feat(config): Add config component, merged and squashed as 180794a: the implementation,
whose merge is
decided.ConfigObject,ConfigCatalogue,Envand the exceptions are described fromsrc/Configat 180794a andtests/Unit/Configat 180794a. - Loading configuration belonging to bootstrap, the config component binding each configuration object into the
container as a shared instance under its class name, every typed
Envaccessor throwing when a value cannot be cast, andEnvexisting only during bootstrap: first written down on . The code at 180794a differs on three of these: nothing binds configuration objects into the container,Env::string()returns the default instead of throwing, and nothing confinesEnvto bootstrap. It also defines aMissingEnvVariableExceptionthat is not part of the design. - The catalogue being described as immutable rather than as the sealed half of a registry pattern: corrected on . PR #25's commits show the class arrived as a configuration registry and was renamed to a catalogue, and neither a registry nor a seal exists at 180794a. Both the registry and the pattern in ADR-0003, decided on , postdate this design.
Source rfc/0002-configuration-objects.md · commit d6910e4 · exported