zend config 分析

介绍

zend 是我对php框架ZendFramework的简称,现在主要有三个版本,因为工作原因,我平时接触的版本以ZendFramework 1.x 居多。

本文以源码为引,简单介绍了框架中配置文件加载有关的逻辑。

过程

我们在使用zend框架,通常会在index.php文件中看见这样的代码:

   $config = new Zend_Config_Ini(dirname(dirname(__FILE__)) . '/config.ini', 'production');
   $options = $config->toArray();
   $config = new Zend_Config($options);

 首先我们需要知道.ini文件的构成,比如

[production]

my.url="http://127.0.0.1:9000/"

[dev]

kid.config.port=2202

其中[production] [dev] 是section 可以理解为配置的分组,Zend_Config_Ini的第二个参数指定的就是这个分组,如果填写了section 则只获取此分组的配置。

上面配置的http://127.0.0.1:9000/, 可以使用$config->my->url获取。

如果配置toArray转化为数组,需要使用$config['my']['url']获取。

看似很简单的功能,框架的作者通过定义Config父类实现通用方法,使用不同子类适配不同格式的配置文件。

另外提一点,使用Zend_Config_Yaml 解析 docker-compose配置文件 对-配置的文件并不友好或者解析不出来。

$yml = new Zend_Config_Yaml(dirname(dirname(__FILE__)) . '/docker-compose.yml');
var_export($yml->toArray());exit;

 new Zend_Config_Ini实例化Zend_Config_Ini类

框架在类中使用的异常处理值得学习。

原文地址:https://www.cnblogs.com/kala00k/p/13369162.html