开发者

INI Parsing with validation of required fields

I have a rather large INI that I parse out and validate to make sure all the required settings have been set.开发者_Go百科 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?


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 call failedINIValidation.

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 native array object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜