Yet Another Config Lib

GitHub GitHub last commit CI Workflow GitHub code size in bytes GitHub Issues Total Downloads Monthly Downloads

Installation

Install via Composer:

composer require derafu/config

Basic Usage

use Derafu\Config\Configuration; // Or `Options`.

$configuration = new Configuration([
    'app' => [
        'name' => 'Derafu Config',
    ],
]);

echo $configuration['app']['name'];           // "Derafu Config"
echo $configuration['app.name'];              // "Derafu Config"
echo $configuration->get('app.name');         // "Derafu Config"
echo $configuration->get('app')->get('name'); // "Derafu Config"

Configuration and Options are the same container, a Vault of derafu/container: they differ only in the interface they implement (ConfigurationInterface and OptionsInterface), so a class can ask for one or the other.

Typed Readers

get() returns mixed and wraps nested arrays as configuration containers. Use getReader() to read raw stored data with explicit types. Required reads reject missing/null values; optional reads return their default. A present value of the wrong type throws.

$configuration->getReader()->requireString('app.name'); // "Derafu Config"
$configuration->getReader()->getInt('app.port', 8080);  // 8080: the key is not defined.
$configuration->getReader()->getBool('app.debug');      // null: the key is not defined.

$app = $configuration->requireContainer('app'); // A Configuration with the data of `app`.
$app->getReader()->requireString('name');               // "Derafu Config"

get('app') and getContainer('app') both give access to the nested settings, but getContainer() always returns a container (of the same class) and getReader()->getArray() reads an array directly. See Typed Readers for the reader API, explicit conversions and child-container operations.

A reader is an immutable snapshot of the resolved data, including schema defaults and normalizers. Obtain a new reader after changing the container; an existing reader retains its prior array values. Objects inside the data are not cloned.

Reader validation uses the Array exception contract. Child-container creation keeps Container’s exceptions. These APIs are inherited; Config adds no separate reader or conversion rules.

Schema and Validation

The second argument is a schema. It says the types, the defaults, the required settings and the accepted values, and the container keeps its data valid: a change that breaks the schema throws and leaves the container as it was.

use Derafu\Config\Options;

$options = new Options(
    ['strategy' => 'template.pdf'],
    [
        'strategy' => [
            'types' => 'string',
            'choices' => ['template.html', 'template.pdf'],
            'default' => 'template.html',
        ],
        'config' => [
            'types' => 'array',
            'schema' => [
                'footer' => ['types' => 'bool', 'default' => false],
            ],
        ],
    ]
);

$options->getReader()->requireBool('config.footer'); // false: the default.
$options->set('strategy', 'template.doc');
// InvalidOptionsException: the value is not one of the choices.

The schema keys, the policy for the settings that the schema does not define and the way to normalize a value are in Validation.

The data of a container can be another container (a Configuration, an Options or any other of derafu/container): only its data is taken, and it is validated with the schema of the new one, so the defaults and the accepted values are the ones of the schema that is given:

$options = new Options($configuration, $schema);

Classes with Options or Configuration

OptionsAwareTrait (with OptionsAwareInterface) gives a class its options, and ConfigurableTrait (with ConfigurableInterface) gives it a configuration. The class returns its schema from getOptionsSchema() (or getConfigurationSchema()), and whoever uses the class passes an array or a container:

use Derafu\Config\Contract\OptionsAwareInterface;
use Derafu\Config\Trait\OptionsAwareTrait;

class Renderer implements OptionsAwareInterface
{
    use OptionsAwareTrait;

    public function getOptionsSchema(): array
    {
        return [
            'strategy' => [
                'types' => 'string',
                'choices' => ['template.html', 'template.pdf'],
                'default' => 'template.html',
            ],
        ];
    }

    public function render(array $options = []): string
    {
        $strategy = $this->resolveOptions($options)->getReader()->requireString('strategy');

        return 'Rendering with ' . $strategy;
    }
}

$renderer = new Renderer();
$renderer->setOptions(['strategy' => 'template.pdf']);

$renderer->getOptions()->getReader()->requireString('strategy');       // "template.pdf"
$renderer->render();                                      // "Rendering with template.pdf"
$renderer->render(['strategy' => 'template.html']);       // "Rendering with template.html"
  • setOptions() takes an array (which is validated with the schema of the class) or an OptionsInterface (which is used as it is).
  • getOptions() returns the options. Without any, they are created from the defaults of the schema.
  • resolveOptions($options) merges the top-level values of $options over the current ones and sets the result. If the result is not valid it throws and the previous options stay.

The schema of the options, and the one of the configuration, can also have the key __allowUndefinedKeys, a bool that says whether the values that the schema does not define are accepted (see validation).

On this page

#php
Last updated on 09/11/2026 by Anonymous