1. 启动 网站的唯一入口程序 index.php : $yii=dirname(__file__)./../framework/yii.php;$config=dirname(__file__)./protected/config/main.php;// remove the following line when in production modedefined(yii_debug) or define(yii_debug,true);requi
1. 启动网站的唯一入口程序 index.php :
$yii=dirname(__file__).’/../framework/yii.php’;$config=dirname(__file__).’/protected/config/main.php’;// remove the following line when in production modedefined(‘yii_debug’) or define(‘yii_debug’,true);require_once($yii);yii::createwebapplication($config)->run();
上面的require_once($yii) 引用出了后面要用到的全局类yii,yii类是yiibase类的完全继承:
class yii extends yiibase{}
系统的全局访问都是通过yii类(即yiibase类)来实现的,yii类的成员和方法都是static类型。
2. 类加载yii利用php5提供的spl库来完成类的自动加载。在yiibase.php 文件结尾处
spl_autoload_register(array(‘yiibase’,'autoload’));
将yiibase类的静态方法autoload 注册为类加载器。 php autoload 的简单原理就是执行 new 创建对象或通过类名访问静态成员时,系统将类名传递给被注册的类加载器函数,类加载器函数根据类名自行找到对应的类文件并include 。
下面是yiibase类的autoload方法:
public static function autoload($classname){ // use include so that the error php file may appear if(isset(self::$_coreclasses[$classname])) include(yii_path.self::$_coreclasses[$classname]); else if(isset(self::$_classes[$classname])) include(self::$_classes[$classname]); else include($classname.’.php’);}
可以看到yiibase的静态成员$_coreclasses 数组里预先存放着yii系统自身用到的类对应的文件路径:
private static $_coreclasses=array( ‘capplication’ => ‘/base/capplication.php’, ‘cbehavior’ => ‘/base/cbehavior.php’, ‘ccomponent’ => ‘/base/ccomponent.php’, …)
非 coreclasse 的类注册在yiibase的$_classes 数组中:
private static $_classes=array();
其他的类需要用yii::import()将类路径导入php include paths 中,直接
include($classname.’.php’)
3. cwebapplication的创建回到前面的程序入口的 yii::createwebapplication($config)->run();
public static function createwebapplication($config=null){ return new cwebapplication($config);}
现在autoload机制开始工作了。
当系统 执行 new cwebapplication() 的时候,会自动
include(yii_path.’/base/capplication.php’)
将main.php里的配置信息数组$config传递给cwebapplication创建出对象,并执行对象的run() 方法启动框架。
cwebapplication类的继承关系
cwebapplication -> capplication -> cmodule -> ccomponent
$config先被传递给capplication的构造函数
public function __construct($config=null){ yii::setapplication($this); // set basepath at early as possible to avoid trouble if(is_string($config)) $config=require($config); if(isset($config['basepath'])) { $this->setbasepath($config['basepath']); unset($config['basepath']); } else $this->setbasepath(‘protected’); yii::setpathofalias(‘application’,$this->getbasepath()); yii::setpathofalias(‘webroot’,dirname($_server['script_filename'])); $this->preinit(); $this->initsystemhandlers(); $this->registercorecomponents(); $this->configure($config); $this->attachbehaviors($this->behaviors); $this->preloadcomponents(); $this->init();}
yii::setapplication($this); 将自身的实例对象赋给yii的静态成员$_app,以后可以通过 yii::app() 来取得。
后面一段是设置capplication 对象的_basepath ,指向 proteced 目录。
yii::setpathofalias(‘application’,$this->getbasepath());yii::setpathofalias(‘webroot’,dirname($_server['script_filename']));
设置了两个系统路径别名 application 和 webroot,后面再import的时候可以用别名来代替实际的完整路径。别名配置存放在yiibase的 $_aliases 数组中。
$this->preinit();
预初始化。preinit()是在 cmodule 类里定义的,没有任何动作。
$this->initsystemhandlers() 方法内容:
/*** initializes the class autoloader and error handlers.*/protected function initsystemhandlers(){ if(yii_enable_exception_handler) set_exception_handler(array($this,’handleexception’)); if(yii_enable_error_handler) set_error_handler(array($this,’handleerror’),error_reporting());}
设置系统exception_handler和 error_handler,指向对象自身提供的两个方法。
4. 注册核心组件$this->registercorecomponents();
代码如下:
protected function registercorecomponents(){ parent::registercorecomponents(); $components=array( ‘urlmanager’=>array( ‘class’=>’curlmanager’, ), ‘request’=>array( ‘class’=>’chttprequest’, ), ‘session’=>array( ‘class’=>’chttpsession’, ), ‘assetmanager’=>array( ‘class’=>’cassetmanager’, ), ‘user’=>array( ‘class’=>’cwebuser’, ), ‘thememanager’=>array( ‘class’=>’cthememanager’, ), ‘authmanager’=>array( ‘class’=>’cphpauthmanager’, ), ‘clientscript’=>array( ‘class’=>’cclientscript’, ), ); $this->setcomponents($components);}
注册了几个系统组件(components)。
components 是在 cmodule 里定义和管理的,主要包括两个数组
private $_components=array();
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 里:
public function configure($config){ if(is_array($config)) { foreach($config as $key=>$value) $this->$key=$value; }}
实际上是把$config数组里的每一项传给 cmodule 的 父类 ccomponent __set() 方法。
public function __set($name,$value){ $setter=’set’.$name; if(method_exists($this,$setter)) $this->$setter($value); else if(strncasecmp($name,’on’,2)===0 && method_exists($this,$name)) { //duplicating geteventhandlers() here for performance $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new clist; $this->_e[$name]->add($value); } else if(method_exists($this,’get’.$name)) throw new cexception(yii::t(‘yii’,'property “{class}.{property}” is read only.’, array(‘{class}’=>get_class($this), ‘{property}’=>$name))); else throw new cexception(yii::t(‘yii’,'property “{class}.{property}” is not defined.’, array(‘{class}’=>get_class($this), ‘{property}’=>$name)));}
我们来看看:
if(method_exists($this,$setter))
根据这个条件,$config 数组里的basepath, params, modules, import, components 都被传递给相应的 setbasepath(), setparams() 等方法里进行处理。
6、$config 之 import其中 import 被传递给 cmodule 的 setimport:
public function setimport($aliases){ foreach($aliases as $alias) yii::import($alias);}
yii::import($alias)里的处理:
public static function import($alias,$forceinclude=false){ // 先判断$alias是否存在于yiibase::$_imports[] 中,已存在的直接return, 避免重复import。 if(isset(self::$_imports[$alias])) // previously imported return self::$_imports[$alias]; // $alias类已定义,记入$_imports[],直接返回 if(class_exists($alias,false)) return self::$_imports[$alias]=$alias; // 类似 urlmanager 这样的已定义于$_coreclasses[]的类,或不含.的直接类名,记入$_imports[],直接返回 if(isset(self::$_coreclasses[$alias]) || ($pos=strrpos($alias,’.'))===false) // a simple class name { self::$_imports[$alias]=$alias; if($forceinclude) { if(isset(self::$_coreclasses[$alias])) // a core class require(yii_path.self::$_coreclasses[$alias]); else require($alias.’.php’); } return $alias; } // 产生一个变量 $classname,为$alias最后一个.后面的部分 // 这样的:’x.y.classnamer’ // $classname不等于 ‘*’, 并且classnamer类已定义的, classnamer’ 记入 $_imports[],直接返回 if(($classname=(string)substr($alias,$pos+1))!==’*’ && class_exists($classname,false)) return self::$_imports[$alias]=$classname; // 取得 $alias 里真实的路径部分并且路径有效 if(($path=self::getpathofalias($alias))!==false) { // $classname!==’*',$classname 记入 $_imports[] if($classname!==’*') { self::$_imports[$alias]=$classname; if($forceinclude) require($path.’.php’); else self::$_classes[$classname]=$path.’.php’; return $classname; } // $alias是’system.web.*’这样的已*结尾的路径,将路径加到include_path中 else // a directory { set_include_path(get_include_path().path_separator.$path); return self::$_imports[$alias]=$path; } } else throw new cexception(yii::t(‘yii’,'alias “{alias}” is invalid. make sure it points to an existing directory or file.’,array(‘{alias}’=>$alias)));}
7. $config 之 components$config 数组里的 $components 被传递给cmodule 的setcomponents($components)
public function setcomponents($components){ foreach($components as $id=>$component) { if($component instanceof iapplicationcomponent) $this->setcomponent($id,$component); else if(isset($this->_componentconfig[$id])) $this->_componentconfig[$id]=cmap::mergearray($this->_componentconfig[$id],$component); else $this->_componentconfig[$id]=$component; }}
$component是iapplicationcomponen的实例的时候,直接赋值:
$this->setcomponent($id,$component),
public function setcomponent($id,$component){ $this->_components[$id]=$component; if(!$component->getisinitialized()) $component->init();}
如果$id已存在于_componentconfig[]中(前面注册的corecomponent),将$component 属性加进入。
其他的component将component属性存入_componentconfig[]中。
8. $config 之 params这个很简单
public function setparams($value){ $params=$this->getparams(); foreach($value as $k=>$v) $params->add($k,$v);}
configure 完毕!
9. attachbehaviors$this->attachbehaviors($this->behaviors);
空的,没动作
预创建组件对象
$this->preloadcomponents();
protected function preloadcomponents(){ foreach($this->preload as $id) $this->getcomponent($id);}
getcomponent() 判断_components[] 数组里是否有 $id的实例,如果没有,就根据_componentconfig[$id]里的配置来创建组件对象,调用组件的init()方法,然后存入_components[$id]中。
10. init()$this->init();
函数内:$this->getrequest();
创建了reques 组件并初始化。
11. run()public function run(){ $this->onbeginrequest(new cevent($this)); $this->processrequest(); $this->onendrequest(new cevent($this));}