YII1.1源码解读(1)

来源:互联网 发布:软件开发自学网站 编辑:程序博客网 时间:2024/05/12 09:18

先放关系图:


这些组件是在CWebApplication初始化时加载的,而且CWebApplication控制着他们的生命周期,所以这里用组合关系

YiiBase我认为是一个容器,也是一个单例模式,也是一个工厂,他第一件事就是生产网站,网站作为一个组件被生产出来

那么做为一个容器,应该尽可能的为他生产出来的类提供一些必要的功能,比如

引入文件的方法

public static function import($alias,$forceInclude=false)
生成类的方法

public static function autoload($className,$classMapOnly=false)

Yii::createWebApplication($config)->run();
YiiBase.php

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


YiiBase就像一个尽职的父母,主要了下几件事:

1.父类CApplication初始化

    public function __construct($config=null)    {        //....省略代码        $this->preinit();        $this->initSystemHandlers();        $this->registerCoreComponents();//1        $this->configure($config);//2        $this->attachBehaviors($this->behaviors);//3        $this->preloadComponents();//4        $this->init();    }

  1)加载CApplication(加载了db,urlManager,request等),CWebApplication(加载了session,assetManager,clientScript等我只写比较重要的)运行所需要的组件

  2)设置配置文件,这里配置是网站的配置,所以要在WebApplication里配置,而不是YiiBase

  3)设置行为方法

  4)加载静态组件,这里没有设置

  5)执行实现类的init方法,也可以说是延迟实现

2、创建CWebapplication初始化

 

public function processRequest(){if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0])){$route=$this->catchAllRequest[0];foreach(array_splice($this->catchAllRequest,1) as $name=>$value)$_GET[$name]=$value;}else$route=$this->getUrlManager()->parseUrl($this->getRequest());$this->runController($route);}
网站当然主要的事情就是处理请求了,处理请求就出现了几个对象,Route,UrlManager,Request这些也都是组件,每个负责自己的事情

catchAllRequest //维护相关的就不说了
先看
public function getRequest()    {        return $this->getComponent('request');    }
WebApplication自己动手丰衣足食,他自己用到的组件他自己去拿,但是他还是用了YiiBase提供的工具import和autolod来加载,这个getComponent是他爷爷类那继承来的CModule,也就是他爷爷那辈开始就知道跟YiiBase这个容器打交道。

看一下他爷爷留下的经验,怎么加载组件的:

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;}}}
拿前面我们给网站的config里一段配置来说

 

// application components'components'=>array('user'=>array(// enable cookie-based authentication'allowAutoLogin'=>true,),'db'=>array('connectionString' => 'sqlite:protected/data/blog.db','tablePrefix' => 'tbl_',),// uncomment the following to use a MySQL database/*'db'=>array('connectionString' => 'mysql:host=localhost;dbname=blog','emulatePrepare' => true,'username' => 'root','password' => '','charset' => 'utf8','tablePrefix' => 'tbl_',),*/'errorHandler'=>array(// use 'site/error' action to display errors'errorAction'=>'site/error',),'urlManager'=>array('urlFormat'=>'path','rules'=>array('post/<id:\d+>/<title:.*?>'=>'post/view','posts/<tag:.*?>'=>'post/index','<controller:\w+>/<action:\w+>'=>'<controller>/<action>',),),
$config=$this->_componentConfig[$id];
这里的$id就是这个数组里的db,urlManager的key,config自然就是对应的数组,有了这些就知道用哪个类去实现组件,还有组件需要的参数。
那么接下来,我们看这个Request这个组件的在init做了什么

protected function normalizeRequest(){// normalize requestif(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()){if(isset($_GET))$_GET=$this->stripSlashes($_GET);if(isset($_POST))$_POST=$this->stripSlashes($_POST);if(isset($_REQUEST))$_REQUEST=$this->stripSlashes($_REQUEST);if(isset($_COOKIE))$_COOKIE=$this->stripSlashes($_COOKIE);}if($this->enableCsrfValidation)Yii::app()->attachEventHandler('onBeginRequest',array($this,'validateCsrfToken'));}

对参数做了过滤。其他的没做什么。

先写到这,第一次写这么长的文章,可能有点肤浅,下次争取写的更好~~


0 0
原创粉丝点击