分享

Yii分析1:web程序入口(1)

 corefashion 2014-05-15

以下分析基于Yii v1.0.6

 

Yii_PATH表示framework的路径

 

通常使用Yii框架的index.php程序如下:

Php代码  收藏代码
  1. // change the following paths if necessary  
  2. $yii    = dirname(__FILE__).'/protected/lib/Yii/framework/yii.php';  
  3. $config = dirname(__FILE__).'/protected/config/main.php';  
  4.   
  5. // remove the following line when in production mode  
  6. defined('YII_DEBUG') or define('YII_DEBUG',true);  
  7.   
  8. require_once $yii;  
  9. $app = Yii::CreateWebApplication($config);  
  10. $app->run();  

 

我们来看一下Yii::CreateWebApplication的过程:

 

Yii其实是YiiBase的helper,因此我们实际查看的是YiiBase::CreateWebApplication

 

Yii_PATH/YiiBase.php:

 

Php代码  收藏代码
  1. class YiiBase  
  2. {  
  3. ……  
  4.     public static function createWebApplication($config=null)  
  5.     {  
  6.         return new CWebApplication($config);  
  7.     }  
  8. ……  
  9.     //自动类加载函数  
  10.     public static function autoload($className)  
  11.     {  
  12.   
  13.         // use include so that the error PHP file may appear  
  14.         if(isset(self::$_coreClasses[$className]))  
  15.             include(YII_PATH.self::$_coreClasses[$className]);  
  16.         else if(isset(self::$_classes[$className]))  
  17.             include(self::$_classes[$className]);  
  18.         else  
  19.         {  
  20.             include($className.'.php');  
  21.             return class_exists($className,false) || interface_exists($className,false);  
  22.         }  
  23.         return true;  
  24.     }  
  25. ……  
  26.   
  27.     //核心类列表  
  28.     private static $_coreClasses=array(  
  29.         'CApplication' => '/base/CApplication.php',  
  30.         'CApplicationComponent' => '/base/CApplicationComponent.php',  
  31.         'CBehavior' => '/base/CBehavior.php',  
  32.         ……  
  33.     );  
  34.   
  35. }  
  36. //注册自动类加载函数  
  37. spl_autoload_register(array('YiiBase','autoload'));  
  38. require(YII_PATH.'/base/interfaces.php');  

 

这里返回的是一个CWebApplication的对象,

 

Yii_PATH/web/CWebApplication.php

Java代码  收藏代码
  1. class CWebApplication extends CApplication  
  2. {  
  3. ……  
  4. }  

 CWebApplication继承自CApplication,没有自定义的constructor,因此我们继续查看CApplication的constructor:

 

Yii_PATH/base/CApplication.php

 

Php代码  收藏代码
  1. abstract class CApplication extends CModule  
  2. {  
  3. ……  
  4.     public function __construct($config=null)  
  5.     {  
  6.         Yii::setApplication($this);  
  7.         // set basePath at early as possible to avoid trouble  
  8.         if(is_string($config))  
  9.             $config=require($config);  
  10.         if(isset($config['basePath']))  
  11.         {  
  12.             $this->setBasePath($config['basePath']);  
  13.             unset($config['basePath']);  
  14.         }  
  15.         else  
  16.             $this->setBasePath('protected');  
  17.         Yii::setPathOfAlias('application',$this->getBasePath());  
  18.         Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));  
  19.   
  20.         $this->preinit();  
  21.   
  22.         $this->initSystemHandlers();  
  23.         $this->registerCoreComponents();  
  24.   
  25.         $this->configure($config);  
  26.         $this->attachBehaviors($this->behaviors);  
  27.         $this->preloadComponents();  
  28.   
  29.         $this->init();  
  30.     }  
  31. ……  
  32. }  

 这里,做了很多工作,我们来慢慢分析:

 

Php代码  收藏代码
  1. Yii::setApplication($this);  

 对应代码如下:

Php代码  收藏代码
  1. public static function setApplication($app)  
  2. {  
  3.     if(self::$_app===null || $app===null)  
  4.         self::$_app=$app;  
  5.     else  
  6.         throw new CException(Yii::t('yii','Yii application can only be created once.'));  
  7. }  

 这里只是set一下application的名称,ok,继续:

Php代码  收藏代码
  1. if(is_string($config))  
  2.     $config=require($config);  
  3. if(isset($config['basePath']))  
  4. {  
  5.     $this->setBasePath($config['basePath']);  
  6.     unset($config['basePath']);  
  7. }  
  8. else  
  9.     $this->setBasePath('protected');  

 这里主要是将createWebApplication时穿过来的配置文件require了一下,然后拿到配置项中的basePath,设置成员变量:

Php代码  收藏代码
  1. public function setBasePath($path)  
  2. {  
  3.     if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))  
  4.         throw new CException(Yii::t('yii','Application base path "{path}" is not a valid directory.',  
  5.             array('{path}'=>$path)));  
  6. }  

 之后:

Php代码  收藏代码
  1. Yii::setPathOfAlias('application',$this->getBasePath());  
  2. Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));  

 通过下面的函数设置路径的别名:

Php代码  收藏代码
  1. public static function setPathOfAlias($alias,$path)  
  2. {  
  3.     if(emptyempty($path))  
  4.         unset(self::$_aliases[$alias]);  
  5.     else  
  6.         self::$_aliases[$alias]=rtrim($path,'\\/');  
  7. }  

 保存在$_aliases数组中,接下来是一些初始化的工作(未完待续):

Php代码  收藏代码
  1. $this->preinit();  

 调用的是Yii_PATH/base/CModule.php中的一个空函数,用于初始化模块(子类覆盖)

Php代码  收藏代码
  1. protected function preinit(){  
  2. }  

 

接下篇: Yii分析1:web程序入口(2)

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多