0

0

android源码学习 yii2源码学习笔记十七)

php中文网

php中文网

发布时间:2016-07-28 08:30:03

|

1140人浏览过

|

来源于php中文网

原创

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

企站帮企业网站管理系统1.0
企站帮企业网站管理系统1.0

一、源码描述这是一款比较简单的企业管理系统源码,界面美观大方,功能简单,特别适合初学者学习研究,系统运行十分流畅,可以作为二次开发,同时也是可以帮助初学者增长知识的优秀代码。二、功能介绍主要功能:企业动态,产品介绍 ,免费下载,定制服务,该源码比较适合新手学习和二次开发使用。三、源码特点1、网站布局:采用目前最先进的布局方式DIV+CSS,符合W3C的标准和Web2.0的风格。2、程序设计模块化,

下载

  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  8namespace yii\base;
  9 10use Yii;
 11use 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*/ 73class 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*/ 82public $pathMap;
 83 84private $_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*/ 90public function getBaseUrl()
 91    {
 92return $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*/ 99public function setBaseUrl($url)
100    {
101         $this->_baseUrl = rtrim(Yii::getAlias($url), '/');
102    }
103104private $_basePath;//根路径105106/**
107     * @return string the root path of this theme. All resources of this theme are located under this directory.
108     * 得到当前主题的根路径
109     * @see pathMap
110*/111public function getBasePath()
112    {
113return $this->_basePath;
114    }
115116/**
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*/121public function setBasePath($path)
122    {
123         $this->_basePath = Yii::getAlias($path);
124124    }
125126/**
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*/134public function applyTo($path)
135    {
136         $pathMap = $this->pathMap; //取得路径映射137if (empty($pathMap)) {//没有设置值 抛出异常138if (($basePath = $this->getBasePath()) === null) {
139thrownew InvalidConfigException('The "basePath" property must be set.');
140            }
141//设置值为[模块根路径=>主题根路径]的形式142             $pathMap = [Yii::$app->getBasePath() => [$basePath]];
143        }
144145         $path = FileHelper::normalizePath($path);//对路径中的"/"."\"进行统一146147foreach ($pathMap as $from => $tos) {
148//映射数组中的来源149             $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
150if (strpos($path, $from) === 0) {//如果在$path中有可替换的旧值151                 $n = strlen($from);
152foreach ((array) $tos as $to) {
153                     $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
154                     $file = $to . substr($path, $n);//把$path中的$from替换为$to155if (is_file($file)) {
156return $file; //是文件直接返回157                    }
158                }
159            }
160        }
161162return $path;
163    }
164165/**
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*/172public function getUrl($url)
173    {
174if (($baseUrl = $this->getBaseUrl()) !== null) {//URL存在,进行转换175return $baseUrl . '/' . ltrim($url, '/');
176         } else {//不存在抛出异常177thrownew InvalidConfigException('The "baseUrl" property must be set.');
178        }
179    }
180181/**
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*/188public function getPath($path)
189    {
190if (($basePath = $this->getBasePath()) !== null) {
191//返回拼接的路径192return $basePath . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
193         } else {
194thrownew InvalidConfigException('The "basePath" property must be set.');
195        }
196    }
197 }

以上就介绍了android源码学习 yii2源码学习笔记十七),包括了android源码学习方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关专题

更多
Word 字间距调整方法汇总
Word 字间距调整方法汇总

本专题整合了Word字间距调整方法,阅读下面的文章了解更详细操作。

2

2025.12.24

任务管理器教程
任务管理器教程

本专题整合了任务管理器相关教程,阅读下面的文章了解更多详细操作。

2

2025.12.24

AppleID格式
AppleID格式

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

0

2025.12.24

csgo视频观看入口合集
csgo视频观看入口合集

本专题整合了csgo观看入口合集,阅读下面的文章了知道更多入口地址。

29

2025.12.24

yandex外贸入口合集
yandex外贸入口合集

本专题汇总了yandex外贸入口地址,阅读下面的文章了解更多内容。

58

2025.12.24

添加脚注通用方法
添加脚注通用方法

本专题整合了添加脚注方法合集,阅读专题下面的文章了解更多内容。

1

2025.12.24

重启电脑教程汇总
重启电脑教程汇总

本专题整合了重启电脑操作教程,阅读下面的文章了解更多详细教程。

3

2025.12.24

纸张尺寸汇总
纸张尺寸汇总

本专题整合了纸张尺寸相关内容,阅读专题下面的文章了解更多内容。

5

2025.12.24

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

1

2025.12.24

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.8万人学习

ECMAScript6 / ES6---十天技能课堂
ECMAScript6 / ES6---十天技能课堂

共25课时 | 1.8万人学习

php-src源码分析探索
php-src源码分析探索

共6课时 | 0.5万人学习

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

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