因为写在线写blog很是不方便,所以就装了一个Zoundry Raven,没想到呀!
把我《Zend Framework 框架学习(二)》给全毁了呀!杯具。。。 以后再重写吧。
Zend Framework项目的应用程序一般都是通过 index.php 来引导和初始化,主要由Zend_Application类对配置文件进行加载。
// Define path to application directory // Define application environment // Ensure library/ is on include_path // |
Zend_Application_Bootstrap_BootstrapAbstract类也会根据配置文件加载相应的引导资源类。因此我们只要看看 Zend_Application类的setOptions()方法和 Zend_Application_Bootstrap_BootstrapAbstract的setOptions()方法,便大概可以知道配置文件应该怎么写了。
我们先分析Ini配置文件的加载过程;
//调用Zend_Application的构造函数 //Zend_Application的构造函数的传递两个参数: //1.$environment(环境名称) application.ini环境名,经常是ini文件中第一个括起来的。 //2.$options 配置信息 String path to configuration file, or array/Zend_Config of configuration options $application = new Zend_Application( APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini' ); ..... //Zend_Application的构造函数如下 /** $this->_environment = (string) $environment; require_once 'Zend/Loader/Autoloader.php'; if (null !== $options) { //将$options数组化;如果是string使用_loadConfig数组化;如果是继承于Zend_Config实例化的对象使用toArray方法数组化; $this->setOptions($options); |
接着看Zend_Application类的setOptions()方法
/*
*
* Set application options * * @param array $options * @throws Zend_Application_Exception When no bootstrap path is provided * @throws Zend_Application_Exception When invalid bootstrap information are provided * @return Zend_Application */ public function setOptions( array $options ) { // 如果键config不为空,加载config文件 if ( ! empty ( $options [ ' config ' ])) { if ( is_array ( $options [ ' config ' ])) { $_options = array (); foreach ( $options [ ' config ' ] as $tmp ) { $_options = $this -> mergeOptions( $_options , $this -> _loadConfig( $tmp )); } $options = $this -> mergeOptions( $_options , $options ); } else { $options = $this -> mergeOptions( $this -> _loadConfig( $options [ ' config ' ]) , $options ); } } $this -> _options = $options ; $options = array_change_key_case ( $options , CASE_LOWER); $this -> _optionKeys = array_keys ( $options ); // 设置php的配置 if ( ! empty ( $options [ ' phpsettings ' ])) { $this -> setPhpSettings( $options [ ' phpsettings ' ]); } // 设置加载路径 if ( ! empty ( $options [ ' includepaths ' ])) { $this -> setIncludePaths( $options [ ' includepaths ' ]); } // 设置自动加载的命名空间 if ( ! empty ( $options [ ' autoloadernamespaces ' ])) { $this -> setAutoloaderNamespaces( $options [ ' autoloadernamespaces ' ]); } // 设置自动加载zend framework 的路径 if ( ! empty ( $options [ ' autoloaderzfpath ' ])) { $autoloader = $this -> getAutoloader(); if ( method_exists ( $autoloader , ' setZfPath ' )) { $zfPath = $options [ ' autoloaderzfpath ' ]; $zfVersion = ! empty ( $options [ ' autoloaderzfversion ' ]) ? $options [ ' autoloaderzfversion ' ] : ' latest ' ; $autoloader -> setZfPath( $zfPath , $zfVersion ); } } // 设置自定义的bootstrap类 if ( ! empty ( $options [ ' bootstrap ' ])) { $bootstrap = $options [ ' bootstrap ' ]; if ( is_string ( $bootstrap )) { $this -> setBootstrap( $bootstrap ); } elseif ( is_array ( $bootstrap )) { if ( empty ( $bootstrap [ ' path ' ])) { throw new Zend_Application_Exception( ' No bootstrap path provided ' ); } $path = $bootstrap [ ' path ' ]; $class = null ; if ( ! empty ( $bootstrap [ ' class ' ])) { $class = $bootstrap [ ' class ' ]; } $this -> setBootstrap( $path , $class ); } else { throw new Zend_Application_Exception( ' Invalid bootstrap information provided ' ); } } return $this ; } |
根据这段代码我们了解,当"config"不为空,递归加载配置文件。
我们可以有多个配置文件,但是在初始化Zend_Application对象的时候,只要把主配置文件的路径作为参数传给 Zend_Application的构造函数就可以了。
我们可以把不同的配置分成多个配置文件,如php配置的写在一个配置文件,数据库的配置写一个配置文件,引导资源的配置写一个配置文件,然后写一个主配置文件,其它包含之前的那些配置文件就可以了。如:
[ yourenvironment ] //注:实例化Zend_Application的第一个参数要为
yourenvironment;
|
这样,我们就不必把配置都写在一个配置文件中,可以分开几个,逻辑划分上更加清晰。
然后,键为phpsettings的,调用setPhpSettings()方法进行设置
也就是说php配置我们也可以写在Zend Framework的配置文件里。如下面是phpsettingconfig.ini的内容
[yourenvironment]
phpSettings.date.timezone
=
"
Asia/Shanghai
"
phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 |
再看一下如果在配置文件里设置自定义的bootstrap文件
[
yourenvironment
]
#可以是一个文件名字符串,此时你的自定义bootstrap类的类名必须是默认的 " Bootstrap " bootstrap = APP_ROOT " /application/bootstrap.php " #如果你的类名不叫 " Bootstrap " 而是叫 " MyBootstrap " , 你可以这样配置 bootstrap.path = APP_ROOT " /applicaton/bootstrap.php " bootstrap.class = " MyBootstrap " |
在Zend_Application中对配置项的引导,大概就这些。
引导资源的初始化,主要是在Zend_Application_Bootstrap_BootstrapAbstract类的setOptions()方法中。
Zend_Application_Bootstrap_BootstrapAbstract类的setOptions()方法主要功能是根据配置文件设置引导资源的加载目录,以及根据配置文件初始化引导资源。
根据配置文件初始化引导资源主要体现在如下代码中:
if ( in_array ( $method , $methods )) { |
也就是说配置文件中resources开头的配置项,都会作为引导资源。去看看你的application.ini把里面就有"resources".
如数据库的配置
[ yourenvionment ] |
如果把ini文件描述成一个数组,那么$ini['resources']['db']将作为一个构造函数的参数,传给Zend_Application_Resource_ResourceAbstract的构造函数。
流程说明: index.php 中$application->bootstrap() 调用 Zend_Application方法{ $this->getBootstrap()->bootstrap($resource); } //getBootStrap 实例化Zend_Application_Bootstrap_Bootstrap类 getBootStrap方法{ //$this 是Zend_Application对象自己 Zend_Application_Bootstrap_Bootstrap的构造函数调用其父类Zend_Application_Bootstrap_BootstrapAbstract的构造函数;
|
然后,adapter 映射 Zend_Application_Resource_Db类的中setAdapter()方法,$ini['resources']['db'] ['adapter']的值作为setAdapter()方法的参数。
params映射Zend_Application_Resource_Db类的中 setParams()方法,$ini['resources']['db']['params']的值作为setParams()方法的参数。