Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/helhum/config-loader
https://github.com/helhum/config-loader
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/helhum/config-loader
- Owner: helhum
- License: gpl-2.0
- Created: 2016-04-30T12:20:05.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2022-11-11T14:43:21.000Z (about 2 years ago)
- Last Synced: 2024-10-14T04:12:35.163Z (2 months ago)
- Language: PHP
- Size: 119 KB
- Stars: 29
- Watchers: 4
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Configuration Loader [![Build Status](https://github.com/helhum/config-loader/actions/workflows/Test.yml/badge.svg)](https://github.com/helhum/config-loader/actions/workflows/Test.yml)
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
```php
$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
```php
$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.
```php
$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.```php
$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:
```yaml
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](https://twitter.com/helhum)
or spread the word.Thank you!