Yii源码阅读笔记(九)

Behvaior类,Behavior类是所有事件类的基类:

 1 namespace yii\base;
 2
 3 /**
 4  * Behavior is the base class for all behavior classes.
 5  * 所有行为的基类
 6  * A behavior can be used to enhance the functionality of an existing component without modifying its code.
 7  * In particular, it can "inject" its own methods and properties into the component
 8  * and make them directly accessible via the component. It can also respond to the events triggered in the component
 9  * and thus intercept the normal code execution.
10  *
11  * @author Qiang Xue <[email protected]>
12  * @since 2.0
13  */
14 class Behavior extends Object
15 {
16     /**
17      * @var 行为的依附对象
18      */
19     public $owner;
20
21
22     /**
23      * Declares event handlers for the [[owner]]‘s events.
24      * 表示这个行为将对类的何种事件进行何种反馈即可
25      *
26      * Child classes may override this method to declare what PHP callbacks should
27      * be attached to the events of the [[owner]] component.
28      *
29      * The callbacks will be attached to the [[owner]]‘s events when the behavior is
30      * attached to the owner; and they will be detached from the events when
31      * the behavior is detached from the component.
32      *
33      * The callbacks can be any of the following:
34      * 事件handler可以是以下形式:
35      *
36      * - method in this behavior: `‘handleClick‘`, equivalent to `[$this, ‘handleClick‘]`
37      *  字符串,表示行为类的方法,和事件不同,事件总的字符串形式表示全局函数,这里表示当前行为类的方法
38      * - object method: `[$object, ‘handleClick‘]`
39      *  一个对象或类的成员函数,以数组的形式
40      * - static method: `[‘Page‘, ‘handleClick‘]`
41      * - anonymous function: `function ($event) { ... }`
42      * 一个匿名函数
43      *
44      * The following is an example:
45      *
46      * ```php
47      * [
48      *     Model::EVENT_BEFORE_VALIDATE => ‘myBeforeValidate‘,
49      *     Model::EVENT_AFTER_VALIDATE => ‘myAfterValidate‘,
50      * ]
51      * ```
52      *
53      * @return 行为所有要响应的事件.
54      */
55     public function events()
56     {
57         return [];
58     }
59
60     /**
61      * Attaches the behavior object to the component.
62      * 绑定行为到组件
63      * The default implementation will set the [[owner]] property
64      * and attach event handlers as declared in [[events]].
65      * 该方法会默认设置[[owner]]属性,且添加事件处理程序绑定到组件
66      * Make sure you call the parent implementation if you override this method.
67      * @param Component $owner the component that this behavior is to be attached to.
68      */
69     public function attach($owner)
70     {
71         $this->owner = $owner;//设置行为的 $owner ,使得行为可以访问、操作所依附的对象
72         foreach ($this->events() as $event => $handler) {//遍历行为中的 events() 返回的数组
73             //将准备响应的事件,通过所依附类的 on() 绑定到类上
74             $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);//这里判断了事件是否是字符串的形式,如果是字符串,表示当前行为类的方法,用$this对象的形式添加
75         }
76     }
77
78     /**
79      * Detaches the behavior object from the component.
80      * 解除行为的绑定
81      * The default implementation will unset the [[owner]] property
82      * 默认将 owner 属性设置为 null ,且解除绑定到类的处理程序
83      * and detach event handlers declared in [[events]].
84      *
85      * Make sure you call the parent implementation if you override this method.
86      */
87     public function detach()
88     {
89         if ($this->owner) {
90             foreach ($this->events() as $event => $handler) {//遍历行为中的 events() 返回的数组
91                 //通过Component的 off() 将绑定到类上的事件hanlder解除下来
92                 $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
93             }
94             $this->owner = null;//将 $owner 设置为 null ,表示这个行为没有依附到任何类上
95         }
96     }
97 }

Request.php用于获取用户请求:

 1 namespace yii\base;
 2
 3 use Yii;
 4
 5 /**
 6  * Request represents a request that is handled by an [[Application]].
 7  *
 8  * @property boolean $isConsoleRequest The value indicating whether the current request is made via console.
 9  * @property string $scriptFile Entry script file path (processed w/ realpath()).
10  *
11  * @author Qiang Xue <[email protected]>
12  * @since 2.0
13  */
14 abstract class Request extends Component
15 {
16     // 属性scriptFile,用于表示入口脚本
17     private $_scriptFile;
18     // 属性isConsoleRequest,用于表示是否是命令行应用
19     private $_isConsoleRequest;
20
21
22     /**
23      * Resolves the current request into a route and the associated parameters.
24      * 抽象函数,要求子类来实现 这个函数的功能主要是为了把Request解析成路由和相应的参数
25      * @return array the first element is the route, and the second is the associated parameters.
26      */
27     abstract public function resolve();
28
29     /**
30      * Returns a value indicating whether the current request is made via command line
31      * isConsoleRequest属性的getter函数   使用 PHP_SAPI 常量判断当前应用是否是命令行应用
32      * @return boolean the value indicating whether the current request is made via console
33      */
34     public function getIsConsoleRequest()
35     {
36         // PHP_SAPI --判断解析php服务是由那种服务器软件,是采用那种协议
37         return $this->_isConsoleRequest !== null ? $this->_isConsoleRequest : PHP_SAPI === ‘cli‘;
38     }
39
40     /**
41      * Sets the value indicating whether the current request is made via command line
42      * isConsoleRequest属性的setter函数 用来设置是否是命令行应用
43      * @param boolean $value the value indicating whether the current request is made via command line
44      */
45     public function setIsConsoleRequest($value)
46     {
47         $this->_isConsoleRequest = $value;
48     }
49
50     /**
51      * Returns entry script file path.
52      * scriptFile属性的getter函数     通过 $_SERVER[‘SCRIPT_FILENAME‘] 来获取入口脚本名
53      * @return string entry script file path (processed w/ realpath())
54      * @throws InvalidConfigException if the entry script file path cannot be determined automatically.
55      */
56     public function getScriptFile()
57     {
58         if ($this->_scriptFile === null) {
59             if (isset($_SERVER[‘SCRIPT_FILENAME‘])) {
60                 $this->setScriptFile($_SERVER[‘SCRIPT_FILENAME‘]);
61             } else {
62                 throw new InvalidConfigException(‘Unable to determine the entry script file path.‘);
63             }
64         }
65
66         return $this->_scriptFile;
67     }
68
69     /**
70      * Sets the entry script file path.
71      * scriptFile属性的setter函数,用于设置入口脚本
72      * The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable.
73      * However, for some server configurations, this may not be correct or feasible.
74      * This setter is provided so that the entry script file path can be manually specified.
75      * @param string $value the entry script file path. This can be either a file path or a path alias.
76      * @throws InvalidConfigException if the provided entry script file path is invalid.
77      */
78     public function setScriptFile($value)
79     {
80         $scriptFile = realpath(Yii::getAlias($value));
81         if ($scriptFile !== false && is_file($scriptFile)) {
82             $this->_scriptFile = $scriptFile;
83         } else {
84             throw new InvalidConfigException(‘Unable to determine the entry script file path.‘);
85         }
86     }
87 }
时间: 2024-11-09 23:59:09

Yii源码阅读笔记(九)的相关文章

Yii源码阅读笔记 - 日志组件

?使用 Yii框架为开发者提供两个静态方法进行日志记录: Yii::log($message, $level, $category);Yii::trace($message, $category); 两者的区别在于后者依赖于应用开启调试模式,即定义常量YII_DEBUG: defined('YII_DEBUG') or define('YII_DEBUG', true); Yii::log方法的调用需要指定message的level和category.category是格式为“xxx.yyy.z

Yii源码阅读笔记(一)

今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于web目录的index.php,用于启动web应用和配置一些路径参数. index.php—— 1 // comment out the following two lines when deployed to production 2 defined('YII_DEBUG') or define('Y

Yii源码阅读笔记(二十九)

动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel is a model class primarily used to support ad hoc data validation. * DynamicModel是一种主要用于支持ad hoc数据验证模型类 * * The typical usage of DynamicModel is as fo

Yii源码阅读笔记(十九)

View中渲染view视图文件的前置和后置方法,以及渲染动态内容的方法: 1 /** 2 * @return string|boolean the view file currently being rendered. False if no view file is being rendered. 3 */ 4 public function getViewFile() 5 { 6 return end($this->_viewFiles);//返回[_viewFiles]中的最后一个view

Yii源码阅读笔记(二)

接下来阅读BaseYii.php vendor/yiisoft/yii2/Yii.php—— 1 namespace yii; 2 3 use yii\base\InvalidConfigException; 4 use yii\base\InvalidParamException; 5 use yii\base\UnknownClassException; 6 use yii\log\Logger; 7 use yii\di\Container; 第1行定义命名空间为yii: 第3到7行使用了

Yii源码阅读笔记(三)

接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— 1 public static function getRootAlias($alias)//获取根别名 2 { //查找别名中斜线的位置 3 $pos = strpos($alias, '/'); //根据斜线的结果判断,如果不包含斜线,表示输入为根别名,否则截取斜线前面的部分作为根别名 4 $root = $pos === false ? $alias : substr($alias

Yii源码阅读笔记(八)

前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: 1 <?php 2 /** 3 * @link http://www.yiiframework.com/ 4 * @copyright Copyright (c) 2008 Yii Software LLC 5 * @license http://www.yiiframework.com/license/ 6 */ 7 8 nam

Yii源码阅读笔记(三十三)

ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: 1 namespace yii\di; 2 3 use Yii; 4 use Closure; 5 use yii\base\Component; 6 use yii\base\InvalidConfigException; 7 8 /** 9 * ServiceLocator implements a [service locator](http://en.wi

Yii源码阅读笔记(二十一)——请求处理流程

Yii2请求处理流程: 首先:项目路径/web/index.php (new yii\web\Application($config))->run();//根据配置文件创建App实例,先实例化yii\web\Application(),然后调用run()方法 该语句可分为两步: $application = new yii\web\Application($config);//实例化app $application->run();//调用run()方法 $config 为配置文件,通过 req