affinity4 / config
从各种格式加载配置文件到PHP数组,并轻松检索深层嵌套的项目
3.0.0
2022-05-25 23:49 UTC
Requires
- php: >=8.0
- nette/neon: ^3.0
- symfony/yaml: ^3.2
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-26 18:10:35 UTC
README
从各种格式加载配置文件到PHP数组,并轻松检索深层嵌套的项目
特性
- 指定任何实现 Affinity4\Config\LoaderInterface 的 Loader
- 获取整个值数组
- 使用简单易读的模式获取特定值
安装
Affinity4/Config 通过 composer 提供
composer require affinity4/config
用法
假设 config.yml 的内容如下
env: local db: local: name: test user: root pass: root host: 127.0.0.1
使用 Affinity4 Yaml Loader
$loader = new Affinity4\Config\Loader\Yaml(__DIR__ . '/config.yml'); $config = new Affinity4\Config\Config($loader);
不传递键,使用 get()
方法获取完整数组
$config->get();
将返回
[ 'env' => 'local', 'db' => [ 'local' => [ 'name' => 'test', 'user' => 'root', 'pass' => 'root', 'host' => '127.0.0.1', ] ] ]
或通过指定映射到嵌套键获取特定值
$config->get('db local name'); // test
加载器
Affinity4/Config 默认包含以下加载器
- Json
- Yaml
- Neon
- PHP
测试
运行测试
vendor/bin/phpunit
许可证
(c) 2017 Luke Watts (Affinity4.ie)
此软件受 MIT 许可证许可。有关完整的版权和许可信息,请查看随源代码分发的 LICENSE 文件。