Zend Framework 框架学习(三)

因为写在线写blog很是不方便,所以就装了一个Zoundry Raven,没想到呀!

把我《Zend Framework 框架学习(二)》给全毁了呀!杯具。。。 以后再重写吧。

Zend Framework项目的应用程序一般都是通过 index.php 来引导和初始化,主要由Zend_Application类对配置文件进行加载。

// Define path to application directory
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';

//
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()->run();

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的构造函数如下

/**
* Constructor
*
* Initialize application. Potentially initializes include_paths, PHP
* settings, and bootstrap class.
*
* @param string $environment
* @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options
* @throws Zend_Application_Exception When invalid options are provided
* @return void
*/
public function __construct($environment, $options = null)
{

$this->_environment = (string) $environment;

require_once 'Zend/Loader/Autoloader.php';
$this->_autoloader = Zend_Loader_Autoloader::getInstance();

if (null !== $options) {

//将$options数组化;如果是string使用_loadConfig数组化;如果是继承于Zend_Config实例化的对象使用toArray方法数组化;
if (is_string($options)) {
$options = $this->_loadConfig($options);
} elseif ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (!is_array($options)) {
throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');
}

$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
                  
                  ]
                  
                    
config.phpsettingconfig
= APP_ROOT "/config/phpsettingconfig.ini"
config.dbconfig
= APP_ROOT "/config/dbconfig.ini"
config.resourceconfig
= APP_ROOT "/config/resourceconfig.ini"
//注:实例化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
此设置跟php配置文件php.ini基本一样,只要在设置项前加phpSettings就可以了(phpSettings大小写不限)

再看一下如果在配置文件里设置自定义的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
                  )) {
$this -> $method ( $value );
}
elseif ( ' resources ' == $key ) {
// 或者是注册引导资源
foreach ( $value as $resource => $resourceOptions ) {
$this -> registerPluginResource( $resource , $resourceOptions );
}
}

也就是说配置文件中resources开头的配置项,都会作为引导资源。去看看你的application.ini把里面就有"resources".

如数据库的配置

[
                  yourenvionment
                  ]
                  
                    
resources.db.adapter
= PDO_MYSQL
resources.db.params.host
= localhost
resources.db.params.username
= root
resources.db.params.password
=
resources.db.params.dbname
= test

如果把ini文件描述成一个数组,那么$ini['resources']['db']将作为一个构造函数的参数,传给Zend_Application_Resource_ResourceAbstract的构造函数。

流程说明:

index.php 中$application->bootstrap() 调用 Zend_Application方法{

$this->getBootstrap()->bootstrap($resource);

}

//getBootStrap 实例化Zend_Application_Bootstrap_Bootstrap类

getBootStrap方法{
if (null === $this->_bootstrap) {

//$this 是Zend_Application对象自己
$this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);
}
return $this->_bootstrap;
}

Zend_Application_Bootstrap_Bootstrap的构造函数调用其父类Zend_Application_Bootstrap_BootstrapAbstract的构造函数;

/**
* Constructor
*
* Sets application object, initializes options, and prepares list of
* initializer methods.
*
* @param Zend_Application|Zend_Application_Bootstrap_Bootstrapper $application
* @return void
* @throws Zend_Application_Bootstrap_Exception When invalid applicaiton is provided
*/
public function __construct($application)
{
$this->setApplication($application);

//调用Zend_Application的getOptions方法
$options = $application->getOptions();
$this->setOptions($options);
}

然后,adapter 映射 Zend_Application_Resource_Db类的中setAdapter()方法,$ini['resources']['db'] ['adapter']的值作为setAdapter()方法的参数。

params映射Zend_Application_Resource_Db类的中 setParams()方法,$ini['resources']['db']['params']的值作为setParams()方法的参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值