comphp/config
最新稳定版本:v0.2
Composer 安装命令:
composer require comphp/config
包简介
Library designed to simplify the management of configuration files in PHP applications. It leverages the power of dynamic driver loading and custom behaviors to provide a highly adaptable solution for application configuration.
README 文档
README
Overview
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.
Features
- 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.
Installation
Install via Composer:
composer require comphp/config
Usage
Initialize ConfigManager
use Neuron\Configuration\ConfigManager; use DI\ContainerBuilder; $container = (new ContainerBuilder())->build(); $configManager = new ConfigManager($container);
Load Configuration Files
$configManager->load('database', 'config.json'); $configManager->load('app', 'config.php');
Access Configuration Values
echo $configManager->get('database.host', 'default_host');
Modify Configuration
$configManager->set('cache.enabled', true); $configManager->unset('cache.enabled');
Convert Configuration to Array
print_r($configManager->toArray());
Supported Formats
By default, comphp/config supports:
- JSON (
.json) - PHP (
.phpreturning an array)
You can register additional parsers using ParserRegistry.
Extending with Custom Parsers
use Neuron\Configuration\Parsers\JsonParser; use Neuron\Configuration\ParserRegistry; $parserRegistry = $configManager->parsers; $parserRegistry->register(YamlParser::class, 'yaml'); $parserRegistry->register(DatabaseParser::class, 'db')
Merge Strategies
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);
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
See CONTRIBUTING.md for guidelines on how to contribute.
Changelog
See CHANGELOG.md for a history of updates.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-17