The comphp/config
library is designed to simplify the management of configuration files in PHP applications. It leverages dynamic driver loading and custom behaviors to provide a highly adaptable solution for application configuration.
- Supports Multiple Formats: JSON and PHP configurations out of the box.
- Modular & Extensible: Easily register additional parsers.
- Dependency Injection Ready: Works seamlessly with PSR-11 containers.
- Merge Strategies: Replace, merge, or ignore existing configuration values.
- Dot-Notation Access: Retrieve nested configuration values effortlessly.
Install via Composer:
composer require comphp/config
use Neuron\Configuration\ConfigManager;
use DI\ContainerBuilder;
$container = (new ContainerBuilder())->build();
$configManager = new ConfigManager($container);
$configManager->load('database', 'config.json');
$configManager->load('app', 'config.php');
echo $configManager->get('database.host', 'default_host');
$configManager->set('cache.enabled', true);
$configManager->unset('cache.enabled');
print_r($configManager->toArray());
By default, comphp/config
supports:
- JSON (
.json
) - PHP (
.php
returning an array)
You can register additional parsers using ParserRegistry
.
use Neuron\Configuration\Parsers\JsonParser;
use Neuron\Configuration\ParserRegistry;
$parserRegistry = $configManager->parsers;
$parserRegistry->register(YamlParser::class, 'yaml');
$parserRegistry->register(DatabaseParser::class, 'db')
When loading configurations, you can define how new values are merged:
MergeMode::Replace
(default): Overwrites existing values.MergeMode::Merge
: Merges arrays recursively.MergeMode::Ignore
: Keeps existing values untouched.MergeMode::Error
: Throws an exception if a key already exists.
Example:
use Neuron\Configuration\MergeMode;
$configManager->load('app', 'config.json', MergeMode::Merge);
This project is licensed under the MIT License - see the LICENSE file for details.
See CONTRIBUTING.md for guidelines on how to contribute.
See CHANGELOG.md for a history of updates.