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