yii执行流程及其框架结构

一 目录文件  

|-framework 框架核心库

  |--base 底层类库文件夹,包 含CApplication(应用类,负责全局的用户请求处理,它管理的应用组件集,将提供特定功能给整个应用程序),CComponent(组件类,该 文件包含了基于组件和事件驱动编程的基础类,从版本.1.0开始,一个行为的属性(或者它的公共成员变量或它通过getter和/或setter方 法??定义的属性)可以通过组件的访问来调用),CBehavior(行为类,主要负责声明事件和相应事件处理程序的方法、将对象的行为附加到组件 等),CModel(模型类,为所有的数据模型提供的基类),CModule(是模块和应用程序的基类,主要负责应用组件和子模块)等等

  |--caching 所有缓存方法,其中包含了Memcache缓存,APC缓存,数据缓存,CDummyCache虚拟缓存,CEAcceleratorCache缓存等等各种缓存方法

  |--cli YII项目生成脚本

  |--collections 用php语言构造传统OO语言的数据存储单元。如:队列,栈,哈希表等等

  |--console YII控制台

  |--db 数据库操作类

  |--gii YII 代码生成器(脚手架),能生成包括模型,控制器,视图等代码

  |--i18n YII 多语言,提供了各种语言的本地化数据,信息、文件的翻译服务、本地化日期和时间格式,数字等

  |--logging 日 志组件,YII提供了灵活和可扩展的日志记录功能。消息记录可分为根据日志级别和信息类别。应用层次和类别过滤器,可进一步选择的消息路由到不同的目的 地,例如文件,电子邮件,浏览器窗口,等等

  |--messages 提示信息的多语言

  |--test YII提供的测试,包括单元测试和功能测试

  |--utils 提供了常用的格式化方法

  |--validators 提供了各种验证方法

  |--vendors 这个文件夹包括第三方由Yii框架使用的资料库

  |--views 提供了YII错误、日志、配置文件的多语言视图

  |--web YII所有开发应用的方法

  |---actions 控制器操作类

  |---auth 权限认识类,包括身份认证,访问控制过滤,基本角色的访问控制等

  |---filters 过滤器,可被配置在控制器动作执行之前或之后执行。例如, 访问控制过滤器将被执行以确保在执行请求的动作之前用户已通过身份验证;性能过滤器可用于测量控制器执行所用的时间

  |---form 表单生成方法

  |---helpers 视图助手,包含GOOGLE AJAX API,创建HTML,JSON,JAVASCRIPT相关功能

  |---js JS库

  |---renderers 视图渲染组件

  |---services 封装SoapServer并提供了一个基于WSDL的Web服务

  |---widgets 部件

  |---CArrayDataProvider.php 可以配置的排序和分页属性自定义排序和分页的行为

  |---CActiveDataProvider.php ActiveRecord方法类

  |---CController.php 控制器方法,主要负责协调模型和视图之间的交互

  |---CPagination.php 分页类

  |---CUploadedFile.php 上传文件类

  |---CUrlManager.php URL管理

  |---CWebModule.php 应用模块管理,应用程序模块可被视为一个独立的子应用等等方法  

  |--.htaccess 重定向文件

  |--yii.php 引导文件

  |--YiiBase.php YiiBase类最主要的功能是注册了自动加载类方法,加载框架要用到所有接口。

  |--yiic Yii LINUX 命令行脚本

  |--yiic.bat YII WINDOW 命令行脚本

  |--yiilite.php 它是一些常用到的 Yii 类文件的合并文件。在文件中,注释和跟踪语句都被去除。因此,使用 yiilite.php 将减少被引用的文件数量并避免执行跟踪语句

 

二 源码分析 

1. 启动  

网站的唯一入口程序 index.php 

<?php

// change the following paths if necessary
$yii=dirname(__FILE__).'/../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主程序文件,里面有一个局类Yii,Yii类是YiiBase类的完全继承。 

class Yii extends YiiBase
{
}

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

2. 类加载  

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

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

将YiiBase类的静态方法autoload 注册为类加载器。 PHP autoload 的简单原理就是执行 new 创建对象或通过类名访问静态成员时,系统将类名传递给被注册的类加载器函数,类加载器函数根据类名自行找到对应的类文件并include  
下面是YiiBase类的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 appear
        if(isset(self::$classMap[$className]))
            include(self::$classMap[$className]);
        elseif(isset(self::$_coreClasses[$className]))
            include(YII_PATH.self::$_coreClasses[$className]);
        else
        {
            // include class file relying on include_path
            if(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);
                            if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
                                throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
                                    '{class}'=>$className,
                                    '{file}'=>$classFile,
                                )));
                            break;
                        }
                    }
                }
                else
                    include($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');
                else
                    return false;
            }
            return class_exists($className,false) || interface_exists($className,false);
        }
        return true;
    }

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

private static $_coreClasses=array(
        'CApplication' => '/base/CApplication.php',
        'CApplicationComponent' => '/base/CApplicationComponent.php',
        'CBehavior' => '/base/CBehavior.php',
        'CComponent' => '/base/CComponent.php',
        'CErrorEvent' => '/base/CErrorEvent.php',
        .....
        );

 

 

  3. CWebApplication的创建  

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

    

public static function createWebApplication($config=null)
    {
        return self::createApplication('CWebApplication',$config);
    }

 

public static function createApplication($class,$config=null)    {         
        return new $class($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']));
        Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');

        $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 目录。和webroot  

        Yii::setPathOfAlias('application',$this->getBasePath());
        Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
        Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');

 

      设置了三个系统路径别名 application , webroot,后面 ext 再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,指向对象自身提供的两个方法。  

      注册核心组件  

      $this->registerCoreComponents();  

    

protected function registerCoreComponents()
    {
        parent::registerCoreComponents();

        $components=array(
            'session'=>array(
                'class'=>'CHttpSession',
            ),
            'assetManager'=>array(
                'class'=>'CAssetManager',
            ),
            'user'=>array(
                'class'=>'CWebUser',
            ),
            'themeManager'=>array(
                'class'=>'CThemeManager',
            ),
            'authManager'=>array(
                'class'=>'CPhpAuthManager',
            ),
            'clientScript'=>array(
                'class'=>'CClientScript',
            ),
            'widgetFactory'=>array(
                'class'=>'CWidgetFactory',
            ),
        );

        $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())创建。

 

    处理 $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))
            return $this->$setter($value);
        elseif(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;
            return $this->_e[$name]->add($value);
        }
        elseif(is_array($this->_m))
        {
            foreach($this->_m as $object)
            {
                if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name)))
                    return $object->$name=$value;
            }
        }
        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() 等方法里进行处理。

 

       1、$config 之 import  

public function setImport($aliases)
    {
        foreach($aliases as $alias)
            Yii::import($alias);
    }

         Yii::import

public static function import($alias,$forceInclude=false)
    {
        if(isset(self::$_imports[$alias]))  // previously imported
            return self::$_imports[$alias];
        if(class_exists($alias,false) || interface_exists($alias,false))
            return self::$_imports[$alias]=$alias;
        if(($pos=strrpos($alias,'\\'))!==false) // a class name in PHP 5.3 namespace format
        {
            $namespace=str_replace('\\','.',ltrim(substr($alias,0,$pos),'\\'));
            if(($path=self::getPathOfAlias($namespace))!==false)
            {
                $classFile=$path.DIRECTORY_SEPARATOR.substr($alias,$pos+1).'.php';
                if($forceInclude)
                {
                    if(is_file($classFile))
                        require($classFile);
                    else
                        throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.',array('{alias}'=>$alias)));
                    self::$_imports[$alias]=$alias;
                }
                else
                    self::$classMap[$alias]=$classFile;
                return $alias;
            }
            else
                throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory.',
                    array('{alias}'=>$namespace)));
        }
        if(($pos=strrpos($alias,'.'))===false)  // a simple class name
        {
            if($forceInclude && self::autoload($alias))
                self::$_imports[$alias]=$alias;
            return $alias;
        }
        $className=(string)substr($alias,$pos+1);
        $isClass=$className!=='*';
        if($isClass && (class_exists($className,false) || interface_exists($className,false)))
            return self::$_imports[$alias]=$className;
        if(($path=self::getPathOfAlias($alias))!==false)
        {
            if($isClass)
            {
                if($forceInclude)
                {
                    if(is_file($path.'.php'))
                        require($path.'.php');
                    else
                        throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.',array('{alias}'=>$alias)));
                    self::$_imports[$alias]=$className;
                }
                else
                    self::$classMap[$className]=$path.'.php';
                return $className;
            }
            else  // a directory
            {
                if(self::$_includePaths===null)
                {
                    self::$_includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
                    if(($pos=array_search('.',self::$_includePaths,true))!==false)
                        unset(self::$_includePaths[$pos]);
                }
                array_unshift(self::$_includePaths,$path);
                if(self::$enableIncludePath && set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR,self::$_includePaths))===false)
                    self::$enableIncludePath=false;
                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)));
    }

      2.$config 之 components      

public function setComponents($components,$merge=true)
    {
        foreach($components as $id=>$component)
            $this->setComponent($id,$component,$merge);
    }
public function setComponent($id,$component,$merge=true)
    {
        if($component===null)
        {
            unset($this->_components[$id]);
            return;
        }
        elseif($component instanceof IApplicationComponent)
        {
            $this->_components[$id]=$component;
            if(!$component->getIsInitialized())
                $component->init();
            return;
        }
        elseif(isset($this->_components[$id]))
        {
            if(isset($component['class']) && get_class($this->_components[$id])!==$component['class'])
            {
                unset($this->_components[$id]);
                $this->_componentConfig[$id]=$component; //we should ignore merge here
                return;
            }
            foreach($component as $key=>$value)
            {
                if($key!=='class')
                    $this->_components[$id]->$key=$value;
            }
        }
        elseif(isset($this->_componentConfig[$id]['class'],$component['class'])
            && $this->_componentConfig[$id]['class']!==$component['class'])
        {
            $this->_componentConfig[$id]=$component; //we should ignore merge here
            return;
        }
        if(isset($this->_componentConfig[$id]) && $merge)
            $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
        else
            $this->_componentConfig[$id]=$component;
    }

      $componen是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[]中。  

      3. $config 之 params  

        这个很简单      

public function setParams($value)
    {
        $params=$this->getParams();
        foreach($value as $k=>$v)
            $params->add($k,$v);
    }

    configure 完毕! 

  

    attachBehaviors  

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

    

public function attachBehaviors($behaviors)
    {
        foreach($behaviors as $name=>$behavior)
            $this->attachBehavior($name,$behavior);
    }
public function attachBehavior($name,$behavior)
    {
        if(!($behavior instanceof IBehavior))
            $behavior=Yii::createComponent($behavior);
        $behavior->setEnabled(true);
        $behavior->attach($this);
        return $this->_m[$name]=$behavior;
    }

 

      //目前不知道做什么的。

 

    预创建组件对象  

       $this->preloadComponents();  

  

protected function preloadComponents()
    {
        foreach($this->preload as $id)
            $this->getComponent($id);
    }
public function getComponent($id,$createIfNull=true)
    {
        if(isset($this->_components[$id]))
            return $this->_components[$id];
        elseif(isset($this->_componentConfig[$id]) && $createIfNull)
        {
            $config=$this->_componentConfig[$id];
            if(!isset($config['enabled']) || $config['enabled'])
            {
                Yii::trace("Loading \"$id\" application component",'system.CModule');
                unset($config['enabled']);
                $component=Yii::createComponent($config);
                $component->init();
                return $this->_components[$id]=$component;
            }
        }
    }

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

    $this->init();

       //空的

  Yii::createWebApplication()->run();    

public function run()
    {
        if($this->hasEventHandler('onBeginRequest'))
            $this->onBeginRequest(new CEvent($this));
        register_shutdown_function(array($this,'end'),0,false);
        $this->processRequest();
        if($this->hasEventHandler('onEndRequest'))
            $this->onEndRequest(new CEvent($this));
    }

 

  

    

 

三 、大概过程  

application构造函数:  

设置当前运行实例  

获取配置参数  

设置basepath  

设置几个path;application,webroot ,ext  

preinit  

注册error、exception处理函数 initSystemHandlers  

加载核心组件 registerCoreComponents 包括webapplication的和application的  

设置配置文件 configure($config 

附加行为 $this->attachBehaviors($this->behaviors);  

10处理加载config中的preload,//通过getComponent分别加载并初始化 $this->preloadComponents();   

11 初始化init(); //加载CHttpRequest组件  

 

 

四、run()

public function run()
    {
        if($this->hasEventHandler('onBeginRequest'))
            $this->onBeginRequest(new CEvent($this));
        register_shutdown_function(array($this,'end'),0,false);
        $this->processRequest();
        if($this->hasEventHandler('onEndRequest'))
            $this->onEndRequest(new CEvent($this));
    }

 

  首先   $this->hasEventHandler('onBeginRequest')   判断有没有定义 $this->_e['onBeginRequest'] 我把它理解成构子 它是在congif里定义的。

public function hasEventHandler($name)
    {
        $name=strtolower($name);
        return isset($this->_e[$name]) && $this->_e[$name]->getCount()>0;
    }
  如果有定义  $this->_e['onBeginRequest'] 就执行  $this->onBeginRequest(new CEvent($this));  
public function raiseEvent($name,$event)
    {
        $name=strtolower($name);
        if(isset($this->_e[$name]))
        {
            foreach($this->_e[$name] as $handler)
            {
                if(is_string($handler))
                    call_user_func($handler,$event);
                elseif(is_callable($handler,true))
                {
                    if(is_array($handler))
                    {
                        // an array: 0 - object, 1 - method name
                        list($object,$method)=$handler;
                        if(is_string($object))    // static method call
                            call_user_func($handler,$event);
                        elseif(method_exists($object,$method))
                            $object->$method($event);
                        else
                            throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
                                array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1])));
                    }
                    else // PHP 5.3: anonymous function
                        call_user_func($handler,$event);
                }
                else
                    throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
                        array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler))));
                // stop further handling if param.handled is set true
                if(($event instanceof CEvent) && $event->handled)
                    return;
            }
        }
        elseif(YII_DEBUG && !$this->hasEvent($name))
            throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
                array('{class}'=>get_class($this), '{event}'=>$name)));
    }

 

  由此可以看出 $config 里的 onBeginRequest 写法可以有以下几种
<?php

return array(
    "onBeginRequest"=>array(
        "方法一",
        array(
            "class名","function名"
        )
    )
);
  结束后调用 register_shutdown_function(array($this,'end'),0,false);也就是$this->onEndRequest(); onEndRequest设置方与onBeginRequest是一样;
  下篇讲解 $this->processRequest();

 
 

  

  
  

 

 

 

posted on 2013-10-26 14:41  luffy丶zhao  阅读(779)  评论(0)    收藏  举报