PHP 配置管理器

发布于 2024-08-26 05:45:44 字数 335 浏览 9 评论 0原文

我正在研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2024-09-02 05:45:45

假设您想要使用一个类来处理所有这些,您有 3 个选择:

  1. 有一个名为 ReadConfigurationBase 的基类,然后是 3 个实现类,ReadConfigurationXML,< code>ReadConfigurationINI 和 ReadConfigurationDatabase 并且您必须选择正确的一个
  2. 与上面相同,但使用工厂来选择,基于传入的内容。就像如果您传递 < code>config.xml 它会知道返回使用 ReadConfigurationXML 实现的 ReadConfigurationBase
  3. 有一个名为 ReadConfiguration 的类,它充当步骤 2 ,但创建、包含并拥有其他 3 个类。

3 个非基类只知道如何读取该类型的配置文件,并以通用方式将信息传回。按照接口的思路思考:您知道可以获取数据,但不关心如何获取数据。

我建议选择 3,因为它会让生活变得更轻松。每次想要添加存储方法时,您都必须进行一些修改,但这只是在 ReadConfiguration 类中添加一点点。

有一种方法可以使其 100% 动态,但这会使事情变得复杂,而且我认为您并不真正需要它。

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.

つ可否回来 2024-09-02 05:45:45

查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文