轉載鏈接:http://tech.ddvip.com/2013-11/1384432766205970.html
一 ?程序入口
<?php// change the following paths if necessary
$yii=dirname(__FILE__).'/http://www.cnblogs.com/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/main.php';// remove the following line when in production mode
// defined('YII_DEBUG') or define('YII_DEBUG',true);require_once($yii);
Yii::createWebApplication($config)->run();
require_once($yii) 語句包含了yii.php 文件,該文件是Yii bootstrap file,包含了 yiibase的基礎類,yii完全繼承了yiibase
<?php
/*** Yii bootstrap file.** @author Qiang Xue <qiang.xue@gmail.com>* @link http://www.yiiframework.com/* @copyright Copyright ? 2008-2011 Yii Software LLC* @license http://www.yiiframework.com/license/* @version $Id: yii.php 2799 2011-01-01 19:31:13Z qiang.xue $* @package system* @since 1.0*/require(dirname(__FILE__).'/YiiBase.php');/*** Yii is a helper class serving common framework functionalities.** It encapsulates {@link YiiBase} which provides the actual implementation.* By writing your own Yii class, you can customize some functionalities of YiiBase.** @author Qiang Xue <qiang.xue@gmail.com>* @version $Id: yii.php 2799 2011-01-01 19:31:13Z qiang.xue $* @package system* @since 1.0*/
class Yii extends YiiBase
{
}
在 YiiBase 類中 定義了一些 比如:
public static function createWebApplication($config=null) // 創建啟動public static function import($alias,$forceInclude=false) // 類導入public static function createComponent($config) // 創建組件public static function setApplication($app) // 創建類的實例 yii::app()
二 自動加載機制
還有比較重要的yii自動加載機制,在yiibase的最后引用了php的標準庫函數 spl_autoload_register(array('YiiBase','autoload')) 調用框架中的autoload方法
/*** Class autoload loader.* This method is provided to be invoked within an __autoload() magic method.* @param string $className class name* @return boolean whether the class has been loaded successfully*/public static function autoload($className){// use include so that the error PHP file may appearif(isset(self::$classMap[$className]))include(self::$classMap[$className]);else if(isset(self::$_coreClasses[$className]))include(YII_PATH.self::$_coreClasses[$className]);else{// include class file relying on include_pathif(strpos($className,'')===false) // class without namespace{if(self::$enableIncludePath===false){foreach(self::$_includePaths as $path){$classFile=$path.DIRECTORY_SEPARATOR.$className.'.php';if(is_file($classFile)){include($classFile);break;}}}elseinclude($className.'.php');}else // class name with namespace in PHP 5.3{$namespace=str_replace('','.',ltrim($className,''));if(($path=self::getPathOfAlias($namespace))!==false)include($path.'.php');elsereturn false;}return class_exists($className,false) || interface_exists($className,false);}return true;}
靜態成員 $_coreClasses 變量中定義了一些系統自身的核心類
private static $_coreClasses=array('CApplication' => '/base/CApplication.php','CApplicationComponent' => '/base/CApplicationComponent.php','CBehavior' => '/base/CBehavior.php','CComponent' => '/base/CComponent.php',
非 coreClasse 的類注冊在YiiBase的$_classes 數組中: ?
private static $_classes=array(); ? ?
其他的類需要用Yii::import()講類路徑導入PHP include paths 中,直接 ?
include($className.'.php')?
三 CWebApplication的創建 ?
Yii::createWebApplication($config)->run(); 調用createWebApplication函數
public static function createWebApplication($config=null){return self::createApplication('CWebApplication',$config); // 函數中調用createApplication}
public static function createApplication($class,$config=null){return new $class($config);}
返回 CWebApplication類的實例
現在autoload機制開始工作了。 ?
當系統 執行 new CWebApplication() 的時候,會自動 ?
include(YII_PATH.'/base/CApplication.php') ?
幾個類的繼承關系是 CWebApplication->CApplication->CModule->CComponent
$config 首先傳遞到CApplication的構造函數中,
public function __construct($config=null){Yii::setApplication($this); // 返回自身的實例,之后可以通過 yii::app() 全局調用// set basePath at early as possible to avoid troubleif(is_string($config))$config=require($config);if(isset($config['basePath'])){$this->setBasePath($config['basePath']);unset($config['basePath']);}else$this->setBasePath('protected'); // 設置路徑 指向protected 目錄Yii::setPathOfAlias('application',$this->getBasePath());Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');$this->preinit();$this->initSystemHandlers(); 設置error 和 exception$this->registerCoreComponents(); 注冊核心組件,放入_componentConfig 靜態變量中$this->configure($config); // 把配置文件數組循環,設置為自身屬性$this->attachBehaviors($this->behaviors); // 設置行為$this->preloadComponents(); // 預加載$this->init(); // 加載請求處理模塊,開始處理請求}
大概過程 ?
application構造函數: ?
1 設置當前運行實例 ?
2 獲取配置參數 ?
3 設置basepath ?
4 設置幾個path;application,webroot ,ext ?
5 preinit ?
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組件?