Oliver Nassar

Framework: S^3 (aka S cubed): I'm calling it Smart Setting Selection

August 03, 2009

In working on a custom framework (that's uniting a lot of the best of the existing ones, but keeping it lean and cutting out what I don't need), I ran into a configuration roadblock which caused me to write some nifty code.

Basically, this framework has a bunch of settings/config files, and some of the files (ini format, using parse_ini_file) are separated by the following keywords: local,development,staging,production

Basically meaning, if I have a configuration setting that is supposed to mean something like 'maxTimeout = 500', I could, and in a real world application, would have that setting store different values based on which server the code base is being run on (eg. my local would be a lot higher, but the other ones like the production would be much lower for optimization purposes).

This lead me to write some nifty code that will parse all the ini files, and then when I want to retrieve something, it does a check for one of those 4 reserved words. So a real world example would be something like this:

An ini file is parsed and has the following format:

Array
(
    [local] => Array
    (
        [name] => TEST
        [cookies] => 1
    )
    [production] => Array
    (
        [name] => TEST
        [cookies] => 1
    )
)

When doing a call to the function and asking for the setting 'name', recursively as it moves it's way down the settings array, it'll look to see if it contains a keyword of local, production, etc., and if it does, automatically set the value to that array/value. This will be clearer once I OS the framework which won't be for a while, but to understand it from a code perspective, check this out:

public static function getDetails() {
    $details = self::$details;
    foreach(func_get_args() as $argument) {
    $details = $details[$argument];
    if(array_key_exists(Request::getServerRole(),(array) $details))
        $details = $details[Request::getServerRole()];
    }
    return $details;
}

This means that my server will automatically pull the right setting detail based on what server is doing the requesting. I just need to setup my ini files so that if a setting needs to be different based on which server is being loaded, it can be.