geeklab/conf

PHP >= 8.1 的不可变配置系统

4.0.0 2022-11-24 17:09 UTC

This package is auto-updated.

Last update: 2024-09-25 01:17:12 UTC


README

License: BSD phpstan enabled Scrutinizer Code Quality Code Coverage

geeklab/conf

geeklab/conf 是一个为 PHP >= 8.1 开发的不可变配置系统加载器和解析器,支持多种文件格式并具有一些模板功能。这个库是 '.env' 类型配置库的替代品,并使用 策略模式

基准测试

最新版本

4.0.0 (2020-11-24):更新为与 >= 8.1.0 兼容。

功能

  • 多文件配置加载,不再有单体配置!
  • 自引用占位符。 @[X.Y.Z]
  • 递归自引用占位符。 @[@[X.Y.Z].SOME_KEY]
  • 环境变量占位符。 $[ENVIRONMENT_VARIABLE_NAME](PHP 很喜欢 ${YOUR_TEXT_HERE}...)
  • 可以使用 INI、JSON、YAML 和数组文件。
  • 不可变性,因为你不应该在运行时更改你的配置。
  • 可以注入值,使事物更加动态。

安装

composer require geeklab/conf

使用方法

基本

// Where the configurations are.
$configurationDirectory = __DIR__ . '/config/';

// Load Configuration system with the JSON Configuration Driver 
$configuration = new GLConf(
    new JSONConfDriver(
        $configurationDirectory . 'system.json',  // Path and file name of main (top level) configuration.
        $configurationDirectory                   // Path to the other configuration files. 
    ),
    ['mySecretKey' => md5(rand())],                // Value injections.
    ['keys_to_lower']                              // Options:
                                                   //  Options to change the case of the key if returning a keyed array:    
                                                   //  keys_upper_case, keys_lower_case, keys_same_case
);

$configuration->init();

// Get the whole configuration.
var_export($configuration->getAll());

// Get one item.
var_export($configuration->get('space_pants.look_at_my'));

详细

PSR 兼容性

  • PSR-1
  • PSR-2
  • PSR-4
  • PSR-12

待办事项

  • 更多文档。
  • 包含 .env 吗?