- 浏览: 331224 次
- 性别:
- 来自: 北京
博客专栏
-
高性能PHP框架Yii分析...
浏览量:83934
文章分类
最新评论
-
huxiaoyunhust:
价值观这种东西,说不好听是口号,其实是公司每天潜移默化的感染, ...
关于企业价值观的一点点感悟 -
silentime:
stef831018 写道你想多了,企业价值观就两个字:利益怎 ...
关于企业价值观的一点点感悟 -
stef831018:
你想多了,企业价值观就两个字:利益
关于企业价值观的一点点感悟 -
guoyuqiangf8:
关于企业价值观的一点点感悟 -
silentime:
mangguo 写道推荐大家去看看我写的python入门,可以 ...
python如何打印变量的类型
然后调用了两个初始化异常/错误和注册核心组件的方法:
$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配置的类;
(未完待续)
发表评论
-
[转]PHP的内存管理
2015-05-11 22:03 832原文出处:http://blogread.cn/it/ar ... -
《高性能PHP应用开发》读书笔记
2014-12-24 09:20 758注:只记录了一些关键点,部分知识点/内容请看到的朋友 ... -
[转]HTTP缓存算法
2013-01-04 14:34 1256原文地址:http://www.p ... -
【转】漫谈社区PHP 业务开发
2012-11-19 10:07 1069原文地址:http://stblog.baidu-tech.c ... -
Yii分析13:Yii核心组件之AssetManager
2012-04-10 00:37 4341我们通过使用来讲解CAssetManager的使用和工 ... -
Yii分析12:Yii核心web组件之会话类CHttpSession
2012-03-06 09:44 3191CHttpSession提供session级的数据管理和配置 ... -
Yii分析11:Yii核心组件之错误处理类CErrorHandler
2012-02-24 09:57 4037类如其名,CErrorHandler在Yii中主要的功能 ... -
Yii分析10:Yii核心组件之数据库类CDbConnection
2012-02-17 09:18 8457数据库类几乎是每个PHP框架必不可少的组件之一,Yii的 ... -
Yii分析9:CController控制视图(Cview)的展现
2011-11-30 00:01 4822《The Definitive Guide to ... -
[转]优酷网架构学习笔记
2011-11-22 10:22 1392原文地址:http://www.itivy.com/ivy/a ... -
[转]YouTube架构学习体会
2011-11-22 10:15 1176原文地址:http://www.itivy.com/ivy/a ... -
[转]深掘XSS漏洞场景之XSS Rootkit
2011-10-21 12:22 1312转载顿神大作:http://www.80sec.com/%E6 ... -
Yii分析7:runController的执行
2011-10-20 17:28 5940在《Yii分析4:run的执行》一文中,介绍了Yi ... -
关于PHP错误:unexpected T_PAAMAYIM_NEKUDOTAYIM
2011-10-08 16:23 1908PHP解析错误:Parse error: syntax err ... -
[推荐]基于PHP和MySQL的开源点击统计软件Piwik
2011-08-10 11:50 1681刚刚发现一款基于PHP和MySQL的开源点击统计软 ... -
PHP的is_callable和method_exists方法比较
2011-08-03 13:17 2142之前发过一篇关于is_callable的文章,主要是PHP手册 ... -
Yii分析6:CHttpRequest
2011-08-03 11:20 2836官方说明:CHttpRequest封装了$_SERVER变量, ... -
[转]10种方式实现跨域资源的共享
2011-07-27 10:50 997原文地址:http://www.ued16 ... -
[转]高级PHP应用程序漏洞审核技术
2011-07-19 10:18 1847原文地址:http://code.google.com/p/p ... -
使用PHP的SESSION直接保存对象需要注意隐含的序列化
2011-06-23 14:12 3349昨天在调试代码时,在同一个sever上有两个版本的代码,刚开始 ...
相关推荐
Yii 2和PHP Web应用程序开发
"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 Web应用程序 Yii2 Web应用程序是一个专用于yii2开发人员社区的入门项目,它具有满足快速创建应用程序需求和额外安全性的功能。该项目的建造: AdminLTE 3 Yii2最新版本 Bootstrap 4 成为开源集体贡献者:安装$ ...
Yii2的基本应用程序模板 yii-basic-app-2.0.12
请注意,这是: 1 Yii2教程 2 英文版
Yii 2 Advanced Application Template 是一个骨架 Yii 2 应用程序,最适合开发具有多层的复杂 Web 应用程序。 模板包括三层:前端、后端和控制台,每一层都是一个独立的 Yii 应用程序。 该模板旨在在团队开发环境...
yii2elfinder 感谢: : 感谢:zybodya 提供当前 yii 版本 yii2elfinder 介绍:旧版本无法使用,因为它完全不适用于最新的jquery版本! 所以除了行动,我不得不改变一切;) 这个扩展允许你将 ElFinder 文件管理...
yii2.0基础和高级应用程序模板两个,不想使用composer安装yii2.0的,就直接下载这个用吧
Yii-QA简介(此项目目前已不再维护)感谢选择Yii-QA,基于框架基础实现的问答程序。 #意识到目前的急性时间有限,无法管理太多的额外项目,我准备合并现有手上的项目,集成在一个项目中,感谢支持!!!!!!!请关注:...
yii2-beanstalk, Yii2 beanstalk web和控制台组件 yii2-beanstalkYii2 beanstalkd web和控制台组件,它是 pda/pheanstalk服务器顶部的一个接口。 感谢 Paul Annesley 完成这项工作。:如何使用?插件安装与 Composer...
yii2-swoole 为赋予 Yii2 框架协程异步能力而生。 后期开发会依赖 去实现功能,相信 Swoft 会是下一代优秀的框架。 此插件基于 swoole (v2.0) 底层实现的协程,改造 Yii2 的核心代码,使开发者无感知,以及在不改动...
Yii框架演示项目 [Yii Framework]是一个现代框架,旨在为您PHP应用程序奠定坚实的基础。 它旨在显示和测试所有Yii功能。 安装 您至少需要PHP 7.4。 克隆此存储库。 在项目根目录中运行composer update 。 运行./...
yii2 swoole:让yii2运行在swoole上 , 运行在swoole上的yii2是运行在php-fpm上yii2的5倍以上
yii2-schemadump 从现有数据库生成模式。演示版要求PHP 7.3或更高版本Yii 2.x安装composer require --dev jamband/yii2-schemadump用法在config / console.php中添加以下内容: return [ . . . 'components' => [ . ...
Yii护照 安装 :light_bulb: 这是展示如何安装软件包的好地方,请参见下文: 跑步 $ composer require inquid/yii-passport 用法 :light_bulb: 这是显示一些用法示例的好地方! 变更日志 请看看 。 贡献 请看看 。...
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 定价:140.10元 作者:Mark Safronov 出版社:Packt Publishing Limited (2014年9月25日) 出版日期:2014年9月25日
在遗留的 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
yii2sly 这个扩展是惊人的 jquery 滑块“sly”的包装器,可以在这里找到: 请。 仔细查看所有插件选项,可以通过将它们添加到“clientOptions”参数来传递这些选项,如下所示。 可以在此处找到扩展的演示: 安装 ...