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

 namespace yii\base;

 /**
  * Behavior is the base class for all behavior classes.
  * 所有行为的基类
  * A behavior can be used to enhance the functionality of an existing component without modifying its code.
  * In particular, it can "inject" its own methods and properties into the component
  * and make them directly accessible via the component. It can also respond to the events triggered in the component
  * and thus intercept the normal code execution.
  *
  * @author Qiang Xue <qiang.xue@gmail.com>
  * @since 2.0
  */
 class Behavior extends Object
 {
     /**
      * @var 行为的依附对象
      */
     public $owner;

     /**
      * Declares event handlers for the [[owner]]'s events.
      * 表示这个行为将对类的何种事件进行何种反馈即可
      *
      * Child classes may override this method to declare what PHP callbacks should
      * be attached to the events of the [[owner]] component.
      *
      * The callbacks will be attached to the [[owner]]'s events when the behavior is
      * attached to the owner; and they will be detached from the events when
      * the behavior is detached from the component.
      *
      * The callbacks can be any of the following:
      * 事件handler可以是以下形式:
      *
      * - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
      *  字符串,表示行为类的方法,和事件不同,事件总的字符串形式表示全局函数,这里表示当前行为类的方法
      * - object method: `[$object, 'handleClick']`
      *  一个对象或类的成员函数,以数组的形式
      * - static method: `['Page', 'handleClick']`
      * - anonymous function: `function ($event) { ... }`
      * 一个匿名函数
      *
      * The following is an example:
      *
      * ```php
      * [
      *     Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
      *     Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
      * ]
      * ```
      *
      * @return 行为所有要响应的事件.
      */
     public function events()
     {
         return [];
     }

     /**
      * Attaches the behavior object to the component.
      * 绑定行为到组件
      * The default implementation will set the [[owner]] property
      * and attach event handlers as declared in [[events]].
      * 该方法会默认设置[[owner]]属性,且添加事件处理程序绑定到组件
      * Make sure you call the parent implementation if you override this method.
      * @param Component $owner the component that this behavior is to be attached to.
      */
     public function attach($owner)
     {
         $this->owner = $owner;//设置行为的 $owner ,使得行为可以访问、操作所依附的对象
         foreach ($this->events() as $event => $handler) {//遍历行为中的 events() 返回的数组
             //将准备响应的事件,通过所依附类的 on() 绑定到类上
             $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);//这里判断了事件是否是字符串的形式,如果是字符串,表示当前行为类的方法,用$this对象的形式添加
         }
     }

     /**
      * Detaches the behavior object from the component.
      * 解除行为的绑定
      * The default implementation will unset the [[owner]] property
      * 默认将 owner 属性设置为 null ,且解除绑定到类的处理程序
      * and detach event handlers declared in [[events]].
      *
      * Make sure you call the parent implementation if you override this method.
      */
     public function detach()
     {
         if ($this->owner) {
             foreach ($this->events() as $event => $handler) {//遍历行为中的 events() 返回的数组
                 //通过Component的 off() 将绑定到类上的事件hanlder解除下来
                 $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
             }
             $this->owner = null;//将 $owner 设置为 null ,表示这个行为没有依附到任何类上
         }
     }
 }

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

 namespace yii\base;

 use Yii;

 /**
  * Request represents a request that is handled by an [[Application]].
  *
  * @property boolean $isConsoleRequest The value indicating whether the current request is made via console.
  * @property string $scriptFile Entry script file path (processed w/ realpath()).
  *
  * @author Qiang Xue <qiang.xue@gmail.com>
  * @since 2.0
  */
 abstract class Request extends Component
 {
     // 属性scriptFile,用于表示入口脚本
     private $_scriptFile;
     // 属性isConsoleRequest,用于表示是否是命令行应用
     private $_isConsoleRequest;

     /**
      * Resolves the current request into a route and the associated parameters.
      * 抽象函数,要求子类来实现 这个函数的功能主要是为了把Request解析成路由和相应的参数
      * @return array the first element is the route, and the second is the associated parameters.
      */
     abstract public function resolve();

     /**
      * Returns a value indicating whether the current request is made via command line
      * isConsoleRequest属性的getter函数   使用 PHP_SAPI 常量判断当前应用是否是命令行应用
      * @return boolean the value indicating whether the current request is made via console
      */
     public function getIsConsoleRequest()
     {
         // PHP_SAPI --判断解析php服务是由那种服务器软件,是采用那种协议
         return $this->_isConsoleRequest !== null ? $this->_isConsoleRequest : PHP_SAPI === 'cli';
     }

     /**
      * Sets the value indicating whether the current request is made via command line
      * isConsoleRequest属性的setter函数 用来设置是否是命令行应用
      * @param boolean $value the value indicating whether the current request is made via command line
      */
     public function setIsConsoleRequest($value)
     {
         $this->_isConsoleRequest = $value;
     }

     /**
      * Returns entry script file path.
      * scriptFile属性的getter函数     通过 $_SERVER['SCRIPT_FILENAME'] 来获取入口脚本名
      * @return string entry script file path (processed w/ realpath())
      * @throws InvalidConfigException if the entry script file path cannot be determined automatically.
      */
     public function getScriptFile()
     {
         if ($this->_scriptFile === null) {
             if (isset($_SERVER['SCRIPT_FILENAME'])) {
                 $this->setScriptFile($_SERVER['SCRIPT_FILENAME']);
             } else {
                 throw new InvalidConfigException('Unable to determine the entry script file path.');
             }
         }

         return $this->_scriptFile;
     }

     /**
      * Sets the entry script file path.
      * scriptFile属性的setter函数,用于设置入口脚本
      * The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable.
      * However, for some server configurations, this may not be correct or feasible.
      * This setter is provided so that the entry script file path can be manually specified.
      * @param string $value the entry script file path. This can be either a file path or a path alias.
      * @throws InvalidConfigException if the provided entry script file path is invalid.
      */
     public function setScriptFile($value)
     {
         $scriptFile = realpath(Yii::getAlias($value));
         if ($scriptFile !== false && is_file($scriptFile)) {
             $this->_scriptFile = $scriptFile;
         } else {
             throw new InvalidConfigException('Unable to determine the entry script file path.');
         }
     }
 }

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

  1. Yii源码阅读笔记(一)

    今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...

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

    动态模型DynamicModel类,用于实现模型内数据验证: namespace yii\base; use yii\validators\Validator; /** * DynamicModel ...

  3. Yii源码阅读笔记(十九)

    View中渲染view视图文件的前置和后置方法,以及渲染动态内容的方法: /** * @return string|boolean the view file currently being rend ...

  4. Yii源码阅读笔记(八)

    前面阅读了Yii2的两个基本类Object和Component,了解了Yii的三个重要概念属性.事件.行为,下面开始阅读Event类,Event类是所有事件类的基类: <?php /** * @ ...

  5. Yii源码阅读笔记(三)

    接着上次的继续阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— public static function getRootAlias($alias)// ...

  6. Yii源码阅读笔记(二)

    接下来阅读BaseYii.php vendor/yiisoft/yii2/BaseYii.php—— namespace yii; use yii\base\InvalidConfigExceptio ...

  7. Yii源码阅读笔记(三十五)

    Container,用于动态地创建.注入依赖单元,映射依赖关系等功能,减少了许多代码量,降低代码耦合程度,提高项目的可维护性. namespace yii\di; use ReflectionClas ...

  8. Yii源码阅读笔记(三十四)

    Instance类, 表示依赖注入容器或服务定位器中对某一个对象的引用 namespace yii\di; use Yii; use yii\base\InvalidConfigException; ...

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

    ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: namespace yii\di; use Yii; use Cl ...

随机推荐

  1. Lucene查询索引(分页)

    分页查询只需传入每页显示记录数和当前页就可以实现分页查询功能 Lucene分页查询是对搜索返回的结果进行分页,而不是对搜索结果的总数量进行分页,因此我们搜索的时候都是返回前n条记录 package c ...

  2. JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)

    package com.itcast.test20140113; import java.util.ArrayList; import java.util.Arrays; import java.ut ...

  3. linux tricks 之 BUILD_BUG_ON_ZERO.

    ------------------------------------------- 本文系作者原创, 欢迎大家转载! 转载请注明出处:netwalker.blog.chinaunix.net -- ...

  4. Java Hour 21 Weather

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为21 Hour,请各位不吝赐教. 继续心情不佳,那 ...

  5. 封装了get post方法

    function g($name, $defaultValue = "") { // php这里区分大小写,将两者都变为小写 $_GET = array_change_key_ca ...

  6. ajax请求原理及jquery $.ajax封装全解析

    .ajax原理: Ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,然后用javascript来操作DOM而更新页面.这其中最关键的一步就是从服务器获得 ...

  7. hdu 1430+hdu 3567(预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路:由于只是8种颜色,所以标号就无所谓了,对起始状态重新修改标号为 12345678,对目标状 ...

  8. 手机响应式wap网页最佳方案

    wap页面怎么做? 这个问题困扰了我好久,在PC上我们惯用的px单位在手机上根本不适用,即使我们写了<meta name="viewport" content="w ...

  9. filter()函数 条件筛选

    filter()函数 filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 ...

  10. Playmaker Input篇教程之Playmaker购买下载和导入

    Playmaker Input篇教程之Playmaker购买下载和导入 Playmaker Input篇认识Playmaker Playmaker是Unity的插件,其标志如图1-1所示.开发者使用它 ...