Yii 系统启动 trace源码

  摘要:学习使用Yii框架,总觉得使用起来不顺手,趁这几天工作不忙,就trace下框架源码吧。这篇先来trace从入口文件到控制器启动的过程。

1. 入口文件index.php

$yii=dirname(__FILE__).'/../../lib/yii-1.1.16/yii.php';
require_once($yii);
Yii::createWebApplication($config)->run();

  

2. system.YiiBase

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


  在这里new出了第一个对象,CWebApplication
  继承关系如下:

class CWebApplication » CApplication » CModule » CComponent

  new的时候,调用了构造函数__construct(),这个构造函数是在CApplication这个类里面声明的。
  就是在这个构造函数里面实现了系统初始化工作。

3. system.web.CApplication

  这里只trace部分代码(component相关)

public function __construct($config=null) {
    //tag
    Yii::setApplication($this);
    //作者特意在这里写了注释:设置basePath要尽可能的早,以避免麻烦,估计这个路径是后面很多地方都用到的基础的路径。
    if(isset($config['basePath']))
    {
        $this->setBasePath($config['basePath']);
        unset($config['basePath']);
    }
    //tag2
    $this->registerCoreComponents();
    $this->configure($config);
    $this->preloadComponents();
    $this->init();
}


//tag
这句代码很关键,我们在任何地方调用Yii::app()这个对象就是这就代码实现的
它将创建的CWebApplication实例传入YiiBase::setApplication($app);
代码如下:
public static function setApplication($app)
{
    if(self::$_app===null || $app===null)
        self::$_app=$app;
    else
        throw new CException(Yii::t('yii','Yii application can only be created once.'));
}

//tag2
$this->registerCoreComponents();
   这个方法用来注册核心组件。
   这个函数虽然是在CApplication的构造函数里面声明的,但是根据继承关系,当new CWebApplication的时候,这个构造函数实际上是在CWebApplication里面执行的,而registerCoreComponents()方法,在CWebApplication和CApplication里面都有声明,并且在CWebApplication->registerCoreComponents()里面有调用parent::registerCoreComponents();所以是这样的:
   在CWebApplication里面注册的核心组件有:session,assetManager,user,themeManager,authManager,clientScript,widgetFactory
   在CApplication里面注册的核心组件有:coreMessages,db,messages,errorHandler,securityManager,statePersister,urlManager,request,format
   在这个方法最后调用了$this->setComponents($components);

下面trace 这个方法,有玄机

3.1 system.base.CModule

public function setComponents($components,$merge=true) {
    //这里的$components就是上面的配置数组
    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;
}

<未完>

你可能感兴趣的:(源码,框架,yii)