helhum/config-loader

Generic config loader with context and environment support.

Maintainers

👁 helhum

Package info

github.com/helhum/config-loader

pkg:composer/helhum/config-loader

Fund package maintenance!

www.paypal.me/helhum/19.99

helhum

Statistics

Installs: 8 616 464

Dependents: 9

Suggesters: 1

Stars: 30

Open Issues: 2

v0.12.6 2024-12-16 10:48 UTC

Requires

  • php: >=7.2

Requires (Dev)

Suggests

  • ext-yaml: For improved performance when parsing yaml files you should use the PECL YAML Parser php extension
  • symfony/yaml: To be able to parse yaml files, you will need symfony/yaml

Provides

None

Conflicts

None

Replaces

None

GPL-2.0-or-later 4538ad189ebbb319d97697f350401150dd7a6155


README

This is just a class, which helps you to merge a base configuration with configuration from different contexts and the environment.

Just require it using composer: composer require helhum/config-loader

Basic usage

$context = 'production';
$confDir = '/path/to/conf';
$configReaderFactory = new \Helhum\ConfigLoader\ConfigurationReaderFactory($confDir);
$configLoader = new \Helhum\ConfigLoader\ConfigurationLoader(
 [
 $configReaderFactory->createReader($confDir . '/default.php'),
 $configReaderFactory->createReader($confDir . '/' . $context . '.php'),
 $configReaderFactory->createReader('PREFIX', ['type' => 'env']),
 $configReaderFactory->createReader($confDir . '/override.php'),
 ]
);
$config = $configLoader->load();

Basic usage cached

$context = 'production';
$confDir = '/path/to/conf';
$cacheDir = '/path/to/cache';
$cacheIdentifier = md5($context . filemtime('/path/to/.env'));
$configReaderFactory = new \Helhum\ConfigLoader\ConfigurationReaderFactory($confDir);
$configLoader = new \Helhum\ConfigLoader\CachedConfigurationLoader(
 $cacheDir,
 $cacheIdentifier,
 function() use ($confDir, $context, $configReaderFactory) {
 return new \Helhum\ConfigLoader\ConfigurationLoader(
 [
 $configReaderFactory->createReader($confDir . '/default.php'),
 $configReaderFactory->createReader($confDir . '/' . $context . '.php'),
 $configReaderFactory->createReader('PREFIX', ['type' => 'env']),
 $configReaderFactory->createReader($confDir . '/override.php'),
 ]
 );
 }
);
$config = $configLoader->load();

Using processors

It is possible to add one or more processors to the config loader.

$context = 'production';
$confDir = '/path/to/conf';
$configReaderFactory = new \Helhum\ConfigLoader\ConfigurationReaderFactory($confDir);
$configLoader = new \Helhum\ConfigLoader\ConfigurationLoader(
 [
 $configReaderFactory->createReader($confDir . '/config.php'),
 ],
 [
 new \Helhum\ConfigLoader\Processor\PlaceholderValue(),
 ]
);
$config = $configLoader->load();

Advanced usage

Instead of hard coding which configuration sources should be included, it is possible to include multiple sources from within one configuration file.

$context = 'production';
$confDir = '/path/to/conf';
$configReaderFactory = new \Helhum\ConfigLoader\ConfigurationReaderFactory($confDir);
$configLoader = new \Helhum\ConfigLoader\ConfigurationLoader(
 [
 $configReaderFactory->createRootReader($confDir . '/config.yaml'),
 ]
);
$config = $configLoader->load();

The configuration file can then include an import section:

imports:
 - { resource: 'config.*.yml', type: glob }
 - { resource: 'env.yml' }

Feedback

Any feedback is appreciated. Please write bug reports, feature request, create pull requests, or just drop me a "thank you" via Twitter or spread the word.

Thank you!