Connecting Tech Pros Worldwide Help | Site Map

PHP Configuration Processing

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#1: Jul 8 '09
(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
Expand|Select|Wrap|Line Numbers
  1.  
  2. # comments begin with a hash symbol
  3. #
  4. [section_name]
  5. setting=value
  6. multi=value1,value2,value3
  7.  
  8. #  this is the second section
  9. [second_section]
  10. foo=false
  11. bar=true
  12.  
  13.  
This file will be passed to the class constructor and then it can be used, using the above config file, like so:

Final Goal:
Expand|Select|Wrap|Line Numbers
  1.  
  2. $config = new Config('myFile.cfg'); 
  3.  
  4. // outputs "value"
  5. echo $config->section_name->setting
  6.  
  7. // outputs an array of values (not just comma separated) 
  8. echo $config->section_name->multi
  9.  
  10. // throws error, cannot override value of foo
  11. $config->second_section->foo = true
  12.  
  13.  
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....
Expand|Select|Wrap|Line Numbers
  1.  
  2. $sourceArray = array_keys($config->sources);
  3. // build <select><options>
  4.  
  5. then alter access it with the selected source: 
  6. $sourceSettings = $config->sources->$selectedSource; 
  7.  
  8. // then VOILA!! we have: 
  9. mysql_connect($sourceSettings->db_user, $soruceSettings->db_pass, etc); 
  10.  
  11.  
Wheew!! sorry for long post.

So! Any Ideas for a (standard-style) config file layout my fried brain can't think of?





Da
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,745
#2: Jul 8 '09

re: PHP Configuration Processing


Hi.

Couldn't you just do something like:
Expand|Select|Wrap|Line Numbers
  1. [db_section]
  2. sources='live server','test server'
  3. hosts='db.example.com','localhost'
  4. schemas='liveDbName','localDbName'
  5. loginNames='liveUser','localUser'
  6. loginPasswords='livePass','localPass'
It's not exactly perfect but it's simple, and it fits into the configuration syntax you posted.
Would be easy to work with in PHP.

Or you could go with something more like how an array would be written:
Expand|Select|Wrap|Line Numbers
  1. [db_section]
  2. sources = {
  3.   'live server' : {
  4.     'host' : 'db.example.com',
  5.     'schema' : 'liveDbName',
  6.     # etc...
  7.   },
  8.   'testserver' : {
  9.     'host' : 'db.localhost',
  10.     'schema' : 'localDbName',
  11.     # etc...
  12.   }
  13. }
A lot more flexible, but more complex. Perhaps not something you would want to see in a config file.

Or you could do something like sub-sections:
Expand|Select|Wrap|Line Numbers
  1. [db_section::live_server]
  2. name="Live server of DOOM"
  3. host="db.example.com"
  4. #etc...
  5.  
  6. [db_section::local_server]
  7. name="Local server"
  8. host="db.localhost"
  9. #etc...
  10.  
Endless possibilities :P
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#3: Jul 8 '09

re: PHP Configuration Processing


:P to you too.

For the sake of not making my post into a novel, I didn't post what I had already thought about and reasons why it might* not work.

The two I tried were your first and last one.

Reasons for first one:
Users would mess up and trying to align, which password goes with which. The values are not clearly apparent as they are in your example, also there might be more than two, which gets even gets messier. But If I had to choose one, I think, I'd go this route.

second one:
How would I easily parse through and the all the source names (to display in the drop down), without having to get all the db_sections with a regex then parse out the db_sections.

I gotta go, but I'll come back in a bit to continue....

Quote:

Originally Posted by Atli View Post

Hi.

Couldn't you just do something like:

Expand|Select|Wrap|Line Numbers
  1. [db_section]
  2. sources='live server','test server'
  3. hosts='db.example.com','localhost'
  4. schemas='liveDbName','localDbName'
  5. loginNames='liveUser','localUser'
  6. loginPasswords='livePass','localPass'
It's not exactly perfect but it's simple, and it fits into the configuration syntax you posted.
Would be easy to work with in PHP.

Or you could go with something more like how an array would be written:
Expand|Select|Wrap|Line Numbers
  1. [db_section]
  2. sources = {
  3.   'live server' : {
  4.     'host' : 'db.example.com',
  5.     'schema' : 'liveDbName',
  6.     # etc...
  7.   },
  8.   'testserver' : {
  9.     'host' : 'db.localhost',
  10.     'schema' : 'localDbName',
  11.     # etc...
  12.   }
  13. }
A lot more flexible, but more complex. Perhaps not something you would want to see in a config file.

Or you could do something like sub-sections:
Expand|Select|Wrap|Line Numbers
  1. [db_section::live_server]
  2. name="Live server of DOOM"
  3. host="db.example.com"
  4. #etc...
  5.  
  6. [db_section::local_server]
  7. name="Local server"
  8. host="db.localhost"
  9. #etc...
  10.  
Endless possibilities :P

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#4: Jul 8 '09

re: PHP Configuration Processing


....i'm back...

I'm thinking more about coding the third one. Specifically about how I would put that into PHP code for the program to use WITHOUT hard coding the sub_section/source into the data.

maybe: $config->db_section->$selectedSource->host

Yes! that will work. I think I touched this in the OP and didn't connect the dots.

Thanks Atli for making me brainstorm this. I'll attack it, create a class and maybe put an article in about it.

See ya,



Dan
Reply