Config providers

The ConfigAggregator works by aggregating "config providers" passed to its constructor. Each provider should be a callable, returning a configuration array (or a PHP generator) to be merged.

$aggregator = new ConfigAggregator([
    function () {
        return ['foo' => 'bar'];
    },
    new PhpFileProvider('*.global.php'),
]);
var_dump($aggregator->getMergedConfig());

If the provider is a class name, the aggregator automatically instantiates it before invoking it; as such, any class name you use as a config provider must also define __invoke(), and that method must return an array.

This can be used to mimic the Zend Framework module system: you can specify a list of config providers from different packages, and aggregated configuration will be available to your application.

As a library owner, you can distribute your own configuration providers that provide default values for use with your library.

As an example:

class ApplicationConfig
{
    public function __invoke()
    {
        return ['foo' => 'bar'];
    }
}

$aggregator = new ConfigAggregator([
    ApplicationConfig::class,
    new PhpFileProvider('*.global.php'),
]);
var_dump($aggregator->getMergedConfig());

Output from both examples will be the same:

array(4) {
  'foo' =>
  string(3) "bar"
  'db' =>
  array(1) {
    'dsn' =>
    string(9) "mysql:..."
  }
  'cache_storage' =>
  string(5) "redis"
  'redis' =>
  array(0) {
  }
}

Generators

Config providers can be written as generators. This way, a single callable can provide multiple configurations:

use Zend\ConfigAggregator\ConfigAggregator;
use Zend\Stdlib\Glob;

$aggregator = new ConfigAggregator([
    function () {
        foreach (Glob::glob('data/*.global.php', Glob::GLOB_BRACE) as $file) {
            yield include $file;
        }
    },
]);
var_dump($aggregator->getMergedConfig());

The PhpFileProvider is implemented as a generator.

Available config providers

PhpFileProvider

Loads configuration from PHP files returning arrays, such as this one:

return [
    'db' => [
        'dsn' => 'mysql:...',
    ],
];

Wildcards are supported:

use Zend\ConfigAggregator\ConfigAggregator;
use Zend\ConfigAggregator\PhpFileProvider;

$aggregator = new ConfigAggregator(
    [
        new PhpFileProvider('config/*.global.php'),
    ]
);

The example above will merge all matching files from the config/ directory. If you have files such as app.global.php or database.global.php in that directory, they will be loaded using this above lines of code.

The provider also supports globbing. Globbing defaults to PHP's glob() function. However, if Zend\Stdlib\Glob is available, it will use that to allow for cross-platform glob patterns, including brace notation: 'config/autoload/{{,*.}global,{,*.}local}.php'. Install zendframework/zend-stdlib to utilize this feature.

ZendConfigProvider

Sometimes using plain PHP files may be not enough; you may want to build your configuration from multiple files of different formats, such as INI, JSON, YAML, or XML. zend-config-aggregator allows you to do so via its ZendConfigProvider. This feature requires first installing zend-config:

$ composer require zendframework/zend-config

Once installed, you may use as many ZendConfigProvider instances as you need:

use Zend\ConfigAggregator\ConfigAggregator;
use Zend\ConfigAggregator\ZendConfigProvider;

$aggregator = new ConfigAggregator(
    [
        new ZendConfigProvider('*.global.json'),
        new ZendConfigProvider('database.local.ini'),
    ]
);

These could even be combined into a single glob statement:

$aggregator = new ConfigAggregator(
    [
        new ZendConfigProvider('*.global.json,database.local.ini'),
    ]
);

ZendConfigProvider accepts wildcards and globs, and autodetects the config type based on file extension.

Some config readers (in particular, YAML) may need additional dependencies; please refer to the zend-config manual for more details.