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
composer require quillstack/configUsage
A configuration class
Extend Config and say what it holds:
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
use Quillstack\Config\ConfigProviderInterface;
final class ConfigProvider implements ConfigProviderInterface
{
public function load(): array
{
return [
'aws' => AwsConfig::class,
'mail' => MailConfig::class,
];
}
}Reading
$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 eitherThe 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
| Class | What it is |
|---|---|
Config | what a configuration class extends; holds protected array $config |
Configuration | reads a value out of whichever class the key names |
ConfigInterface | get(string $key, mixed $default = null): mixed |
ConfigProviderInterface | load(): 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
composer test
composer test:coverage
composer stanDocker
docker-compose up -d
docker exec -w /var/www/html -it quillstack_config shLicense
MIT. See LICENSE.