PHP-Yii执行流程分析(源码)

摘要:
|--yiicYiiLINUX命令行脚本|--yiic.batYIIWINDOW命令行脚本|--yiilite.php它是一些常用到的Yii类文件的合并文件。因此,使用yiilite.php将减少被引用的文件数量并避免执行跟踪语句二源码分析1.启动网站的唯一入口程序index.php:1.$yii=dirname.'/../framework/yii.php';2.$config=dirname.'/protected/config/main.php';3.4.//removethefollowinglinewheninproductionmode5.definedordefine;6.7.require_once;8.Yii::createWebApplication-˃run();上面的require_once引用出了后面要用到的全局类Yii,Yii类是YiiBase类的完全继承:1.classYiiextendsYiiBase2.{3.}系统的全局访问都是通过Yii类来实现的,Yii类的成员和方法都是static类型。
转自:http://www.cnblogs.com/zhanghaoyong/articles/2659846.html
一目录文件
|-framework框架核心库
|--base底 层类库文件夹,包含CApplication(应用类,负责全局的用户请求处理,它管理的应用组件集,将提供特定功能给整个应用程 序),CComponent(组件类,该文件包含了基于组件和事件驱动编程的基础类,从版本1.1.0开始,一个行为的属性(或者它的公共成员变量或它通 过getter和/或setter方法??定义的属性)可以通过组件的访问来调用),CBehavior(行为类,主要负责声明事件和相应事件处理程序的 方法、将对象的行为附加到组件等等),CModel(模型类,为所有的数据模型提供的基类),CModule(是模块和应用程序的基类,主要负责应用组件 和子模块)等等
|--caching所有缓存方法,其中包含了Memcache缓存,APC缓存,数据缓存,CDummyCache虚拟缓存,CEAcceleratorCache缓存等等各种缓存方法
|--cliYII项目生成脚本
|--collections用php语言构造传统OO语言的数据存储单元。如:队列,栈,哈希表等等
|--consoleYII控制台
|--db数据库操作类
|--giiYII代码生成器(脚手架),能生成包括模型,控制器,视图等代码
|--i18nYII多语言,提供了各种语言的本地化数据,信息、文件的翻译服务、本地化日期和时间格式,数字等
|--logging日 志组件,YII提供了灵活和可扩展的日志记录功能。消息记录可分为根据日志级别和信息类别。应用层次和类别过滤器,可进一步选择的消息路由到不同的目的 地,例如文件,电子邮件,浏览器窗口,等等|--messages提示信息的多语言
|--testYII提供的测试,包括单元测试和功能测试
|--utils提供了常用的格式化方法
|--validators提供了各种验证方法
|--vendors这个文件夹包括第三方由Yii框架使用的资料库
|--views提供了YII错误、日志、配置文件的多语言视图
|--webYII所有开发应用的方法
|---actions控制器操作类
|---auth权限认识类,包括身份认证,访问控制过滤,基本角色的访问控制等
|---filters过滤器,可被配置在控制器动作执行之前或之后执行。例如,访问控制过滤器将被执行以确保在执行请求的动作之前用户已通过身份验证;性能过滤器可用于测量控制器执行所用的时间
|---form表单生成方法
|---helpers视图助手,包含GOOGLEAJAXAPI,创建HTML,JSON,JAVASCRIPT相关功能
|---jsJS库
|---renderers视图渲染组件
|---services封装SoapServer并提供了一个基于WSDL的Web服务
|---widgets部件
|---CArrayDataProvider.php可以配置的排序和分页属性自定义排序和分页的行为
|---CActiveDataProvider.phpActiveRecord方法类
|---CController.php控制器方法,主要负责协调模型和视图之间的交互
|---CPagination.php分页类
|---CUploadedFile.php上传文件类
|---CUrlManager.phpURL管理
|---CWebModule.php应用模块管理,应用程序模块可被视为一个独立的子应用
等等方法
|--.htaccess重定向文件
|--yii.php引导文件
|--YiiBase.phpYiiBase类最主要的功能是注册了自动加载类方法,加载框架要用到所有接口。
|--yiicYiiLINUX命令行脚本
|--yiic.batYIIWINDOW命令行脚本
|--yiilite.php它是一些常用到的Yii类文件的合并文件。在文件中,注释和跟踪语句都被去除。因此,使用yiilite.php将减少被引用的文件数量并避免执行跟踪语句
二源码分析
1.启动
网站的唯一入口程序index.php:
1.$yii=dirname(__FILE__).'/../framework/yii.php';
2.$config=dirname(__FILE__).'/protected/config/main.php';
3.
4.//removethefollowinglinewheninproductionmode
5.defined('YII_DEBUG')ordefine('YII_DEBUG',true);
6.
7.require_once($yii);
8.Yii::createWebApplication($config)->run();
上面的require_once($yii)引用出了后面要用到的全局类Yii,Yii类是YiiBase类的完全继承
1.classYiiextendsYiiBase
2.{
3.}
系统的全局访问都是通过Yii类(即YiiBase类)来实现的,Yii类的成员和方法都是static类型
2.类加载
Yii利用PHP5提供的spl库来完成类的自动加载。在YiiBase.php文件结尾处
1.spl_autoload_register(array('YiiBase','autoload'));
将YiiBase类的静态方法autoload注册为类加载器。PHPautoload的简单原理就是执行new创建对象或通过类名访问静态成员时,系统将类名传递给被注册的类加载器函数,类加载器函数根据类名自行找到对应的类文件并include。
下面是YiiBase类的autoload方法:
1.publicstaticfunctionautoload($className)
2.{
3.//useincludesothattheerrorPHPfilemayappear
4.if(isset(self::$_coreClasses[$className]))
5.include(YII_PATH.self::$_coreClasses[$className]);
6.elseif(isset(self::$_classes[$className]))
7.include(self::$_classes[$className]);
8.else
9.include($className.'.php');
10.}
可以看到YiiBase的静态成员$_coreClasses数组里预先存放着Yii系统自身用到的类对应的文件路径:
1.privatestatic$_coreClasses=array(
2.'CApplication'=>'/base/CApplication.php',
3.'CBehavior'=>'/base/CBehavior.php',
4.'CComponent'=>'/base/CComponent.php',
5....
6.)
非coreClasse的类注册在YiiBase的$_classes数组中:
privatestatic$_classes=array();
其他的类需要用Yii::import()将类路径导入PHPincludepaths中,直接
include($className.'.php')

3.CWebApplication的创建
回到前面的程序入口的Yii::createWebApplication($config)->run();
1.publicstaticfunctioncreateWebApplication($config=null)
2.{
3.returnnewCWebApplication($config);
4.}
现在autoload机制开始工作了。
当系统执行newCWebApplication()的时候,会自动
include(YII_PATH.'/base/CApplication.php')
将main.php里的配置信息数组$config传递给CWebApplication创建出对象,并执行对象的run()方法启动框架。
CWebApplication类的继承关系
CWebApplication->CApplication->CModule->CComponent
$config先被传递给CApplication的构造函数
1.publicfunction__construct($config=null)
2.{
3.Yii::setApplication($this);
4.
5.//setbasePathatearlyaspossibletoavoidtrouble
6.if(is_string($config))
7.$config=require($config);
8.if(isset($config['basePath']))
9.{
10.$this->setBasePath($config['basePath']);
11.unset($config['basePath']);
12.}
13.else
14.$this->setBasePath('protected');
15.Yii::setPathOfAlias('application',$this->getBasePath());
16.Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
17.
18.$this->preinit();
19.
20.$this->initSystemHandlers();
21.$this->registerCoreComponents();
22.
23.$this->configure($config);
24.$this->attachBehaviors($this->behaviors);
25.$this->preloadComponents();
26.
27.$this->init();
28.}
Yii::setApplication($this);将自身的实例对象赋给Yii的静态成员$_app,以后可以通过Yii::app()来取得。
后面一段是设置CApplication对象的_basePath,指向proteced目录。
1.Yii::setPathOfAlias('application',$this->getBasePath());
2.Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
设置了两个系统路径别名application和webroot,后面再import的时候可以用别名来代替实际的完整路径。别名配置存放在YiiBase的$_aliases数组中。
$this->preinit();预初始化。preinit()是在CModule类里定义的,没有任何动作。
$this->initSystemHandlers()方法内容:
1./**
2.*Initializestheclassautoloaderanderrorhandlers.
3.
*/
4.protectedfunctioninitSystemHandlers()
5.{
6.if(YII_ENABLE_EXCEPTION_HANDLER)
7.set_exception_handler(array($this,'handleException'));
8.if(YII_ENABLE_ERROR_HANDLER)
9.set_error_handler(array($this,'handleError'),error_reporting());
10.}
设置系统exception_handler和error_handler,指向对象自身提供的两个方法。
4.注册核心组件
$this->registerCoreComponents();
代码如下:
1.protectedfunctionregisterCoreComponents()
2.{
3.parent::registerCoreComponents();
4.
5.$components=array(
6.'urlManager'=>array(
7.'class'=>'CUrlManager',
8.),
9.'request'=>array(
10.'class'=>'CHttpRequest',
11.),
12.'session'=>array(
13.'class'=>'CHttpSession',
14.),
15.'assetManager'=>array(
16.'class'=>'CAssetManager',
17.),
18.'user'=>array(
19.'class'=>'CWebUser',
20.),
21.'themeManager'=>array(
22.'class'=>'CThemeManager',
23.),
24.'authManager'=>array(
25.'class'=>'CPhpAuthManager',
26.),
27.'clientScript'=>array(
28.'class'=>'CClientScript',
29.),
30.);
31.
32.$this->setComponents($components);
33.}
注册了几个系统组件(Components)。
Components是在CModule里定义和管理的,主要包括两个数组
1.private$_components=array();
2.private$_componentConfig=array();
每个Component都是IApplicationComponent接口的实例,Componemt的实例存放在$_components数组里,相关的配置信息存放在$_componentConfig数组里。配置信息包括Component的类名和属性设置。
CWebApplication对 象注册了以下几个 Component:urlManager,request,session,assetManager,user,themeManager,authManager,clientScript。CWebApplication 的parent注册了以下几 个Component:coreMessages,db,messages,errorHandler,securityManager,statePersister。
Component在YiiPHP里是个非常重要的东西,它的特征是可以通过CModule的__get()和__set()方法来访问。Component注册的时候并不会创建对象实例,而是在程序里被第一次访问到的时候,由CModule来负责(实际上就是Yii::app())创建。
5.处理$config配置

继续,$this->configure($config);
configure()还是在CModule里:
1.publicfunctionconfigure($config)
2.{
3.if(is_array($config))
4.{
5.foreach($configas$key=>$value)
6.$this->$key=$value;
7.}
8.}
实际上是把$config数组里的每一项传给CModule的父类CComponent__set()方法。
1.publicfunction__set($name,$value)
2.{
3.$setter='set'.$name;
4.if(method_exists($this,$setter))
5.$this->$setter($value);
6.elseif(strncasecmp($name,'on',2)===0
7.&&method_exists($this,$name))
8.{
9.//duplicatinggetEventHandlers()hereforperformance
10.$name=strtolower($name);
11.if(!isset($this->_e[$name]))
12.$this->_e[$name]=newCList;
13.$this->_e[$name]->add($value);
14.}
15.elseif(method_exists($this,'get'.$name))
16.thrownewCException(Yii::t('yii','Property"{class}.{property}"isreadonly.',
17.array('{class}'=>get_class($this),'{property}'=>$name)));
18.else
19.thrownewCException(Yii::t('yii','Property"{class}.{property}"isnotdefined.',
20.array('{class}'=>get_class($this),'{property}'=>$name)));
21.}
22.}
我们来看看:
if(method_exists($this,$setter))
根据这个条件,$config数组里的basePath,params,modules,import,components都被传递给相应的setBasePath(),setParams()等方法里进行处理。

6、$config之import
其中import被传递给CModule的setImport:
1.publicfunctionsetImport($aliases)
2.{
3.foreach($aliasesas$alias)
4.Yii::import($alias);
5.}
Yii::import($alias)里的处理:
1.publicstaticfunctionimport($alias,$forceInclude=false)
2.{
3.//先判断$alias是否存在于YiiBase::$_imports[]中,已存在的直接return,避免重复import。
4.if(isset(self::$_imports[$alias]))//previouslyimported
5.returnself::$_imports[$alias];
6.
7.//$alias类已定义,记入$_imports[],直接返回
8.if(class_exists($alias,false))
9.returnself::$_imports[$alias]=$alias;
10.
11.//类似urlManager这样的已定义于$_coreClasses[]的类,或不含.的直接类名,记入$_imports[],直接返回
12.if(isset(self::$_coreClasses[$alias])||($pos=strrpos($alias,'.'))===false)//asimpleclassname
13.{
14.self::$_imports[$alias]=$alias;
15.if($forceInclude)
16.{
17.if(isset(self::$_coreClasses[$alias]))//acoreclass
18.require(YII_PATH.self::$_coreClasses[$alias]);
19.else
20.require($alias.'.php');
21.}
22.return$alias;
23.}
24.
25.//产生一个变量$className,为$alias最后一个.后面的部分
26.//这样的:'x.y.ClassNamer'
27.//$className不等于'*',并且ClassNamer类已定义的,ClassNamer'记入$_imports[],直接返回
28.if(($className=(string)substr($alias,$pos+1))!=='*'&&class_exists($className,false))
29.returnself::$_imports[$alias]=$className;
30.
31.//取得$alias里真实的路径部分并且路径有效
32.if(($path=self::getPathOfAlias($alias))!==false)
33.{
34.//$className!=='*',$className记入$_imports[]
35.if($className!=='*')
36.{
37.self::$_imports[$alias]=$className;
38.if($forceInclude)
39.require($path.'.php');
40.else
41.self::$_classes[$className]=$path.'.php';
42.return$className;
43.}
44.//$alias是'system.web.*'这样的已*结尾的路径,将路径加到include_path中
45.else//adirectory
46.{
47.set_include_path(get_include_path().PATH_SEPARATOR.$path);
48.returnself::$_imports[$alias]=$path;
49.}
50.}
51.else
52.thrownewCException(Yii::t('yii','Alias"{alias}"isinvalid.Makesureitpointstoanexistingdirectoryorfile.',
53.array('{alias}'=>$alias)));
54.}
7.$config之components
$config数组里的$components被传递给CModule的setComponents($components)
1.publicfunctionsetComponents($components)
2.{
3.foreach($componentsas$id=>$component)
4.{
5.if($componentinstanceofIApplicationComponent)
6.$this->setComponent($id,$component);
7.elseif(isset($this->_componentConfig[$id]))
8.$this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
9.else
10.$this->_componentConfig[$id]=$component;
11.}
12.}
$componen是IApplicationComponen的实例的时候,直接赋值:
$this->setComponent($id,$component),
1.publicfunctionsetComponent($id,$component)
2.{
3.$this->_components[$id]=$component;
4.if(!$component->getIsInitialized())
5.$component->init();
6.}
如果$id已存在于_componentConfig[]中(前面注册的coreComponent),将$component属性加进入。
其他的component将component属性存入_componentConfig[]中。

8.$config之params

这个很简单
1.publicfunctionsetParams($value)
2.{
3.$params=$this->getParams();
4.foreach($valueas$k=>$v)
5.$params->add($k,$v);
6.}
configure完毕!
9.attachBehaviors
$this->attachBehaviors($this->behaviors);
空的,没动作
预创建组件对象
1.$this->preloadComponents();
2.
3.protectedfunctionpreloadComponents()
4.{
5.foreach($this->preloadas$id)
6.$this->getComponent($id);
7.}
getComponent()判断_components[]数组里是否有$id的实例,如果没有,就根据_componentConfig[$id]里的配置来创建组件对象,调用组件的init()方法,然后存入_components[$id]中。

10.init()

this->init();
函数内:$this->getRequest();
创建了Reques组件并初始化。
11.run()
1.publicfunctionrun()
2.{
3.$this->onBeginRequest(newCEvent($this));
4.$this->processRequest();
5.$this->onEndRequest(newCEvent($this));
6.}
三大概过程
application构造函数:
1设置当前运行实例
2获取配置参数
3设置basepath
4设置几个path;application,webroot,ext
5preinit
6注册error、exception处理函数initSystemHandlers
7加载核心组件registerCoreComponents包括webapplication的和application的
8设置配置文件configure($config)
9附加行为$this->attachBehaviors($this->behaviors);
10处理加载config中的preload,//通过getComponent分别加载并初始化$this->preloadComponents();
11初始化init();//加载CHttpRequest组件

run:
1处理onBeginRequest
2processRequest();真正处理请求
3处理onEndRequest

webapplication->processRequest():
1如果配置文件设置了catchAllRequest,//'catchAllRequest'=>array('site/error','p1'=>'1','p2'=>'2'),
则所有请求都跳转到这个controller/action这个route,并且设置$_GET参数
2分析url得到route,便于后面的控制器/动作创建
3执行runController

runController:
1创建controller,createController(),创建失败,则抛出404错误
2得到controller对象和actionID
3控制器初始化$controller->init();
4最后执行$controller->run($actionID);//真正执行页面请求
控制器类
CController:默认控制器在CWebApplication::defaultController定义('site'),可以在配置文件修改
run():
1//根据actionID创建action对象,这里生成的action对象分为定义在controller内联动作和自定义action,比如CViewAction
$action=$this->createAction($actionID),如果创建动作失败,missingAction抛出404错误
2beforeControllerAction(beforeControllerAction定义在CWebApplication,有时也在module里面)为真,才执行runActionWithFilters;
3afterControllerAction
runActionWithFilters($action,$this->filters()):
1//如果过滤器为空,直接运行runAction()
2执行过滤器链
runAction():
1beforeAction()返回真,才执行
2执行$action->runWithParams();注意:这里存在多态,每个action都可以实现这个方法,因为CInlineAction自己实现了runWithParams()
3第2步骤为真,才执行afterAction($action);
动作类默认动作在CController::$defaultAction定义('index'),可以在CController的继承类重新定义
runWithParams():
1分为2种情况,1种是内联动作,1种是通过控制器的actions方法定义的外联动作。
2内联动作通过action+动作id作为动作处理函数
3外联动作通过调用run()函数来实现
4如果动作方法参数个数大于0,执行runWithParamsInternal,否则直接执行动作方法。
runWithParamsInternal();
1根据反射的方法对象得到方法的形参列表,从控制器对象->getActionParams()得到实参,
如果实参有形参要求的参数,取其值,不然取形参默认值,否则,出错。
2调用动作方法2种形式1是action+动作id,2是Caction的派生类(比如cviewaction)的run()
3执行控制器的CController->render方法;$controller->render($view)
控制器类
CController:
render();
1renderPartial();得到视图,//先得到contact页面的view文件内容,注意是用include的形式,所以其中的$this是指siteControlerd对象,
这里调用了renderFile();
2然后$output=$this->renderFile($layoutFile,array('content'=>$output),true)
把view中的内容插入到布局页面layouts的column1.php,'content'和layout的页面的$content变量相关
renderFile();
1如果程序没有定义viewrender,则执行controller->renderInternal();否则,执行$renderer=Yii::app()->getViewRenderer())->renderFile();
viewsourceprint?发生404错误
errorHandler在配置文件main中,'errorAction'=>'site/error',
**********************************************************
runActionWithFilters
过滤:
CFilterChain(继承CList,提供数字索引存取功能,遍历)::create($this,$action,$filters)->run();
首先创建过滤链,然后执行过滤
CFilterChain::create($controller,$action,$filters):
1$chain=newCFilterChain($controller,$action);创建一个过滤链$chain
2根据参数filters数组,遍历创建过滤器$filter(字符串:通过CInlineFilter::create或者数组:Yii::createComponent),
并且初始化$filter::init,通过$chain->add($filter)添加到过滤链中,并且返回这个过滤链$chain
注意:如果是字符串,控制器类controller必须要有"filter"+过滤器名的方法。
$chain::run();
1如果数字索引合法,得到$filter,然后执行$filter->filter($this);
1.1$filter->filter($this):
1执行动作的'filter'+过滤器名称的方法。//比如CController::filterAccessControl($filterChain);
1.1.1CController::filterAccessControl($filterChain):
1$filter=newCAccessControlFilter;//新建过滤器
2$filter->setRules($this->accessRules());//设置规则
3$filter->filter($filterChain);//执行过滤
4$filter->preFilter($filterChain)为真,继续执行$filterChain->run();
5$filter->postFilter($filterChain);//这个是在动作执行之后过滤
2否则,说明过滤完毕,$this->controller->runAction($this->action);直接执行动作。

免责声明:文章转载自《PHP-Yii执行流程分析(源码)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Gradle 源配置SetTimer的使用问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

开源gvSIG环境搭建

gvSIG网上资料实在太少,自己Java功底也不深,所以感觉比较头疼,好歹周围有不少Java高手,可以请教,在自己深入的研究和高手的指导下,终于能够运行起来,回过头来说说,环境搭建,也让后来者能有点资料可查。 1.从http://www.gvsig.org/web/projects/gvsig-mobile/网站上下载gvSIG源码project_gvSI...

使用autotools自动生成Makefile并在此之上使用dh-make生成可发布的deb程序包(详解)

转自:http://blog.csdn.net/longerzone/article/details/12705507 一、前言 本文将介绍如何使用autotools生成一个Makefile文件,并在此基础上使用dh-make和debuild生成一个可发布的deb程序包,这也是我们在Linux下开发应用程序以及想要发布应用程序需要做的。 无论是在Linux...

SpringCloud(9)----mysql实现配置中心

本公司配置数据的管理是通过mysql进行配置管理,因为已经搭建好了,所以自己动手重新搭建一遍,熟悉整个流程。有关项目源码后期会补上github地址 微服务要实现集中管理微服务配置、不同环境不同配置、运行期间也可动态调整、配置修改后可以自动更新的需求,Spring Cloud Config同时满足了以上要求。 项目代码GitHub地址:https://git...

IntelliJ IDEA 更新

  一. 下载最新版本的idea          1. 官网下载,官网地址:http://www.jetbrains.com/idea/download/#section=windows          2. 百度网盘直接下载:https://pan.baidu.com/s/1qmNhRI7lSo9G-QEMDPQZfg , 密码:1jxd   二....

UVM_COOKBOOK学习【DUT-Testbench Connections】

关注微信公众号摸鱼范式,后台回复COOKBOOK获取COOKBOOK原本和译本 PDF度盘链接 将testbench连接到DUT 概述 本节,我们主要讨论将UVM testbench连接到RTL DUT的问题。 UVM testbench对象不能直接连接到DUT信号来驱动或采样。driver和monitor组件对象与DUT之间的连接是通过一个或多个具有静态...

C# 操作自定义config文件

示例文件:DB.config 1.读取 1 //先实例化一个ExeConfigurationFileMap对象,把物理地址赋值到它的 ExeConfigFilename 属性中; 2 ExeConfigurationFileMap fileMap = newExeConfigurationFileMap(); 3 fileMap.ExeConfigFi...