Widget类是所有小部件的基类,开始,结束和渲染小部件内容的方法的注释:

 namespace yii\base;

 use Yii;
 use ReflectionClass;

 /**
  * Widget is the base class for widgets.
  * Widget是所有小部件的基类
  *
  * @property string $id ID of the widget.
  * @property \yii\web\View $view The view object that can be used to render views or view files. Note that the
  * type of this property differs in getter and setter. See [[getView()]] and [[setView()]] for details.
  * @property string $viewPath The directory containing the view files for this widget. This property is
  * read-only.
  *
  * @author Qiang Xue <qiang.xue@gmail.com>
  * @since 2.0
  */
 class Widget extends Component implements ViewContextInterface
 {
     /**
      * @var integer a counter used to generate [[id]] for widgets.
      * @var integer 一个用于生成widget ID的计数器
      * @internal
      */
     public static $counter = 0;
     /**
      * @var string the prefix to the automatically generated widget IDs.
      * @var string widget IDs自动生成的前缀
      * @see getId()
      */
     public static $autoIdPrefix = 'w';
     /**
      * @var Widget[] the widgets that are currently being rendered (not ended). This property
      * is maintained by [[begin()]] and [[end()]] methods.
      * @var 目前正在呈现的小部件
      * @internal
      */
     public static $stack = [];

     /**
      * Begins a widget.
      * 开始一个小部件
      * This method creates an instance of the calling class. It will apply the configuration
      * to the created instance. A matching [[end()]] call should be called later.
      * 该方法将应用配置文件创建调用类的实例,有一个[[end()]]方法相对应
      * @param array $config name-value pairs that will be used to initialize the object properties
      * @return static the newly created widget instance
      */
     public static function begin($config = [])
     {
         $config['class'] = get_called_class();// get_called_class -- 后期静态绑定("Late Static Binding")类的名称
                                               // 就是用那个类调用的这个方法,就返回那个类,返回值中带有 namespace
         /* @var $widget Widget */
         $widget = Yii::createObject($config);//通过类名和传入的配置,实例化调用类
         static::$stack[] = $widget;//将实例放入正在呈现的小部件堆栈中

         return $widget;//返回小部件实例
     }

     /**
      * Ends a widget.
      * 结束一个小部件
      * Note that the rendering result of the widget is directly echoed out.
      * @return static the widget instance that is ended.
      * @throws InvalidCallException if [[begin()]] and [[end()]] calls are not properly nested
      */
     public static function end()
     {
         if (!empty(static::$stack)) {//正在呈现的小部件堆栈中存在调用类实例
             $widget = array_pop(static::$stack);//则从堆栈中删除最后一个实例--调用当前小部件的实例
             if (get_class($widget) === get_called_class()) {//如果删除的实例的类名和当前调用类的类名相同
                 echo $widget->run();//输出小部件的内容
                 return $widget;//返回调用类实例
             } else {
                 throw new InvalidCallException('Expecting end() of ' . get_class($widget) . ', found ' . get_called_class());
             }
         } else {//抛出异常,未找到对应的begin()开始方法
             throw new InvalidCallException('Unexpected ' . get_called_class() . '::end() call. A matching begin() is not found.');
         }
     }

     /**
      * Creates a widget instance and runs it.
      * 创建一个widget实例,并运行它
      * The widget rendering result is returned by this method.
      * widget的渲染结果将被该方法返回
      * @param array $config name-value pairs that will be used to initialize the object properties
      * @return string the rendering result of the widget.
      * @throws \Exception
      */
     public static function widget($config = [])
     {
         ob_start();//打开一个输出缓冲区,所有的输出信息不再直接发送到浏览器,而是保存在输出缓冲区里面
         ob_implicit_flush(false);//打开或关闭绝对刷新,默认为关闭,打开后ob_implicit_flush(true),
           //所谓绝对刷新,即当有输出语句(e.g: echo)被执行时,便把输出直接发送到浏览器,而不再需要调用flush()或等到脚本结束时才输出
         try {
             /* @var $widget Widget */
             $config['class'] = get_called_class();//获取调用类的类名
             $widget = Yii::createObject($config);//实例化调用类
             $out = $widget->run();//运行widget
         } catch (\Exception $e) {
             // close the output buffer opened above if it has not been closed already
             if (ob_get_level() > 0) {//返回输出缓冲机制的嵌套级别
                 ob_end_clean();//删除内部缓冲区的内容,关闭缓冲区(不输出)
             }
             throw $e;
         }

         return ob_get_clean() . $out;//返回内部缓冲区的内容,关闭缓冲区
     }

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

  1. Yii源码阅读笔记(十八)

    View中的查找视图文件方法和渲染文件方法 /** * Finds the view file based on the given view name. * 通过view文件名查找view文件 * ...

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

    Model类,集中整个应用的数据和业务逻辑—— /** * Generates a user friendly attribute label based on the give attribute ...

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

    Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mai ...

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

    Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: /** * Returns a list of scenarios and the corresponding active attr ...

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

    Action类,控制器中方法的基类: namespace yii\base; use Yii; /** * Action is the base class for all controller ac ...

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

    控制器类,所有控制器的基类,用于调用模型和布局,输出到视图 namespace yii\base; use Yii; /** * Controller is the base class for cl ...

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

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

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

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

  9. Werkzeug源码阅读笔记(三)

    这次主要讲下werkzeug中的Local. 源码在werkzeug/local.py Thread Local 在Python中,状态是保存在对象中.Thread Local是一种特殊的对象,它是对 ...

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

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

随机推荐

  1. JavaScript——理解闭包及作用

    js是一个函数级语言,变量的作用域是: 内部可以访问内部,内部可以访问外部,外部不能访问内部. 如果要在外部,访问函数内部的变量,就要用到闭包.闭包就是指访问到了本不该访问的变量. 闭包作用1:实现封 ...

  2. JVM内存管理------垃圾搜集器精解(让你在垃圾搜集器的世界里耍的游刃有余)

    引言 在上一章我们已经探讨过hotspot上垃圾搜集器的实现,一共有六种实现六种组合.本次LZ与各位一起探讨下这六种搜集器各自的威力以及组合的威力如何. 为了方便各位的观看与对比,LZ决定采用当初写设 ...

  3. bootstrap-datetimepicker 日期控件的开始日期

    今天做日期控件,需求要求设置一个时间范围限制,选择从今天开始的日期才可以选择,今天以前都不可以选择 主要体现在bootstrap-datetimepicker控件下面的2个日期参数 weekStart ...

  4. Spring Web MVC 多viewResolver视图解析器解决方案

    viewResolver的定义如下: public interface ViewResolver { View resolveViewName(String viewName, Locale loca ...

  5. 我为什么喜欢用C#来做并发编程

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:就语言和运行时层面,C#做并发编程一点都不弱,缺的是生态和社区. 硅谷才女朱赟(我的家 ...

  6. 解决VS+opencv中Debug版本与Release版本lib切换的问题

    Author: Maddock Date: 2015-03-26 09:34:48 问题来源:http://bbs.csdn.net/topics/390733725 PS: 按照上述方法做的时候,在 ...

  7. poj2631 求树的直径裸题

    题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...

  8. 浏览器控制台console

    console对象 console对象代表浏览器的JavaScript控制台.虽然它还不是标准,但是各大浏览器都原生支持,已经成为事实上的标准. console对象主要有两个作用: 显示网页代码运行时 ...

  9. 同步、更新、下载Android Source & SDK from 国内镜像站(转载)

    同步.更新.下载Android Source & SDK from 国内镜像站 转自: 同步.更新.下载Android Source & SDK from 国内镜像站 Download ...

  10. 如何使用PL/SQL Developer查看和杀掉session

    http://jingyan.baidu.com/article/3ea51489eb65b152e61bba8b.html