flaviovs / uconfig
一个微型、无装饰的PHP配置接口
0.0.1
2016-03-15 22:28 UTC
Requires (Dev)
- havramar/phpunit-tmp-dir: dev-master
- phpunit/phpunit: ^5.2
This package is not auto-updated.
Last update: 2024-09-26 00:24:46 UTC
README
用法
$handler = new \UConfig\INIFileHandler('/path/to/config.ini');
$defaults = [
'section' => [
'bar' => 1,
'zoo' => 2,
],
];
$config = new \UConfig\Config($defaults);
$config->addHandler(new \UConfig\INIFileHandler('/path/to/config.ini'));
$bar = $config->get('section', 'bar');
// $bar = 1
try {
$config->get('nonexistent', 'bar');
} catch ( \UConfig\SectionNotFoundException $ex ) {
die("Section not found: " . $ex->getMessage());
} catch ( \UConfig\OptionNotFoundException $ex ) {
die("Option not found: " . $ex->getMessage());
}
处理器
配置由 配置处理器 处理。要实现一个新的处理器,添加一个实现 UConfig\Handler
接口的类,其中有一个名为 load()
的方法,该方法返回一个配置数组,其结构必须如下所示
$config = [
'section' => [
'key1' => value,
'key2' => value,
//(...)
],
'anothersection' => [
'key1' => value,
'key2' => value,
//(...)
]
]
UConfig\Config
对象可以附加多个处理器。使用 addHandler()
方法添加处理器。配置按顺序加载,后一个处理器覆盖先前的处理器。
目前,仅提供了 UConfig\INIFileHandler
。将来可能会添加其他处理器。