Caching

Merging configuration on every request is not performant, particularly when using many configuration files. As such, laminas-config-aggregator also provides the ability to enable a filesystem-based configuration cache.

To enable the configuration cache, pass a cache file name as the second parameter to the ConfigAggregator constructor:

use Laminas\ConfigAggregator\ArrayProvider;
use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\ConfigAggregator\PhpFileProvider;

$aggregator = new ConfigAggregator(
    [
        new ArrayProvider([ConfigAggregator::ENABLE_CACHE => true]),
        new PhpFileProvider('*.global.php'),
    ],
    'data/config-cache.php'
);

When a cache file is specified, you will also need to add the config_cache_enabled key (which you can also specify via the ConfigAggregator::ENABLE_CACHE constant) somewhere within one of your configuration providers, and set it to boolean true. Using this approach, if you were to use the globbing pattern {{,*.}global,{,*.}local}.php (or similar) with the PhpFileProvider, you could drop a file named enable-cache.local.php into your production deployment with the following contents in order to enable configuration caching in production:

<?php
use Laminas\ConfigAggregator\ConfigAggregator;

return [
    ConfigAggregator::ENABLE_CACHE => true,
];

When caching is enabled, the ConfigAggregator does not iterate config providers. Because of that it is very fast, but after it is enabled, you cannot make any changes to configuration without clearing the cache. Caching should be used only in a production environment, and your deployment process should clear the cache.

You can control the permissions used when creating the cache file by passing the file mode in the ConfigAggregator::CACHE_FILEMODE configuration. Use this if your config contains sensitive information such as database passwords:

use Laminas\ConfigAggregator\ArrayProvider;
use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\ConfigAggregator\PhpFileProvider;

$aggregator = new ConfigAggregator(
    [
        new ArrayProvider([
            ConfigAggregator::ENABLE_CACHE => true,
            ConfigAggregator::CACHE_FILEMODE => 0600 // only owner can read and write
        ]),
        new PhpFileProvider('*.global.php'),
    ],
    'data/config-cache.php'
);

Note that mode is an octal value. To ensure the expected operation, you need to prefix the file mode with a zero (e.g. 0644).