`

Yii分析1:web程序入口(2)

阅读更多

 

接上篇:Yii分析1:web程序入口(1)

 

然后调用了两个初始化异常/错误和注册核心组件的方法:

         $this->initSystemHandlers();  
         $this->registerCoreComponents();  

 函数实现如下:

    //初始化errorhandler和exceptionhandler
    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());
    } 

 registerCoreComponents调用了CWebApplication的方法:

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

        $components=array(                   
 
            //url路由类
            'urlManager'=>array(                                                  
                'class'=>'CUrlManager',                                           
            ),             
            //http请求处理类
            'request'=>array(
                'class'=>'CHttpRequest',                                          
            ),
            //session
处理类
            'session'=>array(
                'class'=>'CHttpSession',                                          
            ), 
 
            //assets文件管理类
            'assetManager'=>array(                                                
                'class'=>'CAssetManager',                                         
            ),                          
            //用户类
            'user'=>array(                                                        
                'class'=>'CWebUser',
            ),
            //主题管理类
            'themeManager'=>array(
                'class'=>'CThemeManager',
            ),

            //认证管理类
            'authManager'=>array(
                'class'=>'CPhpAuthManager',
            ),

             //客户端脚本管理类
            'clientScript'=>array(
                'class'=>'CClientScript',
            ),
        );

        $this->setComponents($components);
    }

 其中CWebApplication的parent中代码如下:

protected function registerCoreComponents()
    {
        $components=array(
            //消息类(国际化)
            'coreMessages'=>array(
                'class'=>'CPhpMessageSource',
                'language'=>'en_us',
                'basePath'=>YII_PATH.DIRECTORY_SEPARATOR.'messages',
            ),
            //数据库类
            'db'=>array(
                'class'=>'CDbConnection',
            ),
            'messages'=>array(
                'class'=>'CPhpMessageSource',
            ),
            //错误处理
            'errorHandler'=>array(
                'class'=>'CErrorHandler',
            ),
            //安全处理类
            'securityManager'=>array(
                'class'=>'CSecurityManager',
            ),
            //基于文件的持久化存储类
            'statePersister'=>array(
                'class'=>'CStatePersister',
            ),
        );

        $this->setComponents($components);
    }

setComponents定义在CModule中:

    /**
     * Sets the application components.
     *
     * When a configuration is used to specify a component, it should consist of
     * the component's initial property values (name-value pairs). Additionally,
     * a component can be enabled (default) or disabled by specifying the 'enabled' value
     * in the configuration.
     *
     * If a configuration is specified with an ID that is the same as an existing
     * component or configuration, the existing one will be replaced silently.
     *
     * The following is the configuration for two components:
     * <pre>
     * array(
     *     'db'=>array(
     *         'class'=>'CDbConnection',
     *         'connectionString'=>'sqlite:path/to/file.db',
     *     ),
     *     'cache'=>array(
     *         'class'=>'CDbCache',
     *         'connectionID'=>'db',
     *         'enabled'=>!YII_DEBUG,  // enable caching in non-debug mode
     *     ),
     * )
     * </pre>
     *
     * @param array application components(id=>component configuration or instances)
     */
    public function setComponents($components)
    {
        foreach($components as $id=>$component)
        {
            //如果继承了IApplicationComponent,则立即set,否则放进_componentConfig数组中
            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;
        }
    } 
 

 

之后这三行代码:

        $this->configure($config);
        $this->attachBehaviors($this->behaviors);
        $this->preloadComponents();

 每一行都做了很多工作,我们一步一步分析:

 

$this->configure($config);

 CApplication并没有configure方法,因此我们查看父类CModule的configure方法:

    public function configure($config)
    {
        if(is_array($config))
        {
            foreach($config as $key=>$value)
                $this->$key=$value;
        }
    }

 是否还记得$config这个变量?其实它是一个数组包含了一些基本配置信息,例如(yii自带blog demo):

return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'blog',
    'defaultController'=>'post',
……
}

 configure看起来仅仅是将数组的键值对赋值给类的变量和值,其实这里使用了__set方法,而CModule并没有__set方法,因此看他的父类CComponent:

    public function __set($name,$value)
    {
        $setter='set'.$name; 
        //如果set开头的方法存在,则调用set方法进行赋值
        if(method_exists($this,$setter))
            $this->$setter($value);         
        //如果有on开通的事件函数,则调用该事件函数处理
        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);
        }  
        //如果只存在get方法,那么说明这个属性是制度的,抛出异常 
        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)));
    }
 

接下来是:

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

 依然调用的CComponent中的方法:

    public function attachBehaviors($behaviors)
    {
        foreach($behaviors as $name=>$behavior)
            $this->attachBehavior($name,$behavior);
    } 

 事实上,这里$this->behavior还是空的,因此这里并没有做什么事情;

 

之后是:

$this->preloadComponents();

 调用的是CModule中的方法:

    protected function preloadComponents()
    {
        foreach($this->preload as $id)
            $this->getComponent($id);
    }    

 是否还记得config会配置一个键名为'preload‘的数组成员?在这里会将其中的类初始化;

 

最后调用的方法是:

        $this->init();

首先调用了CWebApplication中的init方法:

    protected function init()
    {
        parent::init();
        // preload 'request' so that it has chance to respond to onBeginRequest event.
        $this->getRequest();
    }
 

其中调用了父类的父类CModule中的方法init:

    protected function init()
    {
    }

 没有做任何事

 

到这里,new CreateWebApplication的工作全部完成,主要过程总结如下:

 

读取配置文件;

将配置文件中的数组成员赋值给成员变量;

初始化preload配置的类;

 

 

(未完待续)

 

 

 

 

 

分享到:
评论
1 楼 philip8728 2012-09-11  
给力 还有后续的吗?

相关推荐

    Yii 2和PHP Web应用程序开发

    Yii 2和PHP Web应用程序开发

    The Yii Book: Developing Web Applications Using the Yii PHP Framework(Part1&2)

    "The Yii Book: Developing Web Applications Using the Yii PHP Framework" written by Larry Ullman and published over the course of 2012 and 2013. This book, and the selling of it, is a bit of an ...

    yii2-webapps:Yii2 Web应用程序

    Yii2 Web应用程序 Yii2 Web应用程序是一个专用于yii2开发人员社区的入门项目,它具有满足快速创建应用程序需求和额外安全性的功能。该项目的建造: AdminLTE 3 Yii2最新版本 Bootstrap 4 成为开源集体贡献者:安装$ ...

    Yii2的基本应用程序模板 yii-basic-app-2.0.12

    Yii2的基本应用程序模板 yii-basic-app-2.0.12

    YII2 教程( Web Application Development with Yii 2 and PHP ) 英文版

    请注意,这是: 1 Yii2教程 2 英文版

    yii2cms:yii2cms

    Yii 2 Advanced Application Template 是一个骨架 Yii 2 应用程序,最适合开发具有多层的复杂 Web 应用程序。 模板包括三层:前端、后端和控制台,每一层都是一个独立的 Yii 应用程序。 该模板旨在在团队开发环境...

    yii2elfinder:yii2elfinder

    yii2elfinder 感谢: : 感谢:zybodya 提供当前 yii 版本 yii2elfinder 介绍:旧版本无法使用,因为它完全不适用于最新的jquery版本! 所以除了行动,我不得不改变一切;) 这个扩展允许你将 ElFinder 文件管理...

    yii2.0基础高级应用程序模板

    yii2.0基础和高级应用程序模板两个,不想使用composer安装yii2.0的,就直接下载这个用吧

    yii-qa:基于Yii2实现的问答系统

    Yii-QA简介(此项目目前已不再维护)感谢选择Yii-QA,基于框架基础实现的问答程序。 #意识到目前的急性时间有限,无法管理太多的额外项目,我准备合并现有手上的项目,集成在一个项目中,感谢支持!!!!!!!请关注:...

    yii2-beanstalk, Yii2 beanstalk web和控制台组件.zip

    yii2-beanstalk, Yii2 beanstalk web和控制台组件 yii2-beanstalkYii2 beanstalkd web和控制台组件,它是 pda/pheanstalk服务器顶部的一个接口。 感谢 Paul Annesley 完成这项工作。:如何使用?插件安装与 Composer...

    yii2-swoole:完整的解决方案,使yii2-framework与协程在swoole上运行

    yii2-swoole 为赋予 Yii2 框架协程异步能力而生。 后期开发会依赖 去实现功能,相信 Swoft 会是下一代优秀的框架。 此插件基于 swoole (v2.0) 底层实现的协程,改造 Yii2 的核心代码,使开发者无感知,以及在不改动...

    yii-demo:Yii 3演示应用程序

    Yii框架演示项目 [Yii Framework]是一个现代框架,旨在为您PHP应用程序奠定坚实的基础。 它旨在显示和测试所有Yii功能。 安装 您至少需要PHP 7.4。 克隆此存储库。 在项目根目录中运行composer update 。 运行./...

    yii2swoole让yii2运行在swoole上

    yii2 swoole:让yii2运行在swoole上 , 运行在swoole上的yii2是运行在php-fpm上yii2的5倍以上

    yii2-schemadump:从现有数据库生成模式

    yii2-schemadump 从现有数据库生成模式。演示版要求PHP 7.3或更高版本Yii 2.x安装composer require --dev jamband/yii2-schemadump用法在config / console.php中添加以下内容: return [ . . . 'components' =&gt; [ . ...

    yii-passport:使Laravel Passport与Yii一起工作

    Yii护照 安装 :light_bulb: 这是展示如何安装软件包的好地方,请参见下文: 跑步 $ composer require inquid/yii-passport 用法 :light_bulb: 这是显示一些用法示例的好地方! 变更日志 请看看 。 贡献 请看看 。...

    yii2fullcalendar:jQuery Fullcalendar Yii2扩展

    yii2fullcalendar JQuery Fullcalendar Yii2扩展JQuery来自: ://arshaw.com/fullcalendar/版本4.0.2许可证MIT jQuery文档: //arshaw.com/fullcalendar/docs/ Yii2扩展,通过 可以在这里找到一个小样本:http: ...

    Web Application Development with Yii 2 and PHP

    书名:Web Application Development with Yii 2 and PHP 定价:140.10元 作者:Mark Safronov 出版社:Packt Publishing Limited (2014年9月25日) 出版日期:2014年9月25日

    yii-yii2-bridge:在 Yii 1 应用程序中使用 Yii 2 小部件

    在遗留的 Yii 1 应用程序中使用 Yii 2 小部件。 要求 Yii 1.1.15 应用 安装 安装此扩展的首选方法是通过 。 要么跑 php composer.phar require --prefer-dist "neam/yii-yii2-bridge" "*" 或添加 " neam/yii-yii2...

    Yii2的高级应用程序模板yii-advanced-app-2.0.12.tgz

    Yii2的高级应用程序模板yii-advanced-app-2.0.12.tgz

    yii2sly:jquery 狡猾

    yii2sly 这个扩展是惊人的 jquery 滑块“sly”的包装器,可以在这里找到: 请。 仔细查看所有插件选项,可以通过将它们添加到“clientOptions”参数来传递这些选项,如下所示。 可以在此处找到扩展的演示: 安装 ...

Global site tag (gtag.js) - Google Analytics