PHP 配置管理器
我正在研究 PHP 中配置文件加载部分的代码重构。早些时候,我使用了多个“ini”文件,但现在我计划使用单个 XML 文件,该文件将包含项目的所有配置详细信息。问题是,如果有人想要 ini 或 DB 或其他任何文件中的配置文件而不是默认文件(在本例中为 XML),我的代码应该处理该部分。
如果有人想要使用其他配置选项,例如 ini,他将必须创建类似于我的 XML 配置文件的 ini 文件,并且我的配置管理器应该负责解析、存储在缓存中等所有事情。为此,我需要一种机制,让我的配置数据有正确的接口,其中底层数据存储可以是任何东西(XML、DB、ini 等),而且我不希望它依赖于这些底层存储,并且在将来的任何时候这应该可扩展为其他文件格式。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您想要使用一个类来处理所有这些,您有 3 个选择:
ReadConfigurationBase
的基类,然后是 3 个实现类,ReadConfigurationXML
,< code>ReadConfigurationINI 和ReadConfigurationDatabase
并且您必须选择正确的一个ReadConfigurationXML
实现的ReadConfigurationBase
ReadConfiguration
的类,它充当步骤 2 ,但创建、包含并拥有其他 3 个类。3 个非基类只知道如何读取该类型的配置文件,并以通用方式将信息传回。按照接口的思路思考:您知道可以获取数据,但不关心如何获取数据。
我建议选择 3,因为它会让生活变得更轻松。每次想要添加存储方法时,您都必须进行一些修改,但这只是在
ReadConfiguration
类中添加一点点。有一种方法可以使其 100% 动态,但这会使事情变得复杂,而且我认为您并不真正需要它。
Assuming you're wanting to use a class to handle all this, you have 3 options:
ReadConfigurationBase
then 3 implementation classes,ReadConfigurationXML
,ReadConfigurationINI
, andReadConfigurationDatabase
and you'd have to choose the right oneconfig.xml
it would know to returnReadConfigurationBase
implemented usingReadConfigurationXML
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.
查看 Zend_Config。它提供了数组、Xml 和 Inis 的适配器。与 Zend Framework 中的所有组件一样,它可以与其余 Framework 分开使用。即使您不想使用它,它也经过精心设计,您可能会从中获得一些关于您自己的配置管理器的想法。
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.