The Game Panel Design record
The Game Panel
RFC-0002 Accepted

Configuration objects

Comparing
v1 to v2
Current
Read the document

Abstract

A I will give the panel a configuration component in which each configuration is an instance object of a class unique to it, , registered against a module a module and a name, held in an immutable catalogue and injected by its its class. Environment variables are available variables will be available during bootstrap through a static static Env class, read from the process environment or from an env file, with typed typed accessors that cast their values.

Motivation

The panel needs a configuration system to configure the engine's components, and to provide additional provide additional configuration for modulesmodules.

Proposal

ConceptsOne class for each configuration

Term Meaning
Configuration object An instance of a class unique to one configuration, holding that configuration's values.
Module The owner a configuration is registered against. The engine's own configuration is registered against a module like any other.
Configuration name The name a configuration is registered under within its module.
Configuration catalogue The immutable collection of every configuration object, looked up by module and name, or by class.
Environment variable A value supplied through the process environment or an env file, read during bootstrap.

Components

Component Responsibility
ConfigObject Contract every configuration object implements.
ConfigCatalogue Immutable. Holds the configuration objects and looks them up by module and name, or by class.
Env Static. Holds the environment variables during bootstrap and reads them, with typed accessors.
ConfigException Marker contract implemented by every exception the component throws.

Configuration objects

Each configuration is an instance of a class unique to that configuration, per ADR-0005. Its values are typed properties, and it may contain child objects, but the configuration itself is contained within its class. Each configuration is registered against a module and a name, and its class maps to exactly one module and name.

A configuration object implements ConfigObject, which requires __set_state(). PHP calls __set_state() when an object exported with var_export() is restored, so configuration objects can be written to a cache and read back.

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']);
    }
}

CatalogueFinding a configuration

ConfigCatalogue is will hold every configuration object and will be immutable. It is It will be constructed with every configuration object, the objects keyed by module module and then by name, , and builds will build a map map from each object's class to its module and name as it is constructed. Nothing collects will collect registrations: the registry registry that seals into it the catalogue arrives with RFC-0004.

Method Effect
get(string $module, string $config): ?ConfigObject Returns the configuration object registered under the module and name, or null.
has(string $module, string $config): bool Returns whether a configuration object is registered under the module and name.
for(string $class): ?ConfigObject Returns the configuration object of the given class, or null.

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 object has a class of its own, a component receives will receive the configuration it needs by its type. . The config configuration component binds will bind every configuration object into into the container as a shared instance under its own class class name, so that a parameter typed as a configuration class is resolved resolves to that object through an ordinary binding, as as described in RFC-0001.

public function __construct(
    private MailConfig $mail,
) {}

Environment variables

Env holds will hold environment variables variables for the length of bootstrap, per as ADR-0006 decides. It is will be a static singleton, initialised once, from one of two sources:.

Method Effect
Env::createFromSuperglobal() Initialises Env with the values in the $_ENV superglobal.
Env::createFromFile(string $path) Initialises Env with the values parsed from the .env file in the directory at the path. The file's values are held by Env alone and are not written into $_ENV.
Env::destroy() Removes the instance, and with it the values read from an env file.

Initialising Env a second time throwsWhile configuration is built, values will be read from it, and so does reading from it before it has been initialised. Once bootstrap initialised will throw. get() will return a variable's value, or a default when the variable is overmissing or null, and Envhas() will say whether a variable is destroyed present, including when its value is null. Four typed accessors will read a value as a particular type. Each takes the key and nothing reads environment variables directly.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:

Values are read with these accessors. Each typed accessor returns the default when the variable is missing or null, and throws when the value cannot be cast to its type.

Accessor Behaviour
get(string $key, $default = null) Returns the value, or the default when the variable is missing or null.
has(string $key) Returns whether the variable is present, including when its value is null.
string(string $key, ?string $default = null) Casts a string, number or boolean to a string.
int(string $key, ?int $default = null) Casts an integer, numeric string or boolean to an integer.
float(string $key, ?float $default = null) Casts a float, numeric string or boolean to a float.
bool(string $key, ?bool $default = null) Casts a boolean or integer to a boolean. The strings true, 1 and yes are true, and false, 0 and no are false. Any other string throws.

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.

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 implements 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.

Exception Thrown when
EnvInitialisationException Env is read before it has been initialised, or initialised a second time.
InvalidEnvException A typed accessor's value cannot be cast to its type.

Out of scope

  • Loading configuration. Reading configuration files and building the catalogue from them belongs to bootstrap.
  • Caching. Writing configuration objects objects to a cache and restoring them from from it belongs to whatever loads loads configuration.

Alternatives considered

The decisions this This design rests on are recorded separatelyADR-0005, with the alternatives each one rejected:which holds configuration in typed objects, and ADR-0006, which reads environment variables only during bootstrap.

  • holding configuration in typed objects, in ADR-0005
  • reading environment variables only during bootstrap, in ADR-0006

No other alternatives were weighed.

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 $_ENV or a local env file, and injection because each configuration is a unique class. This is created.
  • PR #25, feat(config): Add config component, merged and squashed as 180794a: the implementation, whose merge is decided. ConfigObject, ConfigCatalogue, Env and the exceptions are described from src/Config at 180794a and tests/Unit/Config at 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 Env accessor throwing when a value cannot be cast, and Env existing 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 confines Env to bootstrap. It also defines a MissingEnvVariableException that 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 9444a99 · published