tags:

views:

105

answers:

2

I am working on code re-factoring of configuration file loading part in PHP. Earlier I was using multiple 'ini' files but now I plan to go for single XML file which will be containing all configuration details of the project. Problem is, if somebody wants configuration file in ini or DB or anything else and not the default one (in this case XML), my code should handle that part.

If somebody wants to go for other configuration option like ini, he will have to create ini file similar to my XML configuration file and my configuration manager should take care everything like parsing, storing in cache. For that I need a mechanism lets say proper interface for my configuration data where the underlying data store can be anything( XML, DB, ini etc) also I don't want it to be dependent on these underlying store and anytime in future this should be extensible to other file formats.

+2  A: 

Have a look at Zend_Config. It provides adapters for Arrays, Xml and Inis. Like all components in Zend Framework, it can be used isolated from the remaining Framework. Even if you don't want to use it, it's well designed and you might get a few ideas for your own config manager from it.

Gordon
+1 for mentioning ZF.
Pekka
+1  A: 

Assuming you're wanting to use a class to handle all this, you have 3 options:

  1. Have a base class called something like, ReadConfigurationBase then 3 implementation classes, ReadConfigurationXML, ReadConfigurationINI, and ReadConfigurationDatabase and you'd have to choose the right one
  2. Same as above, but using a factory to choose, based off of something passed in. Like if you pass config.xml it would know to return ReadConfigurationBase implemented using ReadConfigurationXML
  3. Have a class called ReadConfiguration and it acts as step 2, but creates, contains, and owns, the 3 other classes.

The 3 non-base classes would simply know how to read that type of configuration file, and pass the information back in a generic manner. think along the lines of an interface: You know you can get the data, but you don't care how.

I'd suggest option 3, since it would make life easiest. You would have to do a little bit of modification every time you want to add a storage method, but that would just be adding a little bit into the ReadConfiguration class.

There is a way you could make it 100% dynamic, but that would complicate matters, and I don't think you really need it for this.

Slokun
+1 for showing how to DIY.
Pekka
Thanks for the answer. Can you elaborate third option in bit more detail?
Jack