Theme 类,即一个应用的主题,主要通过替换路径实现主题的应用,里边的方法为获取根路径和根链接,以及应用主题的方法:

 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]]渲染视图文件的时候,会检查视图的主题[[View::theme|active theme]]是否存在,如果存在则渲染主题取代默认样式
  *
  * 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:
  * Theme 通过[[pathMap]]来实现视图文件替换:
  *
  * 1. It first looks for a key in [[pathMap]] that is a substring of the given view file path;
  *    首先在[[pathMap]] 中查找关键字,该关键字是一个给定的视图路径的子字符串
  * 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,
  *
  * ```php
  * '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:
  *
  * ```php
  * '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.
  *
  * @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;
     /**
      * @var 设置要访问的资源的url,没有结尾的斜线(without ending slash)
      */
     private $_baseUrl;

     /**
      * @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
      * to be under this base URL.
      * @return string 返回值为当前主题的基础链接,即访问链接,其他的资源都在该链接下
      */
     public function getBaseUrl()
     {
         return $this->_baseUrl;
     }

     /**
      * @param string $url 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), '/');
     }
     /**
      * @var array 当前主题的根路径,该主题的资源都在该目录下
      */
     private $_basePath;

     /**
      * @return string the root path of this theme. All resources of this theme are located under this directory.
      * @return string 返回当前主题的根路径,该主题的资源都在该目录下
      * @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;//取得路径映射[[pathMap]]的值
         if (empty($pathMap)) {//如果[[pathMap]]没有设置值,则取当前主题的根路径,否则抛出异常
             if (($basePath = $this->getBasePath()) === null) {
                 throw new InvalidConfigException('The "basePath" property must be set.');
             }
             $pathMap = [Yii::$app->getBasePath() => [$basePath]];//设置$pathMap的值为[模块根路径=>主题根路径]的形式
         }
         //对路径中的"/"、“\”进行统一替换
         $path = FileHelper::normalizePath($path);

         foreach ($pathMap as $from => $tos) {
              //映射数组中的来源(旧值)
             $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
             //如果在$path中有可替换的旧值
             if (strpos($path, $from) === 0) {
                 $n = strlen($from);
                 //对目标值循环
                 foreach ((array) $tos as $to) {
                     $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
                      //把$path中的$from替换为$to
                     $file = $to . substr($path, $n);
                      //如果是文件,直接返回
                     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.
      * @return string the absolute URL
      * @throws InvalidConfigException if [[baseUrl]] is not set
      */
     public function getUrl($url)
     {
         if (($baseUrl = $this->getBaseUrl()) !== null) {
             return $baseUrl . '/' . ltrim($url, '/');//通过取得[[baseUrl]]的值拼接出当前主题的绝对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 [[basePath]] is not set
      */
     public function getPath($path)
     {
         if (($basePath = $this->getBasePath()) !== null) {
             return $basePath . DIRECTORY_SEPARATOR . ltrim($path, '/\\');//通过取得[[basePath]]的值拼接出当前主题的绝对路径
         } else {
             throw new InvalidConfigException('The "basePath" property must be set.');
         }
     }
 }

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

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

    View.php,继承了component,用于渲染视图文件: namespace yii\base; use Yii; use yii\helpers\FileHelper; use yii\wid ...

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

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

  3. werkzeug源码阅读笔记(二) 下

    wsgi.py----第二部分 pop_path_info()函数 先测试一下这个函数的作用: >>> from werkzeug.wsgi import pop_path_info ...

  4. werkzeug源码阅读笔记(二) 上

    因为第一部分是关于初始化的部分的,我就没有发布出来~ wsgi.py----第一部分 在分析这个模块之前, 需要了解一下WSGI, 大致了解了之后再继续~ get_current_url()函数 很明 ...

  5. Detectron2源码阅读笔记-(二)Registry&build_*方法

    ​ Trainer解析 我们继续Detectron2代码阅读笔记-(一)中的内容. 上图画出了detectron2文件夹中的三个子文件夹(tools,config,engine)之间的关系.那么剩下的 ...

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

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

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

    web/Application类的注释,继承base/Application类,针对web应用的一些处理: namespace yii\web; use Yii; use yii\base\Inval ...

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

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

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

    Yii/web中的Controller类,实现参数绑定,启动csrf验证功能,重定向页面功能: namespace yii\web; use Yii; use yii\base\InlineActio ...

随机推荐

  1. TStringList 常用操作

    //TStringList 常用方法与属性: var   List: TStringList;   i: Integer; begin   List := TStringList.Create;   ...

  2. MFC 重载退出(窗口顶上最右边的x按钮)

    其实可以在*Dlg.cpp中的BEGIN_MESSAGE_MAP中对IDCANCEL和自定义函数进行匹配就可以了. 如: 自定义的退出函数是OnClose(),则在BEGIN_MESSAGE_MAP中 ...

  3. address元素

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  5. iOS学习26之UINavigationController

    1. UINavigationController 1> 概述 UINavigationController : 导航控制器, 是 iOS 中最常用的多视图控制器之一, 用它来管理多个视图控制器 ...

  6. BZOJ4435 : [Cerc2015]Juice Junctions

    最大流=最小割,而因为本题点的度数不超过3,所以最小割不超过3,EK算法的复杂度为$O(n+m)$. 通过分治求出最小割树,设$f[i][j][k]$表示最小割为$i$时,$j$点在第$k$次分治过程 ...

  7. 【转】Android之自定义Adapter的ListView

    http://www.cnblogs.com/topcoderliu/archive/2011/05/07/2039862.html 在开发中,我们经常使用到ListView这个控件.Android的 ...

  8. [知识点]计算几何I——基础知识与多边形面积

    // 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...

  9. jQuery取得select选中的值

    $("#sxselect").change(function(){ alert($("#sxselect option:selected").val()); } ...

  10. 运行java的class文件方法详解

    一.运行class文件 执行带main方法的class文件,命令行为:java <CLASS文件名>注意:CLASS文件名不要带文件后缀.class 例如: 复制代码代码如下: java ...