473,431 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,431 software developers and data experts.

PHP Configuration Processing

dlite922
1,584 Expert 1GB
(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
Jul 7 '09 #1
3 1568
Atli
5,058 Expert 4TB
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
Jul 8 '09 #2
dlite922
1,584 Expert 1GB
: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....

@Atli
Jul 8 '09 #3
dlite922
1,584 Expert 1GB
....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
Jul 8 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: chokk | last post by:
All, I am getting the following error when I try to run a form page. Can someone tell me what this is about and show me how to fix it. Thnaks. Server Error in '/' Application...
3
by: rdcpro | last post by:
Hi all, I've been building a nifty deserializing configuration handler that I use in conjunction with my web.config in an ASP.NET web app. This is working quite well, but I'm planning on...
1
by: Ramanfromoz | last post by:
Hi, Developing a new we application. Everything okay on my local WIN XP PROFESSIONAL, IIS 5.0 running locally. The website is running smoothly. Now, the same code I am copying over to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
0
by: js | last post by:
I am getting the following error when enter my web site's url in the browser address bar. I could not even get to the first default page of my web site. All this happened after I recompiled the...
1
by: Alan T | last post by:
I want to store some values so that the application can read from a file, eg. ini file, configuration file. If I add a Application Configuration File, to store the name of files: key = "sales"...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
5
by: Jay | last post by:
I want to store various text settings in a configuration file, that I will read into C# and make use of. Here's a simplified example of the file: #comment loop(var=1:5){ deviceundertest ...
4
by: aSoundMind | last post by:
Hi there, I recieve this error Server Error in '/integrate' Application. -------------------------------------------------------------------------------- Configuration Error Description: An...
3
by: abiclub | last post by:
This is the error im getting,Is it a problem with versions, Im using the .net framework express edition (3.5) ,if so how do i change it Server Error in '/Test' Application....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.