Yii源码阅读笔记(二十七)

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

  1 namespace yii\base;
  2
  3 use Yii;
  4 use yii\helpers\FileHelper;
  5
  6 /**
  7  * Theme represents an application theme.
  8  * Theme 类,即一个应用的主题
  9  *
 10  * When [[View]] renders a view file, it will check the [[View::theme|active theme]]
 11  * to see if there is a themed version of the view file exists. If so, the themed version will be rendered instead.
 12  * 视图对象[[View]]渲染视图文件的时候,会检查视图的主题[[View::theme|active theme]]是否存在,如果存在则渲染主题取代默认样式
 13  *
 14  * A theme is a directory consisting of view files which are meant to replace their non-themed counterparts.
 15  *
 16  * Theme uses [[pathMap]] to achieve the view file replacement:
 17  * Theme 通过[[pathMap]]来实现视图文件替换:
 18  *
 19  * 1. It first looks for a key in [[pathMap]] that is a substring of the given view file path;
 20  *    首先在[[pathMap]] 中查找关键字,该关键字是一个给定的视图路径的子字符串
 21  * 2. If such a key exists, the corresponding value will be used to replace the corresponding part
 22  *    in the view file path;
 23  *    如果该关键字存在,则用对应得值替换给定的视图文件路径中对应的部分
 24  * 3. It will then check if the updated view file exists or not. If so, that file will be used
 25  *    to replace the original view file.
 26  *    然后检查替换后的路径对应的文件是否存在,如果存在就替换原来的文件
 27  * 4. If Step 2 or 3 fails, the original view file will be used.
 28  *    如果2和3失败的话,就返回原来的路径
 29  *
 30  * For example, if [[pathMap]] is `[‘@app/views‘ => ‘@app/themes/basic‘]`,
 31  * then the themed version for a view file `@app/views/site/index.php` will be
 32  * `@app/themes/basic/site/index.php`.
 33  *
 34  * It is possible to map a single path to multiple paths. For example,
 35  *
 36  * ```php
 37  * ‘pathMap‘ => [
 38  *     ‘@app/views‘ => [
 39  *         ‘@app/themes/christmas‘,
 40  *         ‘@app/themes/basic‘,
 41  *     ],
 42  * ]
 43  * ```
 44  *
 45  * In this case, the themed version could be either `@app/themes/christmas/site/index.php` or
 46  * `@app/themes/basic/site/index.php`. The former has precedence over the latter if both files exist.
 47  *  主题的对应关系可以是一个视图文件对应多个主题文件
 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  * ```php
 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.
 69  *
 70  * @author Qiang Xue <[email protected]>
 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      * If this property is empty or not set, a mapping [[Application::basePath]] to [[basePath]] will be used.
 80      * 路径映射属性--用来设置替换映射关系的
 81      */
 82     public $pathMap;
 83     /**
 84      * @var 设置要访问的资源的url,没有结尾的斜线(without ending slash)
 85      */
 86     private $_baseUrl;
 87
 88
 89     /**
 90      * @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
 91      * to be under this base URL.
 92      * @return string 返回值为当前主题的基础链接,即访问链接,其他的资源都在该链接下
 93      */
 94     public function getBaseUrl()
 95     {
 96         return $this->_baseUrl;
 97     }
 98
 99     /**
100      * @param string $url the base URL or path alias for this theme. All resources of this theme are considered
101      * to be under this base URL.
102      * 设置基础访问链接
103      */
104     public function setBaseUrl($url)
105     {
106         $this->_baseUrl = rtrim(Yii::getAlias($url), ‘/‘);
107     }
108     /**
109      * @var array 当前主题的根路径,该主题的资源都在该目录下
110      */
111     private $_basePath;
112
113     /**
114      * @return string the root path of this theme. All resources of this theme are located under this directory.
115      * @return string 返回当前主题的根路径,该主题的资源都在该目录下
116      * @see pathMap
117      */
118     public function getBasePath()
119     {
120         return $this->_basePath;
121     }
122
123     /**
124      * @param string $path the root path or path alias of this theme. All resources of this theme are located
125      * under this directory.
126      * 设置当前主题的根路径
127      * @see pathMap
128      */
129     public function setBasePath($path)
130     {
131         $this->_basePath = Yii::getAlias($path);
132     }
133
134     /**
135      * Converts a file to a themed file if possible.
136      * 将一个文件替换为允许替换的主题文件---主题的应用原理
137      * If there is no corresponding themed file, the original file will be returned.
138      * 如果没有对应的主题文件,将返回源文件
139      * @param string $path the file to be themed
140      * @return string the themed file, or the original file if the themed version is not available.
141      * @throws InvalidConfigException if [[basePath]] is not set
142      */
143     public function applyTo($path)
144     {
145         $pathMap = $this->pathMap;//取得路径映射[[pathMap]]的值
146         if (empty($pathMap)) {//如果[[pathMap]]没有设置值,则取当前主题的根路径,否则抛出异常
147             if (($basePath = $this->getBasePath()) === null) {
148                 throw new InvalidConfigException(‘The "basePath" property must be set.‘);
149             }
150             $pathMap = [Yii::$app->getBasePath() => [$basePath]];//设置$pathMap的值为[模块根路径=>主题根路径]的形式
151         }
152         //对路径中的"/"、“\”进行统一替换
153         $path = FileHelper::normalizePath($path);
154
155         foreach ($pathMap as $from => $tos) {
156              //映射数组中的来源(旧值)
157             $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
158             //如果在$path中有可替换的旧值
159             if (strpos($path, $from) === 0) {
160                 $n = strlen($from);
161                 //对目标值循环
162                 foreach ((array) $tos as $to) {
163                     $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
164                      //把$path中的$from替换为$to
165                     $file = $to . substr($path, $n);
166                      //如果是文件,直接返回
167                     if (is_file($file)) {
168                         return $file;
169                     }
170                 }
171             }
172         }
173
174         return $path;
175     }
176
177     /**
178      * Converts a relative URL into an absolute URL using [[baseUrl]].
179      * 将一个相对URL替换为绝对URL
180      * @param string $url the relative URL to be converted.
181      * @return string the absolute URL
182      * @throws InvalidConfigException if [[baseUrl]] is not set
183      */
184     public function getUrl($url)
185     {
186         if (($baseUrl = $this->getBaseUrl()) !== null) {
187             return $baseUrl . ‘/‘ . ltrim($url, ‘/‘);//通过取得[[baseUrl]]的值拼接出当前主题的绝对URL
188         } else {
189             throw new InvalidConfigException(‘The "baseUrl" property must be set.‘);
190         }
191     }
192
193     /**
194      * Converts a relative file path into an absolute one using [[basePath]].
195      * 通过相对路径生成绝对路径
196      * @param string $path the relative file path to be converted.
197      * @return string the absolute file path
198      * @throws InvalidConfigException if [[basePath]] is not set
199      */
200     public function getPath($path)
201     {
202         if (($basePath = $this->getBasePath()) !== null) {
203             return $basePath . DIRECTORY_SEPARATOR . ltrim($path, ‘/\\‘);//通过取得[[basePath]]的值拼接出当前主题的绝对路径
204         } else {
205             throw new InvalidConfigException(‘The "basePath" property must be set.‘);
206         }
207     }
208 }
时间: 2024-10-12 17:10:56

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

Yii源码阅读笔记(十七)

View.php,继承了component,用于渲染视图文件: 1 namespace yii\base; 2 3 use Yii; 4 use yii\helpers\FileHelper; 5 use yii\widgets\Block; 6 use yii\widgets\ContentDecorator; 7 use yii\widgets\FragmentCache; 8 9 /** 10 * View represents a view object in the MVC patte

Yii源码阅读笔记 - 日志组件

?使用 Yii框架为开发者提供两个静态方法进行日志记录: Yii::log($message, $level, $category);Yii::trace($message, $category); 两者的区别在于后者依赖于应用开启调试模式,即定义常量YII_DEBUG: defined('YII_DEBUG') or define('YII_DEBUG', true); Yii::log方法的调用需要指定message的level和category.category是格式为“xxx.yyy.z

Java Jdk1.8 HashMap源码阅读笔记二

三.源码阅读 3.元素包含containsKey(Object key) /** * Returns <tt>true</tt> if this map contains a mapping for the * specified key. * * @param key The key whose presence in this map is to be tested * @return <tt>true</tt> if this map contains

Yii源码阅读笔记(一)

今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于web目录的index.php,用于启动web应用和配置一些路径参数. index.php—— 1 // comment out the following two lines when deployed to production 2 defined('YII_DEBUG') or define('Y

Yii源码阅读笔记(二)

接下来阅读BaseYii.php vendor/yiisoft/yii2/Yii.php—— 1 namespace yii; 2 3 use yii\base\InvalidConfigException; 4 use yii\base\InvalidParamException; 5 use yii\base\UnknownClassException; 6 use yii\log\Logger; 7 use yii\di\Container; 第1行定义命名空间为yii: 第3到7行使用了

Yii源码阅读笔记(二十一)——请求处理流程

Yii2请求处理流程: 首先:项目路径/web/index.php (new yii\web\Application($config))->run();//根据配置文件创建App实例,先实例化yii\web\Application(),然后调用run()方法 该语句可分为两步: $application = new yii\web\Application($config);//实例化app $application->run();//调用run()方法 $config 为配置文件,通过 req

Yii源码阅读笔记(二十六)

Application 类中设置路径的方法和调用ServiceLocator(服务定位器)加载运行时的组件的方法注释: 1 /** 2 * Handles the specified request. 3 * 处理指定的请求--抽象方法 4 * This method should return an instance of [[Response]] or its child class 5 * which represents the handling result of the reques

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

web/Application类的注释,继承base/Application类,针对web应用的一些处理: 1 namespace yii\web; 2 3 use Yii; 4 use yii\base\InvalidRouteException; 5 6 /** 7 * Application is the base class for all web application classes. 8 * Application 是所有web应用的基类 9 * 10 * @property st

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

Module类,属性的注释和构造函数的注释: 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\di\ServiceLocator;