0

0

yii2源码学习笔记(二十),yii2源码学习笔记

php中文网

php中文网

发布时间:2016-07-06 14:24:30

|

1098人浏览过

|

来源于php中文网

原创

yii2源码学习笔记(二十),yii2源码学习笔记

widget类是所有部件的基类。yii2\base\widget.php

  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 namespace yii\base;
  9 
 10 use Yii;
 11 use ReflectionClass;
 12 
 13 /**
 14  * Widget is the base class for widgets.
 15  * Widget是所有小部件的基类
 16  * @property string $id ID of the widget. 小部件标识
 17  * @property \yii\web\View $view The view object that can be used to render views or view files. Note that the
 18  * type of this property differs in getter and setter. See [[getView()]] and [[setView()]] for details.
 19  * 用于渲染视图或视图文件的视图对象 在getter 和 setter中是不同的
 20  * @property string $viewPath The directory containing the view files for this widget. This property is
 21  * read-only. 包含此控件的视图文件目录
 22  *
 23  * @author Qiang Xue 
 24  * @since 2.0
 25  */
 26 class Widget extends Component implements ViewContextInterface
 27 {
 28     /**
 29      * @var integer a counter used to generate [[id]] for widgets.
 30      * @internal 用于生成widget ID的计数器
 31      */
 32     public static $counter = 0;
 33     /**
 34      * @var string the prefix to the automatically generated widget IDs.
 35      * @see getId() 自动生成的前缀
 36      */
 37     public static $autoIdPrefix = 'w';
 38     /**
 39      * @var Widget[] the widgets that are currently being rendered (not ended). This property
 40      * is maintained by [[begin()]] and [[end()]] methods. 目前正在渲染的小部件
 41      * @internal
 42      */
 43     public static $stack = [];
 44 
 45 
 46     /**
 47      * Begins a widget.  开始一个部件
 48      * This method creates an instance of the calling class. It will apply the configuration
 49      * to the created instance. A matching [[end()]] call should be called later.
 50      * 将应用配置文件创建调用类的实例,与[end()]方法相对应
 51      * @param array $config name-value pairs that will be used to initialize the object properties
 52      * 用于初始化属性的参数
 53      * @return static the newly created widget instance 静态新创建的部件实例
 54      */
 55     public static function begin($config = [])
 56     {
 57         $config['class'] = get_called_class();//后期静态绑定类的名称
 58         /* @var $widget Widget */
 59         $widget = Yii::createObject($config);//通过类名和传入的配置,实例化调用类
 60         static::$stack[] = $widget;//将对象放入正在渲染的部件堆栈中
 61 
 62         return $widget;
 63     }
 64 
 65     /**
 66      * Ends a widget.   结束小部件
 67      * Note that the rendering result of the widget is directly echoed out.渲染结果是直接呼应的
 68      * @return static the widget instance that is ended. 静态结束的部件实例。
 69      * @throws InvalidCallException if [[begin()]] and [[end()]] calls are not properly nested
 70      */
 71     public static function end()
 72     {
 73         if (!empty(static::$stack)) {//正在呈现的小部件堆栈中存在调用类实例
 74             $widget = array_pop(static::$stack);//从堆栈中删除最后一个实例
 75             if (get_class($widget) === get_called_class()) {
 76                 echo $widget->run(); //如果删除的实例类名和当前调用类名相同,输出小部件的内容
 77                 return $widget;
 78             } else {
 79                 throw new InvalidCallException("Expecting end() of " . get_class($widget) . ", found " . get_called_class());
 80             }
 81         } else {
 82             throw new InvalidCallException("Unexpected " . get_called_class() . '::end() call. A matching begin() is not found.');
 83         }
 84     }
 85 
 86     /**
 87      * Creates a widget instance and runs it.   创建一个部件实例,并运行
 88      * The widget rendering result is returned by this method. 返回部件渲染的结果。
 89      * @param array $config name-value pairs that will be used to initialize the object properties
 90      * 用于初始化对象属性的参数
 91      * @return string the rendering result of the widget. 控件的渲染结果。
 92      */
 93     public static function widget($config = [])
 94     {
 95         ob_start(); //打开输出缓冲区
 96         ob_implicit_flush(false);//关闭绝对刷新
 97         /* @var $widget Widget */
 98         $config['class'] = get_called_class(); //获取调用类的类名
 99         $widget = Yii::createObject($config);   //实例化类
100         $out = $widget->run();//运行部件
101 
102         return ob_get_clean() . $out; //返回内部缓冲区的内容,关闭缓冲区
103     }
104 
105     private $_id;
106 
107     /**
108      * Returns the ID of the widget. 返回插件的标识
109      * @param boolean $autoGenerate whether to generate an ID if it is not set previously
110      * 是否生成一个唯一标识,如果没有设置
111      * @return string ID of the widget. 部件唯一标识
112      */
113     public function getId($autoGenerate = true)
114     {
115         if ($autoGenerate && $this->_id === null) {
116             //如果标识为空,并且设置为允许自动生成标识,自动生成
117             $this->_id = static::$autoIdPrefix . static::$counter++;
118         }
119 
120         return $this->_id;
121     }
122 
123     /**
124124      * Sets the ID of the widget. 设置小部件标识
125      * @param string $value id of the widget. 部件的标识。
126      */
127     public function setId($value)
128     {
129         $this->_id = $value;
130     }
131 
132     private $_view;
133 
134     /**
135      * Returns the view object that can be used to render views or view files.返回视图对象
136      * The [[render()]] and [[renderFile()]] methods will use
137      * this view object to implement the actual view rendering.
138      * [render()]和[renderFile()]方法用视图对象实现实际的视图显示。
139      * If not set, it will default to the "view" application component.
140      * @return \yii\web\View the view object that can be used to render views or view files.
141      */
142     public function getView()
143     {
144         if ($this->_view === null) {
145             $this->_view = Yii::$app->getView();//如果视图对象为空,调用getView()取得视图对象实例
146         }
147 
148         return $this->_view;
149     }
150 
151     /**
152      * Sets the view object to be used by this widget. 设置当前部件调用的视图对象实例
153      * @param View $view the view object that can be used to render views or view files.
154      */
155     public function setView($view)
156     {
157         $this->_view = $view;//要用的视图对象
158     }
159 
160     /**
161      * Executes the widget. 执行部件
162      * @return string the result of widget execution to be outputted.
163      * 控件执行的结果输出。
164      */
165     public function run()
166     {
167     }
168 
169     /**
170      * Renders a view.
171      * The view to be rendered can be specified in one of the following formats:
172      * 渲染一个视图   实际调用View类中的同名方法 渲染的视图可以用下列方式指定路径
173      * - path alias (e.g. "@app/views/site/index");
174      * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
175      *   The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
176      * - absolute path within module (e.g. "/site/index"): the view name starts with a single slash.
177      *   The actual view file will be looked for under the [[Module::viewPath|view path]] of the currently
178      *   active module.
179      * - relative path (e.g. "index"): the actual view file will be looked for under [[viewPath]].
180      *
181      * If the view name does not contain a file extension, it will use the default one `.php`.
182      *
183      * @param string $view the view name.   视图名
184      * @param array $params the parameters (name-value pairs) that should be made available in the view.
185      * 在视图中可用的参数
186      * @return string the rendering result. 渲染结果
187      * @throws InvalidParamException if the view file does not exist.
188      */
189     public function render($view, $params = [])
190     {
191         //调用view类中的render渲染指定的视图
192         return $this->getView()->render($view, $params, $this);
193     }
194 
195     /**
196      * Renders a view file. 渲染一个视图文件 同上
197      * @param string $file the view file to be rendered. This can be either a file path or a path alias.
198      * @param array $params the parameters (name-value pairs) that should be made available in the view.
199      * @return string the rendering result.
200      * @throws InvalidParamException if the view file does not exist.
201      */
202     public function renderFile($file, $params = [])
203     {
204         return $this->getView()->renderFile($file, $params, $this);
205     }
206 
207     /**
208      * Returns the directory containing the view files for this widget. 返回视图文件路径
209      * The default implementation returns the 'views' subdirectory under the directory containing the widget class file.
210      * @return string the directory containing the view files for this widget.
211      */
212     public function getViewPath()
213     {
214         $class = new ReflectionClass($this);
215         //取得部件类文件的目录,拼接为视图目录
216         return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
217     }
218 }

 

Videoleap
Videoleap

Videoleap是一个一体化的视频编辑平台

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

14

2026.01.30

c++ 字符串格式化
c++ 字符串格式化

本专题整合了c++字符串格式化用法、输出技巧、实践等等内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.30

java 字符串格式化
java 字符串格式化

本专题整合了java如何进行字符串格式化相关教程、使用解析、方法详解等等内容。阅读专题下面的文章了解更多详细教程。

12

2026.01.30

python 字符串格式化
python 字符串格式化

本专题整合了python字符串格式化教程、实践、方法、进阶等等相关内容,阅读专题下面的文章了解更多详细操作。

4

2026.01.30

java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

20

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

18

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

19

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

3

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Yii2中文手册
Yii2中文手册

共0课时 | 0人学习

thinkphp基础介绍和yii2基础介绍
thinkphp基础介绍和yii2基础介绍

共10课时 | 2.3万人学习

Yii2框架基础视频教程
Yii2框架基础视频教程

共22课时 | 2.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号