Skip to content

Config

composer require quillstack/config

The package to organise a configuration in your application.

Configuration is written as classes rather than arrays in files, so a value has a place it is declared, a type, and something to jump to. Reading one is a single string: aws.token.current names the class first and the key inside it after.

Requirements

  • PHP 8.1 or newer

Installation

shell
composer require quillstack/config

Usage

A configuration class

Extend Config and say what it holds:

php
use Quillstack\Config\Config;

final class AwsConfig extends Config
{
    protected array $config = [
        'region' => 'eu-central-1',
        'token' => [
            'current' => 'abc',
            'previous' => 'xyz',
        ],
    ];
}

Saying which classes there are

php
use Quillstack\Config\ConfigProviderInterface;

final class ConfigProvider implements ConfigProviderInterface
{
    public function load(): array
    {
        return [
            'aws' => AwsConfig::class,
            'mail' => MailConfig::class,
        ];
    }
}

Reading

php
$configuration->get('aws.region');            // 'eu-central-1'
$configuration->get('aws.token.current');     // 'abc'
$configuration->get('aws.token.missing');     // null
$configuration->get('aws.retries', 3);        // 3 — nothing there, so the default
$configuration->get('nothing.at.all', 'x');   // 'x' — no such class either

The first part names the class the provider listed; the rest walks into it, however deep it goes. Nothing found is the default rather than a failure, because configuration is read in places which have something better to do than catch.

Technical documentation

ClassWhat it is
Configwhat a configuration class extends; holds protected array $config
Configurationreads a value out of whichever class the key names
ConfigInterfaceget(string $key, mixed $default = null): mixed
ConfigProviderInterfaceload(): array — the classes, keyed by the name they are addressed as

Config::DELIMITER is the dot. Config::get() is final: a configuration class says what it holds, not how it is read.

The classes are built through the container, so a configuration which needs something — an environment reader, a secret store — asks for it in the usual way.

Unit tests

shell
composer test
composer test:coverage
composer stan

Docker

shell
docker-compose up -d
docker exec -w /var/www/html -it quillstack_config sh

License

MIT. See LICENSE.

Released under the MIT License.