fink/config

一个简单而强大的PHP配置库

1.0.0 2016-07-24 16:26 UTC

This package is not auto-updated.

Last update: 2024-09-26 01:02:34 UTC


README

Build Status Coverage Status Code Climate

Latest Stable Version Latest Unstable Version License

使用方法

给定以下json格式的配置文件

{
  "section": {
    "key": "value"
  }
}

可以使用以下方式使用配置类

use \Fink\config\Configuration;

// create a new instance
$config = new Configuration("config.json", Configuration::JSON);

// access a specific value
echo $config->get("section", "key"); // echoes "value"

功能

多种配置格式

该库目前支持json和ini格式。如果您想添加其他格式,请创建新的问题或pull request。

自动检测配置格式

Configuration类的第二个参数是可选的。因此这等同于

config.ini

[section]
key = value

config.json

{
  "section": {
    "key": "value"
  }
}

按照以下方式使用

use \Fink\config\Configuration;

// create a new instance
$jsonConfig = new Configuration("config.json");
$iniConfig = new Configuration("config.ini");

echo $jsonConfig->get("section", "key"); // echoes "value"
echo $iniConfig->get("section", "key");  // echoes "value", too

使用自定义配置加载器

要创建一个新的(自定义)配置加载器,只需创建一个新的子类 \Fink\config\loader\FileConfigurationLoader 并实现所需的方法。请确保覆盖静态类成员 $supportedFileTypes 作为支持配置文件类型的提示。将新的配置加载器添加到支持加载器的列表中

use \Fink\config\Configuration;

Configuration::addConfigurationLoader(42, YourCustomConfigurationLoader::class);

$config = new Configuration("your-custom-configuration-syntax.type", 42);

自动检测正确的加载器也将适用于自定义加载器。如果您的自定义加载器支持预定义加载器已支持的文件类型,则具有最小id的加载器将处理配置文件。