0

0

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

php中文网

php中文网

发布时间:2016-07-06 14:25:14

|

1480人浏览过

|

来源于php中文网

原创

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

theme 类,应用的主题,通过替换路径实现主题的应用,方法为获取根路径和根链接:yii2\base\theme.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 yii\helpers\FileHelper;
 12 
 13 /**
 14  * Theme represents an application theme.
 15  * Theme 类,应用的主题
 16  * When [[View]] renders a view file, it will check the [[View::theme|active theme]]
 17  * to see if there is a themed version of the view file exists. If so, the themed version will be rendered instead.
 18  * 视图对象[[View]]渲染视图文件的时候,会检查视图的主题是否存在,如果存在则渲染主题取代默认样式
 19  * A theme is a directory consisting of view files which are meant to replace their non-themed counterparts.
 20  *
 21  * Theme uses [[pathMap]] to achieve the view file replacement:
 22  *
 23  * 1. It first looks for a key in [[pathMap]] that is a substring of the given view file path;
 24  *  首先查找关键字,关键字是一个给定的视图路径的字符串
 25  * 2. If such a key exists, the corresponding value will be used to replace the corresponding part
 26  *    in the view file path;关键字存在,则用对应值替换给定的视图文件路径中对应的部分
 27  * 3. It will then check if the updated view file exists or not. If so, that file will be used
 28  *    to replace the original view file.检查替换后的路径对应的文件是否存在,存在就替换原文件
 29  * 4. If Step 2 or 3 fails, the original view file will be used.
 30  * 2和3失败的话,返回原来的路径
 31  * For example, if [[pathMap]] is `['@app/views' => '@app/themes/basic']`,
 32  * then the themed version for a view file `@app/views/site/index.php` will be
 33  * `@app/themes/basic/site/index.php`.
 34  *
 35  * It is possible to map a single path to multiple paths. For example,
 36  *
 37  * ~~~
 38  * 'pathMap' => [
 39  *     '@app/views' => [
 40  *         '@app/themes/christmas',
 41  *         '@app/themes/basic',
 42  *     ],
 43  * ]
 44  * ~~~
 45  *
 46  * In this case, the themed version could be either `@app/themes/christmas/site/index.php` or
 47  * `@app/themes/basic/site/index.php`. The former has precedence over the latter if both files exist.
 48  *
 49  * To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
 50  * component like the following:
 51  *
 52  * ~~~
 53  * 'view' => [
 54  *     'theme' => [
 55  *         'basePath' => '@app/themes/basic',
 56  *         'baseUrl' => '@web/themes/basic',
 57  *     ],
 58  * ],
 59  * ~~~
 60  *
 61  * The above configuration specifies a theme located under the "themes/basic" directory of the Web folder
 62  * that contains the entry script of the application. If your theme is designed to handle modules,
 63  * you may configure the [[pathMap]] property like described above.
 64  *
 65  * @property string $basePath The root path of this theme. All resources of this theme are located under this
 66  * directory.
 67  * @property string $baseUrl The base URL (without ending slash) for this theme. All resources of this theme
 68  * are considered to be under this base URL. This property is read-only.
 69  *
 70  * @author Qiang Xue 
 71  * @since 2.0
 72  */
 73 class Theme extends Component
 74 {
 75     /**
 76      * @var array the mapping between view directories and their corresponding themed versions.
 77      * This property is used by [[applyTo()]] when a view is trying to apply the theme.
 78      * Path aliases can be used when specifying directories.
 79      * 路径映射属性 设置替换映射关系
 80      * If this property is empty or not set, a mapping [[Application::basePath]] to [[basePath]] will be used.
 81      */
 82     public $pathMap;
 83 
 84     private $_baseUrl;//设置要访问资源的url
 85 
 86     /**
 87      * @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
 88      * to be under this base URL. 返回当前主题的基础链接,其他资源都在链接里
 89      */
 90     public function getBaseUrl()
 91     {
 92         return $this->_baseUrl;
 93     }
 94 
 95     /**
 96      * @param $url string the base URL or path alias for this theme. All resources of this theme are considered
 97      * to be under this base URL. 设置基础链接
 98      */
 99     public function setBaseUrl($url)
100     {
101         $this->_baseUrl = rtrim(Yii::getAlias($url), '/');
102     }
103 
104     private $_basePath;//根路径
105 
106     /**
107      * @return string the root path of this theme. All resources of this theme are located under this directory.
108      * 得到当前主题的根路径
109      * @see pathMap
110      */
111     public function getBasePath()
112     {
113         return $this->_basePath;
114     }
115 
116     /**
117      * @param string $path the root path or path alias of this theme. All resources of this theme are located
118      * under this directory. 设置当前主题根路径
119      * @see pathMap
120      */
121     public function setBasePath($path)
122     {
123         $this->_basePath = Yii::getAlias($path);
124124     }
125 
126     /**
127      * Converts a file to a themed file if possible. 将一个文件替换主题文件
128      * If there is no corresponding themed file, the original file will be returned.
129      * 没有相应的主题文件,返回原文件。
130      * @param string $path the file to be themed
131      * @return string the themed file, or the original file if the themed version is not available.
132      * @throws InvalidConfigException if [[basePath]] is not set
133      */
134     public function applyTo($path)
135     {
136         $pathMap = $this->pathMap; //取得路径映射
137         if (empty($pathMap)) {//没有设置值 抛出异常
138             if (($basePath = $this->getBasePath()) === null) {
139                 throw new InvalidConfigException('The "basePath" property must be set.');
140             }
141             //设置值为[模块根路径=>主题根路径]的形式
142             $pathMap = [Yii::$app->getBasePath() => [$basePath]];
143         }
144 
145         $path = FileHelper::normalizePath($path);//对路径中的"/"."\"进行统一
146 
147         foreach ($pathMap as $from => $tos) {
148             //映射数组中的来源
149             $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
150             if (strpos($path, $from) === 0) {//如果在$path中有可替换的旧值
151                 $n = strlen($from);
152                 foreach ((array) $tos as $to) {
153                     $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
154                     $file = $to . substr($path, $n);//把$path中的$from替换为$to
155                     if (is_file($file)) {
156                         return $file; //是文件直接返回
157                     }
158                 }
159             }
160         }
161 
162         return $path;
163     }
164 
165     /**
166      * Converts a relative URL into an absolute URL using [[baseUrl]].
167      * 将一个相对URL转换为绝对URL
168      * @param string $url the relative URL to be converted.要转换的相对URL
169      * @return string the absolute URL  替换后的绝对URL
170      * @throws InvalidConfigException if [[baseUrl]] is not set
171      */
172     public function getUrl($url)
173     {
174         if (($baseUrl = $this->getBaseUrl()) !== null) {//URL存在,进行转换
175             return $baseUrl . '/' . ltrim($url, '/');
176         } else {//不存在抛出异常
177             throw new InvalidConfigException('The "baseUrl" property must be set.');
178         }
179     }
180 
181     /**
182      * Converts a relative file path into an absolute one using [[basePath]].
183      * 通过相对路径生成绝对路径
184      * @param string $path the relative file path to be converted. 要转换的相对文件路径。
185      * @return string the absolute file path 转换后的绝对路径
186      * @throws InvalidConfigException if [[baseUrl]] is not set
187      */
188     public function getPath($path)
189     {
190         if (($basePath = $this->getBasePath()) !== null) {
191             //返回拼接的路径
192             return $basePath . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
193         } else {
194             throw new InvalidConfigException('The "basePath" property must be set.');
195         }
196     }
197 }

 

MagickPen
MagickPen

在线AI英语写作助手,像魔术师一样在几秒钟内写出任何东西。

下载

相关标签:

yii

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

热门AI工具

更多
DeepSeek
DeepSeek

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

豆包大模型
豆包大模型

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

通义千问
通义千问

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

腾讯元宝
腾讯元宝

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

文心一言
文心一言

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

讯飞写作
讯飞写作

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

即梦AI
即梦AI

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

ChatGPT
ChatGPT

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

相关专题

更多
java入门学习合集
java入门学习合集

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

2

2026.01.29

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

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

2

2026.01.29

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

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

0

2026.01.29

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

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

0

2026.01.29

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

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

3

2026.01.29

clawdbot ai使用教程 保姆级clawdbot部署安装手册
clawdbot ai使用教程 保姆级clawdbot部署安装手册

Clawdbot是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

25

2026.01.29

clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址
clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址

clawdbot龙虾机器人官网入口:https://clawd.bot/,clawdbot ai是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

16

2026.01.29

Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

8

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

622

2026.01.28

热门下载

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

精品课程

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

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