(My ultimate question is not PHP related, but since its programmed in PHP, here it goes)
I'm working on a PHP class that will read a semi-standard style of config file. I'm stuck at a point on how to expand to a three level deep value.
Config file right now looks like any other config file you've seen, such as PHP's php.ini or Apache's http.conf
Example File -
-
# comments begin with a hash symbol
-
#
-
[section_name]
-
setting=value
-
multi=value1,value2,value3
-
-
# this is the second section
-
[second_section]
-
foo=false
-
bar=true
-
-
This file will be passed to the class constructor and then it can be used, using the above config file, like so:
Final Goal: -
-
$config = new Config('myFile.cfg');
-
-
// outputs "value"
-
echo $config->section_name->setting
-
-
// outputs an array of values (not just comma separated)
-
echo $config->section_name->multi
-
-
// throws error, cannot override value of foo
-
$config->second_section->foo = true
-
-
As you can see, this is 2 dimensional and works fine for most cases, but I have a different config need: what I call 2.5 dimensional :P (stop laughing, it's five o'clock and I'm burnt out)
Everything better by example:
I have an app that get's it data source from multiple places that the user selects from the drop down. Those two places are separate mysql database with identical schema, one for "live" production feed, one for "offline" testing feed.
I want to put these sources in a config along with their mysql login WITHOUT making the source name the "section_name". Reason: I don't want the app to know what those source names are, (e.g. if they change or add a third source, i'd have to change my code)
So to build my drop down, i'd have to read the entire config directory and parse through all the sections (there are others that are not 'sources') and see which ones are sections, then get their names, and display it in the drop down menu for the user to select (BLEGH! ugly!)
I'd like a config layout that simply gives me the source names (that can later be used as a key to get the MySQL login, etc)
something like....
-
-
$sourceArray = array_keys($config->sources);
-
// build <select><options>
-
-
then alter access it with the selected source:
-
$sourceSettings = $config->sources->$selectedSource;
-
-
// then VOILA!! we have:
-
mysql_connect($sourceSettings->db_user, $soruceSettings->db_pass, etc);
-
-
Wheew!! sorry for long post.
So! Any Ideas for a (standard-style) config file layout my fried brain can't think of?
Da