yii执行流程

  1. 启动
    网站的唯一入口程序 index.php :
    1. $yii=dirname(FILE).'/../framework/yii.php';

    2. $config=dirname(FILE).'/protected/config/main.php';

    3. // remove the following line when in production mode

    4. defined('YII_DEBUG') or define('YII_DEBUG',true);

    5. require_once($yii);

    6. Yii::createWebApplication($config)->run();

上面的require_once($yii) 引用出了后面要用到的全局类Yii,Yii类是YiiBase类的完全继承:

  1. class Yii extends YiiBase

  2. {

  3. }

系统的全局访问都是通过Yii类(即YiiBase类)来实现的,Yii类的成员和方法都是static类型。

  1. 类加载

Yii利用PHP5提供的spl库来完成类的自动加载。在YiiBase.php 文件结尾处

  1. spl_autoload_register(array('YiiBase','autoload'));

将YiiBase类的静态方法autoload 注册为类加载器。 PHP autoload 的简单原理就是执行 new 创建对象或通过类名访问静态成员时,系统将类名传递给被注册的类加载器函数,类加载器函数根据类名自行找到对应的类文件并include 。

下面是YiiBase类的autoload方法:

  1. public static function autoload($className)

  2. {

  3. // use include so that the error PHP file may appear

  4. if(isset(self::$_coreClasses[$className]))

  5. include(YII_PATH.self::$_coreClasses[$className]);  
    
  6. else if(isset(self::$_classes[$className]))

  7. include(self::$_classes[$className]);  
    
  8. else

  9. include($className.'.php');  
    
  10. }

可以看到YiiBase的静态成员$_coreClasses 数组里预先存放着Yii系统自身用到的类对应的文件路径:

  1. private static $_coreClasses=array(

  2. 'CApplication' => '/base/CApplication.php',

  3. 'CBehavior' => '/base/CBehavior.php',

  4. 'CComponent' => '/base/CComponent.php',

  5. ...

  6. )

非 coreClasse 的类注册在YiiBase的$_classes 数组中:

private static $_classes=array();

其他的类需要用Yii::import()讲类路径导入PHP include paths 中,直接

include($className.'.php')

  1. CWebApplication的创建

回到前面的程序入口的 Yii::createWebApplication($config)->run();

  1. public static function createWebApplication($config=null)

  2. {

  3. return new CWebApplication($config);

  4. }

现在autoload机制开始工作了。

当系统 执行 new CWebApplication() 的时候,会自动

include(YII_PATH.'/base/CApplication.php')

将main.php里的配置信息数组$config传递给CWebApplication创建出对象,并执行对象的run() 方法启动框架。
CWebApplication类的继承关系

CWebApplication -> CApplication -> CModule -> CComponent

$config先被传递给CApplication的构造函数

  1. public function __construct($config=null)

  2. {

  3. Yii::setApplication($this);

  4. // set basePath at early as possible to avoid trouble

  5. if(is_string($config))

  6. $config=require($config);  
    
  7. if(isset($config['basePath']))

  8. {

  9. $this->setBasePath($config['basePath']);  
    
  10. unset($config['basePath']);  
    
  11. }

  12. else

  13. $this->setBasePath('protected');  
    
  14. Yii::setPathOfAlias('application',$this->getBasePath());

  15. Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));

  16. $this->preinit();

  17. $this->initSystemHandlers();

  18. $this->registerCoreComponents();

  19. $this->configure($config);

  20. $this->attachBehaviors($this->behaviors);

  21. $this->preloadComponents();

  22. $this->init();

  23. }

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. protected function initSystemHandlers()

  2. {

  3. if(YII_ENABLE_EXCEPTION_HANDLER)

  4. set_exception_handler(array($this,'handleException'));  
    
  5. if(YII_ENABLE_ERROR_HANDLER)

  6. set_error_handler(array($this,'handleError'),error_reporting());    
    
  7. }

设置系统exception_handler和 error_handler,指向对象自身提供的两个方法。

  1. 注册核心组件

$this->registerCoreComponents();

代码如下:

  1. protected function registerCoreComponents()

  2. {

  3. parent::registerCoreComponents();

  4. $components=array(

  5. 'urlManager'=>array(  
    
  6.  'class'=>'CUrlManager',  
    
  7. ),  
    
  8. 'request'=>array(  
    
  9.  'class'=>'CHttpRequest',  
    
  10. ),  
    
  11. 'session'=>array(  
    
  12.  'class'=>'CHttpSession',  
    
  13. ),  
    
  14. 'assetManager'=>array(  
    
  15.  'class'=>'CAssetManager',  
    
  16. ),  
    
  17. 'user'=>array(  
    
  18.  'class'=>'CWebUser',  
    
  19. ),  
    
  20. 'themeManager'=>array(  
    
  21.  'class'=>'CThemeManager',  
    
  22. ),  
    
  23. 'authManager'=>array(  
    
  24.  'class'=>'CPhpAuthManager',  
    
  25. ),  
    
  26. 'clientScript'=>array(  
    
  27.  'class'=>'CClientScript',  
    
  28. ),  
    
  29. );

  30. $this->setComponents($components);

  31. }

注册了几个系统组件(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())创建。

  1. 处理 $config 配置

继续, $this->configure($config);

configure() 还是在CModule 里:

  1. public function configure($config)

  2. {

  3. if(is_array($config))

  4. {

  5. foreach($config as $key=>$value)  
    
  6.  $this->$key=$value;  
    
  7. }

  8. }

实际上是把$config数组里的每一项传给 CModule 的 父类 CComponent __set() 方法。

  1. public function __set($name,$value)

  2. {

  3. $setter='set'.$name;

  4. if(method_exists($this,$setter))

  5. $this->$setter($value);  
    
  6. else if(strncasecmp($name,'on',2)===0

  7.            && method_exists($this,$name))  
    
  8. {

  9. //duplicating getEventHandlers() here for performance  
    
  10. $name=strtolower($name);  
    
  11. if(!isset($this->_e[$name]))  
    
  12.  $this->_e[$name]=new CList;  
    
  13.  $this->_e[$name]->add($value);  
    
  14. }  
    
  15. else if(method_exists($this,'get'.$name))  
    
  16.  throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',  
    
  17.  array('{class}'=>get_class($this), '{property}'=>$name)));  
    
  18. else 
    
  19.  throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',  
    
  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. public function setImport($aliases)

  2. {

  3. foreach($aliases as $alias)

  4. Yii::import($alias);  
    
  5. }

Yii::import($alias)里的处理:

  1. public static function import($alias,$forceInclude=false)

  2. {

  3. // 先判断$alias是否存在于YiiBase::$_imports[] 中,已存在的直接return, 避免重复import。

  4. if(isset(self::$_imports[$alias])) // previously imported

  5. return self::$_imports[$alias];  
    
  6. // $alias类已定义,记入$_imports[],直接返回

  7. if(class_exists($alias,false))

  8. return self::$_imports[$alias]=$alias;  
    
  9. // 类似 urlManager 这样的已定义于$_coreClasses[]的类,或不含.的直接类名,记入$_imports[],直接返回

  10. if(isset(self::$_coreClasses[$alias]) || ($pos=strrpos($alias,'.'))===false) // a simple class name

  11. {

  12. self::$_imports[$alias]=$alias;  
    
  13. if($forceInclude)  
    
  14. {  
    
  15.  if(isset(self::$_coreClasses[$alias])) // a core class  
    
  16.   require(YII_PATH.self::$_coreClasses[$alias]);  
    
  17.  else 
    
  18.   require($alias.'.php');  
    
  19. }  
    
  20. return $alias;  
    
  21. }

  22. // 产生一个变量 $className,为$alias最后一个.后面的部分

  23. // 这样的:'x.y.ClassNamer'

  24. // $className不等于 '*', 并且ClassNamer类已定义的, ClassNamer' 记入 $_imports[],直接返回

  25. if(($className=(string)substr($alias,$pos+1))!=='*' && class_exists($className,false))

  26. return self::$_imports[$alias]=$className;  
    
  27. // 取得 $alias 里真实的路径部分并且路径有效

  28. if(($path=self::getPathOfAlias($alias))!==false)

  29. {

  30. // $className!=='*',$className 记入 $_imports[]  
    
  31. if($className!=='*')  
    
  32. {  
    
  33.  self::$_imports[$alias]=$className;  
    
  34.  if($forceInclude)  
    
  35.   require($path.'.php');  
    
  36.  else 
    
  37.   self::$_classes[$className]=$path.'.php';  
    
  38.  return $className;  
    
  39. }  
    
  40. // $alias是'system.web.*'这样的已*结尾的路径,将路径加到include_path中  
    
  41. else // a directory  
    
  42. {  
    
  43.  set_include_path(get_include_path().PATH_SEPARATOR.$path);  
    
  44.  return self::$_imports[$alias]=$path;  
    
  45. }  
    
  46. }

  47. else

  48. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',  
    
  49.  array('{alias}'=>$alias)));  
    
  50. }

  51. $config 之 components

$config 数组里的 $components 被传递给CModule 的setComponents($components)

  1. public function setComponents($components)

  2. {

  3. foreach($components as $id=>$component)

  4. {

  5. if($component instanceof IApplicationComponent)  
    
  6.  $this->setComponent($id,$component);  
    
  7. else if(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. public function setComponent($id,$component)

  2. {

  3. $this->_components[$id]=$component;

  4. if(!$component->getIsInitialized())

  5. $component->init();  
    
  6. }

如果$id已存在于_componentConfig[]中(前面注册的coreComponent),将$component 属性加进入。

其他的component将component属性存入_componentConfig[]中。

  1. $config 之 params

这个很简单

  1. public function setParams($value)

  2. {

  3. $params=$this->getParams();

  4. foreach($value as $k=>$v)

  5. $params->add($k,$v);  
    
  6. }

configure 完毕!

  1. attachBehaviors

$this->attachBehaviors($this->behaviors);

空的,没动作

预创建组件对象

  1. $this->preloadComponents();

  2. protected function preloadComponents()

  3. {

  4. foreach($this->preload as $id)

  5. $this->getComponent($id);  
    
  6. }

getComponent() 判断_components[] 数组里是否有 $id的实例,如果没有,就根据_componentConfig[$id]里的配置来创建组件对象,调用组件的init()方法,然后存入_components[$id]中。

  1. init()

$this->init();

函数内:$this->getRequest();

创建了Reques 组件并初始化。

  1. run()

  2. public function run()

  3. {

  4. $this->onBeginRequest(new CEvent($this));

  5. $this->processRequest();

  6. $this->onEndRequest(new CEvent($this));

  7. }

转载于:https://my.oschina.net/u/925485/blog/162580

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值