hambrook / config
易于使用的配置对象,可保存到JSON文件。
Requires
This package is not auto-updated.
Last update: 2024-09-18 09:56:44 UTC
README
#Config
一个简单的Config对象,它可以加载JSON文件,让您在不需要检查它们是否已设置的情况下访问值。然后,在脚本结束时自动保存回JSON文件。它继承了Nest的所有获取/设置能力。
##示例
$Config = new \Hambrook\Config\Config("settings.json");
JSON文件(settings.json)
{
"foo": "bar",
"one": {
"two": "three"
}
};
####使用字符串作为路径参数
$value = $Config->get("foo"); // "bar"
####这次我们进入两个层级,所以使用数组作为路径
$value = $Config->get(["one", "two"]); // "three"
####如果我们尝试获取不存在的项,会发生错误吗?
$value = $Config->get(["nope", "two"]); // returns `null`, not an error
####或者我们可以指定自己的默认值以应对错误
$value = $Config->get(["nope", "two"], "safe"); // returns "safe", not an error
##保存默认情况下,Config将在脚本结束时自动保存。如果您以false作为第二个参数实例化Config,则它将不再自动保存。
您可以通过使用save()函数在任何时候保存。
// Save the JSON encoded data to the file and returns `$this` $Config->save();
##快捷方式您可以使用__(默认)作为分隔符而不是使用数组,然后您可以做像...
// Get a nested value $value = Config->one__two // "three" // Get a nested value that isn't there $value = Config->one__bad // null // Get a nested value, with a default if the value isn't there $value = Config->one__bad("default") // "default" // Get a nested value, with a default if the value isn't there $value = Config->one__two = "newthree" // sets the value to "newthree" then returns `$this`
##适合谁?Config是为小型CLI脚本和工具构建的。但它可以在任何地方使用。
##反馈告诉我你是否喜欢它。告诉我你是否讨厌它。告诉我你是否使用过它并且认为“一般”。我渴望听到您的反馈。
##路线图
- 添加任何其他应包含的标准文档
- 添加适当的单元测试
- 如果您有任何想法,让我知道。
##许可版权 © 2015 Rick Hambrook
本程序是自由软件:您可以按照自由软件基金会发布的GNU通用公共许可证的条款重新分发和/或修改它,许可证版本为3,或(根据您的要求)任何较新版本。
本程序按照希望它将是有用的,但没有任何保证;甚至没有关于适销性或特定用途适用性的暗示保证。有关详细信息,请参阅GNU通用公共许可证。
您应该已经收到GNU通用公共许可证的副本。如果没有,请参阅https://gnu.ac.cn/licenses/。