INI 解析并验证必填字段
我有一个相当大的 INI,我解析并验证它以确保所有必需的设置都已设置。如果我需要添加/删除 INI 设置,这将是一个巨大的痛苦。
在我的 PHP 脚本中,我使用类似这样的内容:
$ini = parse_ini_file($this->ini_filename, true);
// Debug Settings
if(isset($ini['debug_settings']['debug'])) {
$this->debug = $ini['debug_settings']['debug'];
} else {
$this->failedINIValidation("['debug_settings']['debug'] not configured in the INI file: ");
}
failedINIValidation() 向我发送一封电子邮件,其中包含缺少的 INI 字段/值/等...
想让它更通用,有什么想法吗?
I have a rather large INI that I parse out and validate to make sure all the required settings have been set. This is a massive pain if I need to add/remove a INI setting.
In my PHP script I use something like this:
$ini = parse_ini_file($this->ini_filename, true);
// Debug Settings
if(isset($ini['debug_settings']['debug'])) {
$this->debug = $ini['debug_settings']['debug'];
} else {
$this->failedINIValidation("['debug_settings']['debug'] not configured in the INI file: ");
}
failedINIValidation() sends me a email with the missing INI field/value/etc...
wanted to make this more generic, any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是将其作为一个想法扔掉,已经晚了,我也累了,但是如何定义一个包含要检查的结构的数组,运行
array_diff
然后循环结果(如果有的话) ) 调用failedINIValidation
。然后,如果您想添加或删除 INI 文件中所需的任何内容,您只需从数组中删除该项目即可。
除非您的 INI 文件非常复杂,否则我不会担心开销。我运行了一些快速而肮脏的基准测试来验证我在其他地方看到的声明:使用
parse_ini_file
比包含包含本机array
对象的配置文件更快。Just throwing this out as an idea, it's late and I'm tired, but how about a defined array containing the structure you want to check against, running an
array_diff
and then looping over the result (if any) to callfailedINIValidation
.Then, if you want to add or remove anything from being required in the INI file you simply have to remove the item from the array.
Unless your INI file is really complex I wouldn't worry about overheads. I ran some quick and dirty benchmarking to verify claims I've seen elsewhere: using
parse_ini_file
was quicker than including a configuration file containing a nativearray
object.