Theme 类,应用的主题,通过替换路径实现主题的应用,方法为获取根路径和根链接:yii2\base\Theme.php

 <?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/ namespace yii\base; use Yii;
use yii\helpers\FileHelper; /**
* Theme represents an application theme.
* Theme 类,应用的主题
* When [[View]] renders a view file, it will check the [[View::theme|active theme]]
* to see if there is a themed version of the view file exists. If so, the themed version will be rendered instead.
* 视图对象[[View]]渲染视图文件的时候,会检查视图的主题是否存在,如果存在则渲染主题取代默认样式
* A theme is a directory consisting of view files which are meant to replace their non-themed counterparts.
*
* Theme uses [[pathMap]] to achieve the view file replacement:
*
* 1. It first looks for a key in [[pathMap]] that is a substring of the given view file path;
* 首先查找关键字,关键字是一个给定的视图路径的字符串
* 2. If such a key exists, the corresponding value will be used to replace the corresponding part
* in the view file path;关键字存在,则用对应值替换给定的视图文件路径中对应的部分
* 3. It will then check if the updated view file exists or not. If so, that file will be used
* to replace the original view file.检查替换后的路径对应的文件是否存在,存在就替换原文件
* 4. If Step 2 or 3 fails, the original view file will be used.
* 2和3失败的话,返回原来的路径
* For example, if [[pathMap]] is `['@app/views' => '@app/themes/basic']`,
* then the themed version for a view file `@app/views/site/index.php` will be
* `@app/themes/basic/site/index.php`.
*
* It is possible to map a single path to multiple paths. For example,
*
* ~~~
* 'pathMap' => [
* '@app/views' => [
* '@app/themes/christmas',
* '@app/themes/basic',
* ],
* ]
* ~~~
*
* In this case, the themed version could be either `@app/themes/christmas/site/index.php` or
* `@app/themes/basic/site/index.php`. The former has precedence over the latter if both files exist.
*
* To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
* component like the following:
*
* ~~~
* 'view' => [
* 'theme' => [
* 'basePath' => '@app/themes/basic',
* 'baseUrl' => '@web/themes/basic',
* ],
* ],
* ~~~
*
* The above configuration specifies a theme located under the "themes/basic" directory of the Web folder
* that contains the entry script of the application. If your theme is designed to handle modules,
* you may configure the [[pathMap]] property like described above.
*
* @property string $basePath The root path of this theme. All resources of this theme are located under this
* directory.
* @property string $baseUrl The base URL (without ending slash) for this theme. All resources of this theme
* are considered to be under this base URL. This property is read-only.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Theme extends Component
{
/**
* @var array the mapping between view directories and their corresponding themed versions.
* This property is used by [[applyTo()]] when a view is trying to apply the theme.
* Path aliases can be used when specifying directories.
* 路径映射属性 设置替换映射关系
* If this property is empty or not set, a mapping [[Application::basePath]] to [[basePath]] will be used.
*/
public $pathMap; private $_baseUrl;//设置要访问资源的url /**
* @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
* to be under this base URL. 返回当前主题的基础链接,其他资源都在链接里
*/
public function getBaseUrl()
{
return $this->_baseUrl;
} /**
* @param $url string the base URL or path alias for this theme. All resources of this theme are considered
* to be under this base URL. 设置基础链接
*/
public function setBaseUrl($url)
{
$this->_baseUrl = rtrim(Yii::getAlias($url), '/');
} private $_basePath;//根路径 /**
* @return string the root path of this theme. All resources of this theme are located under this directory.
* 得到当前主题的根路径
* @see pathMap
*/
public function getBasePath()
{
return $this->_basePath;
} /**
* @param string $path the root path or path alias of this theme. All resources of this theme are located
* under this directory. 设置当前主题根路径
* @see pathMap
*/
public function setBasePath($path)
{
$this->_basePath = Yii::getAlias($path);
} /**
* Converts a file to a themed file if possible. 将一个文件替换主题文件
* If there is no corresponding themed file, the original file will be returned.
* 没有相应的主题文件,返回原文件。
* @param string $path the file to be themed
* @return string the themed file, or the original file if the themed version is not available.
* @throws InvalidConfigException if [[basePath]] is not set
*/
public function applyTo($path)
{
$pathMap = $this->pathMap; //取得路径映射
if (empty($pathMap)) {//没有设置值 抛出异常
if (($basePath = $this->getBasePath()) === null) {
throw new InvalidConfigException('The "basePath" property must be set.');
}
//设置值为[模块根路径=>主题根路径]的形式
$pathMap = [Yii::$app->getBasePath() => [$basePath]];
} $path = FileHelper::normalizePath($path);//对路径中的"/"."\"进行统一 foreach ($pathMap as $from => $tos) {
//映射数组中的来源
$from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
if (strpos($path, $from) === ) {//如果在$path中有可替换的旧值
$n = strlen($from);
foreach ((array) $tos as $to) {
$to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
$file = $to . substr($path, $n);//把$path中的$from替换为$to
if (is_file($file)) {
return $file; //是文件直接返回
}
}
}
} return $path;
} /**
* Converts a relative URL into an absolute URL using [[baseUrl]].
* 将一个相对URL转换为绝对URL
* @param string $url the relative URL to be converted.要转换的相对URL
* @return string the absolute URL 替换后的绝对URL
* @throws InvalidConfigException if [[baseUrl]] is not set
*/
public function getUrl($url)
{
if (($baseUrl = $this->getBaseUrl()) !== null) {//URL存在,进行转换
return $baseUrl . '/' . ltrim($url, '/');
} else {//不存在抛出异常
throw new InvalidConfigException('The "baseUrl" property must be set.');
}
} /**
* Converts a relative file path into an absolute one using [[basePath]].
* 通过相对路径生成绝对路径
* @param string $path the relative file path to be converted. 要转换的相对文件路径。
* @return string the absolute file path 转换后的绝对路径
* @throws InvalidConfigException if [[baseUrl]] is not set
*/
public function getPath($path)
{
if (($basePath = $this->getBasePath()) !== null) {
//返回拼接的路径
return $basePath . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
} else {
throw new InvalidConfigException('The "basePath" property must be set.');
}
}
}

yii2源码学习笔记(十七)的更多相关文章

  1. yii2源码学习笔记(九)

    Application是所有应用程序类的基类,接下来了解一下它的源码.yii2\base\Application.php. <?php /** * @link http://www.yiifra ...

  2. yii2源码学习笔记(八)

    Action是所有控制器的基类,接下来了解一下它的源码.yii2\base\Action.php <?php /** * @link http://www.yiiframework.com/ * ...

  3. 老刘 Yii2 源码学习笔记之 Action 类

    Action 的概述 InlineAction 就是内联动作,所谓的内联动作就是放到controller 里面的 actionXXX 这种 Action.customAction 就是独立动作,就是直 ...

  4. yii2源码学习笔记(二十)

    Widget类是所有部件的基类.yii2\base\Widget.php <?php /** * @link http://www.yiiframework.com/ * @copyright ...

  5. yii2源码学习笔记(十八)

    View继承了component,用于渲染视图文件:yii2\base\View.php <?php /** * @link http://www.yiiframework.com/ * @co ...

  6. yii2源码学习笔记(十四)

    Module类是模块和应用类的基类. yiisoft\yii2\base\Module.php <?php /** * @link http://www.yiiframework.com/ * ...

  7. yii2源码学习笔记(十三)

    模型类DynamicModel主要用于实现模型内的数据验证yii2\base\DynamicModel.php <?php /** * @link http://www.yiiframework ...

  8. yii2源码学习笔记(十一)

    Controller控制器类,是所有控制器的基类,用于调用模型和布局. <?php /** * @link http://www.yiiframework.com/ * @copyright C ...

  9. yii2源码学习笔记(六)

    Behvaior类,Behavior类是所有事件类的基类: 目录yii2\base\Behavior.php <?php /** * @link http://www.yiiframework. ...

随机推荐

  1. C# FileStream复制大文件

    即每次复制文件的一小段,以节省总内存开销.当然,本机复制也可以采用.NET内部的System.IO.File.Copy方法. 本文转载:http://www.cnblogs.com/wolf-sun/ ...

  2. “cvSnakeImage”: 找不到标识符

    1>g:\project\opencv\helloopencv\helloopencv\helloopencv.cpp(74) : error C2065: "CV_VALUE&quo ...

  3. Antelope与 Barracude MYSQL 文件格式

    作者:吴炳锡 来源:http://www.mysqlsupport.cn/ 联系方式: wubingxi#163.com 转载请注明作/译者和出处,并且不能用于商业用途,违者必究. Antelope是 ...

  4. 彻底理解android中的内部存储与外部存储

    我们先来考虑这样一个问题: 打开手机设置,选择应用管理,选择任意一个App,然后你会看到两个按钮,一个是清除缓存,另一个是清除数据,那么当我们点击清除缓存的时候清除的是哪里的数据?当我们点击清除数据的 ...

  5. iOS数据处理之SQLite数据库

    1. 数据库管理系统 1> SQL语言概述 SQL: SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集, 是一种功能齐全的 ...

  6. android activity启动的4种方式记录及打开其他应用的activity的坑

    Android启动的四种方式分别为standard,singleTop,singleTask,singleInstence. standard是最常见的activity启动方式,也是默认的启动的方式. ...

  7. openoffice转换过程中遇到繁体字文档转换失败的问题

    今天发现上线的文档转换功能中存在一个文档转换不成功,查看后台日志标志文档无法加载成功,提示日志如下: INFO: connected Jul 08, 2015 2:50:33 PM com.artof ...

  8. 微信公众号支付(三):页面调用微信支付JS并完成支付

    一.调用微信的JS文件 1.首先要绑定[JS接口安全域名],“公众号设置”的“功能设置”中 2.引入JS文件 备注:支持使用 AMD/CMD 标准模块加载方法加载 <script type=&q ...

  9. Java类加载及实例化的调用顺序

    标题起得略拗口,大概意思就是说在一个Java类中,域和构造方法的调用顺序. 1. 没有继承的情况 单独一个类的场景下,初始化顺序为依次为 静态数据,继承的基类的构造函数,成员变量,被调用的构造函数. ...

  10. JS实现跳转到页面任何地方

    要实现两个内容: 1.从A页面跳转到B页面任何地方 方法:用id对要跳转的地方进行标记. 首先,在A页面可以设一个链接 <a href = "b.html#pos" targ ...