PHP configuration manager

I am working on re-factoring the code to load the configuration file in PHP. I used to use several "ini" files, but now I plan to move to a single XML file, which will contain all the details of the project configuration. The problem is that if someone needs a configuration file in ini or DB or something else, and not the default (in this case, XML), my code should handle this part.

If someone wants to go to another configuration option, for example, ini, he will have to create an ini file similar to my XML configuration file, and the configuration manager should take care of everything, like parsing, storing in the cache. To do this, I need a mechanism that allows me to say the right interface for my configuration data, where anything can be the base data store (XML, DB, ini, etc.), and I don’t want it to depend on this main store, and at any time in the future it should be extensible for other file formats.

+6
xml php config ini
source share
2 answers

Assuming you want to use a class to handle all of this, you have 3 options:

  • The base class has something like ReadConfigurationBase , then 3 implementation classes ReadConfigurationXML , ReadConfigurationINI and ReadConfigurationDatabase , and you will need to choose the right one
  • Same as above, but using factory to select based on something passed in. As if you passed config.xml , it would know that it returns ReadConfigurationBase using ReadConfigurationXML
  • Take a class called ReadConfiguration and it acts like step 2, but creates, contains, and owns 3 other classes.

3 non-base classes would simply know how to read this type of configuration file, and transmit information in a general way. think along the lines of the interface: you know that you can receive data, but you do not care how to do it.

I would suggest option 3, as this would make life easier. You would have to make a small modification every time you want to add a storage method, but that will just add a bit to the ReadConfiguration class.

There is a way to make it 100% dynamic, but it will complicate the situation, and I don’t think you really need it.

+5
source share

Take a look at Zend_Config . It provides adapters for arrays, Xml and Inis. Like all components in the Zend Framework, it can be used separately from the rest of the Framework. Even if you do not want to use it, it is well designed and you can get some ideas from it for your configuration manager.

+3
source share

All Articles