多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
Yii PHP 框架分析 (一) 2009-09-26 22:15 <table style="TABLE-LAYOUT: fixed; WIDTH: 100%"><tbody><tr><td><div class="cnt" id="blog_text"> <p>Yii PHP 框架分析 (一)<br/> 作者:wdy</p> <p><a href="http://hi.baidu.com/delphiss/blog/item/f7da86d787adb72506088b4b.html">http://hi.baidu.com/delphiss/blog/item/f7da86d787adb72506088b4b.html</a></p> <p>基于yii1.0.8的代码分析的。用了一个下午整理的,流水账,感兴趣的凑合着先看,国庆期间推出个整理修改版,然后再完成后两个部分(MVC和Yii的整体结构分析)。</p> <p><strong>1. 启动</strong></p> <p>网站的唯一入口程序 index.php :</p> <p><br/> $yii=dirname(__FILE__).'/../framework/yii.php';<br/> $config=dirname(__FILE__).'/protected/config/main.php';</p> <p>// remove the following line when in production mode<br/> defined('YII_DEBUG') or define('YII_DEBUG',true);</p> <p>require_once($yii);<br/> Yii::createWebApplication($config)-&gt;run();</p> <p>上面的require_once($yii) 引用出了后面要用到的全局类Yii,Yii类是YiiBase类的完全继承:</p> <p>class Yii extends YiiBase<br/> {<br/> }</p> <p>系统的全局访问都是通过Yii类(即YiiBase类)来实现的,Yii类的成员和方法都是static类型。</p> <p><strong>2. 类加载</strong></p> <p>Yii利用PHP5提供的spl库来完成类的自动加载。在YiiBase.php 文件结尾处</p> <p>spl_autoload_register(array('YiiBase','autoload'));</p> <p>将YiiBase类的静态方法autoload 注册为类加载器。 PHP autoload 的简单原理就是执行 new 创建对象或通过类名访问静态成员时,系统将类名传递给被注册的类加载器函数,类加载器函数根据类名自行找到对应的类文件并include 。</p> <p>下面是YiiBase类的autoload方法:</p> <p>public static function autoload($className)<br/> {<br/>    // use include so that the error PHP file may appear<br/>    if(isset(self::$_coreClasses[$className]))<br/>     include(YII_PATH.self::$_coreClasses[$className]);<br/>    else if(isset(self::$_classes[$className]))<br/>     include(self::$_classes[$className]);<br/>    else<br/>     include($className.'.php');<br/> }</p> <p>可以看到YiiBase的静态成员$_coreClasses 数组里预先存放着Yii系统自身用到的类对应的文件路径:</p> <p>private static $_coreClasses=array(<br/>    'CApplication' =&gt; '/base/CApplication.php',<br/>    'CBehavior' =&gt; '/base/CBehavior.php',<br/>    'CComponent' =&gt; '/base/CComponent.php',<br/>    ...<br/> )</p> <p>非 coreClasse 的类注册在YiiBase的$_classes 数组中:<br/> private static $_classes=array();</p> <p>其他的类需要用Yii::import()讲类路径导入PHP include paths 中,直接<br/> include($className.'.php')</p> <p><strong>3. CWebApplication的创建</strong></p> <p>回到前面的程序入口的 Yii::createWebApplication($config)-&gt;run();</p> <p>public static function createWebApplication($config=null)<br/> {<br/>    return new CWebApplication($config);<br/> }</p> <p>现在autoload机制开始工作了。<br/> 当系统 执行 new CWebApplication() 的时候,会自动 <br/> include(YII_PATH.'/base/CApplication.php')</p> <p>将main.php里的配置信息数组$config传递给CWebApplication创建出对象,并执行对象的run() 方法启动框架。</p> <p>CWebApplication类的继承关系</p> <p>CWebApplication -&gt; CApplication -&gt; CModule -&gt; CComponent</p> <p>$config先被传递给CApplication的构造函数</p> <p>public function __construct($config=null)<br/> {<br/>    Yii::setApplication($this);</p> <p>   // set basePath at early as possible to avoid trouble<br/>    if(is_string($config))<br/>     $config=require($config);<br/>    if(isset($config['basePath']))<br/>    {<br/>     $this-&gt;setBasePath($config['basePath']);<br/>     unset($config['basePath']);<br/>    }<br/>    else<br/>     $this-&gt;setBasePath('protected');<br/>    Yii::setPathOfAlias('application',$this-&gt;getBasePath());<br/>    Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));</p> <p>   $this-&gt;preinit();</p> <p>   $this-&gt;initSystemHandlers();<br/>    $this-&gt;registerCoreComponents();</p> <p>   $this-&gt;configure($config);<br/>    $this-&gt;attachBehaviors($this-&gt;behaviors);<br/>    $this-&gt;preloadComponents();</p> <p>   $this-&gt;init();<br/> }</p> <p><br/> Yii::setApplication($this); 将自身的实例对象赋给Yii的静态成员$_app,以后可以通过 Yii::app() 来取得。<br/> 后面一段是设置CApplication 对象的_basePath ,指向 proteced 目录。</p> <p>Yii::setPathOfAlias('application',$this-&gt;getBasePath());<br/> Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));</p> <p>设置了两个系统路径别名 application 和 webroot,后面再import的时候可以用别名来代替实际的完整路径。别名配置存放在YiiBase的 $_aliases 数组中。</p> <p>$this-&gt;preinit();<br/> 预初始化。preinit()是在 CModule 类里定义的,没有任何动作。</p> <p>$this-&gt;initSystemHandlers() 方法内容:</p> <p>/**<br/> * Initializes the class autoloader and error handlers.<br/> */<br/> protected function initSystemHandlers()<br/> {<br/>    if(YII_ENABLE_EXCEPTION_HANDLER)<br/>     set_exception_handler(array($this,'handleException'));<br/>    if(YII_ENABLE_ERROR_HANDLER)<br/>     set_error_handler(array($this,'handleError'),error_reporting());  <br/> }</p> <p>设置系统exception_handler和 error_handler,指向对象自身提供的两个方法。</p> <p><strong>4. 注册核心组件</strong></p> <p>$this-&gt;registerCoreComponents();<br/> 代码如下:</p> <p>protected function registerCoreComponents()<br/> {<br/>    parent::registerCoreComponents();</p> <p>   $components=array(<br/>     'urlManager'=&gt;array(<br/>      'class'=&gt;'CUrlManager',<br/>     ),<br/>     'request'=&gt;array(<br/>      'class'=&gt;'CHttpRequest',<br/>     ),<br/>     'session'=&gt;array(<br/>      'class'=&gt;'CHttpSession',<br/>     ),<br/>     'assetManager'=&gt;array(<br/>      'class'=&gt;'CAssetManager',<br/>     ),<br/>     'user'=&gt;array(<br/>      'class'=&gt;'CWebUser',<br/>     ),<br/>     'themeManager'=&gt;array(<br/>      'class'=&gt;'CThemeManager',<br/>     ),<br/>     'authManager'=&gt;array(<br/>      'class'=&gt;'CPhpAuthManager',<br/>     ),<br/>     'clientScript'=&gt;array(<br/>      'class'=&gt;'CClientScript',<br/>     ),<br/>    );</p> <p>   $this-&gt;setComponents($components);<br/> }</p> <p>注册了几个系统组件(Components)。<br/> Components 是在 CModule 里定义和管理的,主要包括两个数组</p> <p>private $_components=array();<br/> private $_componentConfig=array();</p> <p>每个 Component 都是 IApplicationComponent接口的实例,Componemt的实例存放在$_components 数组里,相关的配置信息存放在$_componentConfig数组里。配置信息包括Component 的类名和属性设置。</p> <p>CWebApplication 对象注册了以下几个Component:urlManager, request,session,assetManager,user,themeManager,authManager,clientScript。CWebApplication的parent 注册了以下几个Component:coreMessages,db,messages,errorHandler,securityManager,statePersister。</p> <p>Component 在YiiPHP里是个非常重要的东西,它的特征是可以通过 CModule 的 __get() 和 __set() 方法来访问。 Component 注册的时候并不会创建对象实例,而是在程序里被第一次访问到的时候,由CModule 来负责(实际上就是 Yii::app())创建。</p> <p><strong>5. 处理 $config 配置</strong></p> <p>继续, $this-&gt;configure($config);<br/> configure() 还是在CModule 里:</p> <p>public function configure($config)<br/> {<br/>    if(is_array($config))<br/>    {<br/>     foreach($config as $key=&gt;$value)<br/>      $this-&gt;$key=$value;<br/>    }<br/> }</p> <p>实际上是把$config数组里的每一项传给 CModule 的 父类 CComponent __set() 方法。</p> <p>public function __set($name,$value)<br/> {<br/>    $setter='set'.$name;<br/>    if(method_exists($this,$setter))<br/>     $this-&gt;$setter($value);<br/>    else if(strncasecmp($name,'on',2)===0 <br/>                &amp;&amp; method_exists($this,$name))<br/>    {<br/>     //duplicating getEventHandlers() here for performance<br/>     $name=strtolower($name);<br/>     if(!isset($this-&gt;_e[$name]))<br/>      $this-&gt;_e[$name]=new CList;<br/>      $this-&gt;_e[$name]-&gt;add($value);<br/>     }<br/>     else if(method_exists($this,'get'.$name))<br/>      throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',<br/>      array('{class}'=&gt;get_class($this), '{property}'=&gt;$name)));<br/>     else<br/>      throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',<br/>      array('{class}'=&gt;get_class($this), '{property}'=&gt;$name)));<br/>    }<br/> }</p> <p>我们来看看:<br/> if(method_exists($this,$setter))<br/> 根据这个条件,$config 数组里的basePath, params, modules, import, components 都被传递给相应的 setBasePath(), setParams() 等方法里进行处理。</p> <p><strong>6、$config 之 import</strong></p> <p>其中 import 被传递给 CModule 的 setImport:</p> <p>public function setImport($aliases)<br/> {<br/>    foreach($aliases as $alias)<br/>     Yii::import($alias);<br/> }</p> <p>Yii::import($alias)里的处理:</p> <p>public static function import($alias,$forceInclude=false)<br/> {<br/>    // 先判断$alias是否存在于YiiBase::$_imports[] 中,已存在的直接return, 避免重复import。<br/>    if(isset(self::$_imports[$alias])) // previously imported<br/>     return self::$_imports[$alias];</p> <p>   // $alias类已定义,记入$_imports[],直接返回<br/>    if(class_exists($alias,false))<br/>     return self::$_imports[$alias]=$alias;</p> <p>   // 类似 urlManager 这样的已定义于$_coreClasses[]的类,或不含.的直接类名,记入$_imports[],直接返回<br/>    if(isset(self::$_coreClasses[$alias]) || ($pos=strrpos($alias,'.'))===false) // a simple class name<br/>    {<br/>     self::$_imports[$alias]=$alias;<br/>     if($forceInclude)<br/>     {<br/>      if(isset(self::$_coreClasses[$alias])) // a core class<br/>       require(YII_PATH.self::$_coreClasses[$alias]);<br/>      else<br/>       require($alias.'.php');<br/>     }<br/>     return $alias;<br/>    }</p> <p>   // 产生一个变量 $className,为$alias最后一个.后面的部分<br/>    // 这样的:'x.y.ClassNamer'<br/>    // $className不等于 '*', 并且ClassNamer类已定义的,      ClassNamer' 记入 $_imports[],直接返回<br/>    if(($className=(string)substr($alias,$pos+1))!=='*' &amp;&amp; class_exists($className,false))<br/>     return self::$_imports[$alias]=$className;</p> <p>   // 取得 $alias 里真实的路径部分并且路径有效<br/>    if(($path=self::getPathOfAlias($alias))!==false)<br/>    {<br/>     // $className!=='*',$className 记入 $_imports[]<br/>     if($className!=='*')<br/>     {<br/>      self::$_imports[$alias]=$className;<br/>      if($forceInclude)<br/>       require($path.'.php');<br/>      else<br/>       self::$_classes[$className]=$path.'.php';<br/>      return $className;<br/>     }<br/>     // $alias是'system.web.*'这样的已*结尾的路径,将路径加到include_path中<br/>     else // a directory<br/>     {<br/>      set_include_path(get_include_path().PATH_SEPARATOR.$path);<br/>      return self::$_imports[$alias]=$path;<br/>     }<br/>    }<br/>    else<br/>     throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',<br/>      array('{alias}'=&gt;$alias)));<br/> }</p> <p><strong>7. $config 之 components</strong></p> <p>$config 数组里的 $components 被传递给CModule 的setComponents($components)</p> <p>public function setComponents($components)<br/> {<br/>    foreach($components as $id=&gt;$component)<br/>    {<br/>     if($component instanceof IApplicationComponent)<br/>      $this-&gt;setComponent($id,$component);<br/>     else if(isset($this-&gt;_componentConfig[$id]))<br/>      $this-&gt;_componentConfig[$id]=CMap::mergeArray($this-&gt;_componentConfig[$id],$component);<br/>     else<br/>      $this-&gt;_componentConfig[$id]=$component;<br/>    }<br/> }</p> <p>$componen是IApplicationComponen的实例的时候,直接赋值:<br/> $this-&gt;setComponent($id,$component),</p> <p>public function setComponent($id,$component)<br/> {<br/>    $this-&gt;_components[$id]=$component;<br/>    if(!$component-&gt;getIsInitialized())<br/>     $component-&gt;init();<br/> }</p> <p>如果$id已存在于_componentConfig[]中(前面注册的coreComponent),将$component 属性加进入。<br/> 其他的component将component属性存入_componentConfig[]中。</p> <p><strong>8. $config 之 params</strong></p> <p>这个很简单<br/> public function setParams($value)<br/> {<br/>    $params=$this-&gt;getParams();<br/>    foreach($value as $k=&gt;$v)<br/>     $params-&gt;add($k,$v);<br/> }</p> <p>configure 完毕!</p> <p><strong>9. attachBehaviors</strong></p> <p>$this-&gt;attachBehaviors($this-&gt;behaviors);<br/> 空的,没动作</p> <p>预创建组件对象<br/> $this-&gt;preloadComponents();</p> <p>protected function preloadComponents()<br/> {<br/>    foreach($this-&gt;preload as $id)<br/>     $this-&gt;getComponent($id);<br/> }</p> <p>getComponent() 判断_components[] 数组里是否有 $id的实例,如果没有,就根据_componentConfig[$id]里的配置来创建组件对象,调用组件的init()方法,然后存入_components[$id]中。</p> <p><strong>10. init()</strong></p> <p>$this-&gt;init();</p> <p>函数内:$this-&gt;getRequest();<br/> 创建了Reques 组件并初始化。</p> <p><strong>11. run()</strong></p> <p>public function run()<br/> {<br/>    $this-&gt;onBeginRequest(new CEvent($this));<br/>    $this-&gt;processRequest();<br/>    $this-&gt;onEndRequest(new CEvent($this));<br/> }</p> </div></td> </tr></tbody></table> |   |   |   | |-----|-----|-----|