rcsi/filesystem-config

3.6 2017-09-23 23:01 UTC

This package is auto-updated.

Last update: 2024-08-29 03:42:34 UTC


README

rcsi/filesystem-config 是一个易于使用的配置工具,它使用 ini 或 json 文件进行系统配置

关于

此工具提供了一种简单的接口来设置和检索配置变量

Composer

将 'rcsi/filesystem-config' 添加到您的 composer 需求中

许可协议

MIT 许可证 - https://open-source.org.cn/licenses/MIT

文档

此类将生成一个 PHP 单例,其中配置.ini 或 config.json 文件中的所有变量都已加载。

代码具有内联文档,阅读后应清晰易懂。

方法

Config::init() 返回配置单例

get(string $key, mixed|null $default = null) 返回 $key 的值或 $default。如果没有提供默认值,则返回 null

getBool(string $key) 根据 key 返回布尔值。字符串 'true' 或 '1' 返回 true,其他都返回 false

getArray(string $key, string $delim = ',') 沿 $delim 分割数组。如果没有提供分隔符,则默认为 ','

setFile(string $file) 设置配置的文件名

getDir() 返回配置目录

load() 将配置文件加载到单例中

save(string|null $type = null) 将单例保存到路径/文件,如果未提供,则尝试发现文件类型

saveINI() 将单例保存为 INI 文件到路径

saveJSON() 将单例保存为 JSON 文件到路径

determineFileType() 读取文件的扩展名以检测其文件类型

getKeys() 返回配置中所有可用的键

set($key, $value) 设置配置变量

addArray($key, $value) 向配置数组添加一个值

dump() 输出配置

包装器

Filesystem-config 包含 ConfigWrapper,它将自动尝试检测您的配置路径并提供一个已加载的配置单例。或者,如果您更喜欢,您可以指定配置路径以绕过检测。默认情况下,它会在与 vendor/ 目录同一级别的 config/ 目录中查找。

使用方法

ConfigWrapper::setPath(__DIR__ . "/../config"); // Optional...
$config = ConfigWrapper::init()

示例

config.ini 内容

[Examples]
stringOne = This is a string
boolOne = 0
boolTwo = 1
boolThree = true
arrayOne = One, Two, Three, Four

代码

$config = ConfigWrapper::init();
$string = $config->get('stringOne');              // Returns 'This is a string'
$string = $config->get('stringTwo');              // Returns null
$string = $config->get('stringTwo', 'default');   // Returns 'default'
$bool   = $config->get('boolOne');                // Returns '0'
$bool   = $config->getBool('boolOne');            // Returns false
$bool   = $config->getBool('boolTwo');            // Returns true
$bool   = $config->getBool('boolThree');          // Returns true
$bool   = $config->getBool('boolFour');           // Returns false
$array  = $config->getArray('arrayOne');          // Returns ['One', 'Two', 'Three', 'Four']