laminas/laminas-config-aggregator
Aggregate and merge configuration from multiple providers in Laminas/Mezzio apps. Supports ordered loading, caching, PHP/array and glob-based config files, and environment-specific overrides for fast, predictable configuration builds.
Installation:
composer require laminas/laminas-config-aggregator
For multi-format support (JSON, YAML, INI, XML):
composer require laminas/laminas-config
Basic Usage:
Create a ConfigAggregator instance with providers (e.g., PHP files):
use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\ConfigAggregator\PhpFileProvider;
$aggregator = new ConfigAggregator([
new PhpFileProvider('config/*.global.php'),
]);
$config = $aggregator->getMergedConfig();
First Use Case:
db.global.php, cache.global.php) in a directory.Provider-Based Configuration:
PhpFileProvider for PHP arrays or LaminasConfigProvider for JSON/YAML/INI/XML.$aggregator = new ConfigAggregator([
new LaminasConfigProvider('config/*.{json,yaml}'),
new PhpFileProvider('config/*.php'),
]);
Dynamic Providers:
__invoke() in custom classes for reusable config logic:
class AppConfig {
public function __invoke() {
return ['app' => ['name' => 'MyApp']];
}
}
$aggregator = new ConfigAggregator([AppConfig::class]);
Environment-Specific Configs:
new PhpFileProvider('config/{production,staging,development}.php')
Caching for Performance:
Laminas\Cache):
$cache = new FilesystemCache('path/to/cache');
$aggregator = new ConfigAggregator([...], $cache);
Laravel-Specific:
AppServiceProvider:
$this->app->singleton(ConfigAggregator::class, function ($app) {
return new ConfigAggregator([
new PhpFileProvider(config_path('*.php')),
], $app->make(Cache::class));
});
app(ConfigAggregator::class)->getMergedConfig().Modular Configs:
$aggregator = new ConfigAggregator([], null, [], [
function (iterable $providers) {
if (class_exists('Vendor\Package\Config')) {
$providers[] = new Vendor\Package\Config();
}
return $providers;
}
]);
Duplicate Providers:
InvalidConfigProviderException if the same class or instance is added twice.Globbing Quirks:
PhpFileProvider uses Laminas\Stdlib\Glob if available (cross-platform patterns like *.{json,yaml}).laminas/laminas-stdlib for advanced globbing.Precedence Overrides:
// Overrides 'db' from earlier providers
new PhpFileProvider('config/database.php')
Caching Caveats:
cache()->forget() or disable caching in development.var_dump($aggregator->getMergedConfig());
$provider = new PhpFileProvider('config/*.php');
foreach ($provider as $config) {
var_dump($config);
}
Custom Providers:
__invoke() for reusable logic (e.g., database-driven configs):
class DatabaseConfig {
public function __invoke() {
return DB::table('config')->get()->toArray();
}
}
Post-Processors:
$aggregator = new ConfigAggregator([...], null, [
function (array $config) {
return array_map('strtolower', $config);
}
]);
Environment Variables:
.env values:
$aggregator = new ConfigAggregator([...], null, [
function (array $config) {
return preg_replace_callback('/%(.+)%/', function ($matches) {
return env($matches[1], $matches[0]);
}, $config);
}
]);
new PhpFileProvider('config/*.php') // Already a generator
$cacheKey = 'config_v2_' . filemtime('config/database.php');
$cache->save($cacheKey, $config);
How can I help you explore Laravel packages today?