Yii2 source code learning notes (17th) and yii2 source code learning notes

Source: Internet
Author: User

Yii2 source code learning notes (17th) and yii2 source code learning notes

Theme class: the topic of the application. You can use the replacement path to get the root path and root link: yii2 \ base \ Theme. php.

1 <? Php 2/*** @ 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 \ helpers \ FileHelper; 12 13/** 14 * Theme represents an application theme. 15 * Theme class, application topic 16 * When [[View] renders a view file, it will check the [[View :: theme | active theme] 17 * to see if th Ere is a themed version of the view file exists. if so, the themed version will be rendered instead. 18 * when rendering a View object [[View], the system checks whether the View topic exists, if so, the rendering topic replaces the default style 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 * First, search for the keyword. the keyword is the string 25*2 of a given view path. if such a key exists, the corresponding value will be used to replace the corresponding part 26 * in the view file path; the keyword exists, replace the corresponding part of the given view file path with the corresponding value 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. check whether the file corresponding to the replaced path exists. If yes, replace the original file 29*4. If Step 2 or 3 fails, the original view file will be used. if 30*2 and 3 fail, the original path 31 * For example is returned, if [[pathMap] is '[' @ app/view' => '@ 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/view' => [40 *' @ app/themes/christmas ', 41 * '@ app/themes/basic', 42 *], 43 *] 44 *~~~ 45*46 * In this case, the themed version cocould 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 shoshould 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 <qiang.xue@gmail.com> 71 * @ since 2.0 72 */73 class Theme extends Component 74 {75/** 76 * @ var array the mapping between view directories and their corresponding Med 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 * replacing the 80 ing relationship with path ing property settings 80 * If this property is empty or not set, a mapping [[Application: basePath] to [[basePath] will be used. 81 */82 public $ pathMap; 83 84 private $ _ baseUrl; // set the url 85 86/** 87 * @ return string the base URL (wit Hout ending slash) for this theme. all resources of this theme are considered 88 * to be under this base URL. returns the base link of the current topic. Other resources are in the Link. 89 */90 public function getBaseUrl () 91 {92 return $ 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. set the basic link 98 */99 public function setBaseUr L ($ url) 100 {101 $ this-> _ baseUrl = rtrim (Yii: getAlias ($ url), '/'); 102} 103 104 private $ _ basePath; // root path 105 106/** 107 * @ return string the root path of this theme. all resources of this theme are located under this directory.108 * Get the root path of the current topic 109 * @ see pathMap110 */111 public function getBasePath () 112 {113 return $ this-> _ basePath; 114} 115 116/** 117 * @ param string $ path the root path or path alias Of this theme. all resources of this theme are located118 * under this directory. set the root path of the current topic to 119 * @ see pathMap120 */121 public function setBasePath ($ path) 122 {123 $ this-> _ basePath = Yii: getAlias ($ path ); 124} 125 126/** 127 * Converts a file to a themed file if possible. replace the topic file 128 * If there is no corresponding themed file, the original file will be returned.129 * with no corresponding topic file, and return to the original file. 130 * @ param string $ path the file to be themed131 * @ return string the themed file, or the original file if the themed version is not available.132 * @ throws InvalidConfigException if [[basePath] is not set133 */134 public function applyTo ($ path) 135 {136 $ pathMap = $ this-> pathMap; // obtain the path ing 137 if (empty ($ pathMap )) {// if no value is set, an exception is thrown. 138 if ($ basePath = $ this-> getBasePath () = null) {139 throw new Inv AlidConfigException (The "basePath" property must be set. '); 140} 141 // The value is in the form of [module root path => topic root path]. 142 $ pathMap = [Yii: $ app-> getBasePath () => [$ basePath]; 143} 144 $ path = FileHelper: normalizePath ($ path "/". "\" for unified 146 147 foreach ($ pathMap as $ from =>$ tos) {148 // source 149 $ from = FileHelper: normalizePath (Yii :: getAlias ($ from )). DIRECTORY_SEPARATOR; 150 if (strpos ($ path, $ from) === 0 ){/ /If there are replaceable old values in $ path 151 $ n = strlen ($ from); 152 foreach (array) $ tos as $ to) {153 $ to = FileHelper:: normalizePath (Yii: getAlias ($ )). DIRECTORY_SEPARATOR; 154 $ file = $. substr ($ path, $ n); // Replace $ from in $ path with $ to155 if (is_file ($ file) {156 return $ file; // The file directly returns 157} 158} 159} 160} 161 162 return $ path; 163} 164 165/** 166 * Converts a relative URL into an absolute URL using [[baseUrl]. 167 * convert a relative U RL to absolute URL168 * @ param string $ url the relative URL to be converted. the relative URL169 * @ return string the absolute URL to be converted replaces the absolute URL170 * @ throws InvalidConfigException if [[baseUrl] is not set171 */172 public function getUrl ($ url) 173 {174 if ($ baseUrl = $ this-> getBaseUrl ())! = Null) {// The URL exists and is converted to 175 return $ baseUrl. '/'. ltrim ($ url, '/'); 176} else {// no exception thrown 177 throw new InvalidConfigException ('the "baseUrl" property must be set. '); 178} 179} 180 181/** 182 * Converts a relative file path into an absolute one using [[basePath]. 183 * generate absolute path 184 * @ param string $ path the relative file path to be converted. the relative file path to be converted. 185 * @ return string the absolute file path absolute path after conversion 186 * @ throws InvalidConfigException if [[baseUrl] is not set187 */188 public function getPath ($ path) 189 {190 if ($ basePath = $ this-> getBasePath ())! = Null) {191 // return the spliced path 192 return $ basePath. DIRECTORY_SEPARATOR. ltrim ($ path, '/\'); 193} else {194 throw new InvalidConfigException (The "basePath" property must be set. '); 195} 196} 197}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.