yii2源碼學習筆記(十七),yii2源碼學習筆記_PHP教程

來源:互聯網
上載者:User

yii2源碼學習筆記(十七),yii2源碼學習筆記


Theme 類,應用的主題,通過替換路徑實現主題的應用,方法為擷取根路徑和根連結:yii2\base\Theme.php

  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\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  */ 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      * 路徑映射屬性 設定替換映射關係 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;//設定要訪問資源的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      */ 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. 設定基礎連結 98      */ 99     public function setBaseUrl($url)100     {101         $this->_baseUrl = rtrim(Yii::getAlias($url), '/');102     }103 104     private $_basePath;//根路徑105 106     /**107      * @return string the root path of this theme. All resources of this theme are located under this directory.108      * 得到當前主題的根路徑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. 設定當前主題根路徑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. 將一個檔案替換主題檔案128      * If there is no corresponding themed file, the original file will be returned.129      * 沒有相應的主題檔案,返回原檔案。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; //取得路徑映射137         if (empty($pathMap)) {//沒有設定值 拋出異常138             if (($basePath = $this->getBasePath()) === null) {139                 throw new InvalidConfigException('The "basePath" property must be set.');140             }141             //設定值為[模組根路徑=>主題根路徑]的形式142             $pathMap = [Yii::$app->getBasePath() => [$basePath]];143         }144 145         $path = FileHelper::normalizePath($path);//對路徑中的"/"."\"進行統一146 147         foreach ($pathMap as $from => $tos) {148             //映射數組中的來源149             $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;150             if (strpos($path, $from) === 0) {//如果在$path中有可替換的舊值151                 $n = strlen($from);152                 foreach ((array) $tos as $to) {153                     $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;154                     $file = $to . substr($path, $n);//把$path中的$from替換為$to155                     if (is_file($file)) {156                         return $file; //是檔案直接返回157                     }158                 }159             }160         }161 162         return $path;163     }164 165     /**166      * Converts a relative URL into an absolute URL using [[baseUrl]].167      * 將一個相對URL轉換為絕對URL168      * @param string $url the relative URL to be converted.要轉換的相對URL169      * @return string the absolute URL  替換後的絕對URL170      * @throws InvalidConfigException if [[baseUrl]] is not set171      */172     public function getUrl($url)173     {174         if (($baseUrl = $this->getBaseUrl()) !== null) {//URL存在,進行轉換175             return $baseUrl . '/' . ltrim($url, '/');176         } else {//不存在拋出異常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      * 通過相對路徑產生絕對路徑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 set187      */188     public function getPath($path)189     {190         if (($basePath = $this->getBasePath()) !== null) {191             //返回拼接的路徑192             return $basePath . DIRECTORY_SEPARATOR . ltrim($path, '/\\');193         } else {194             throw new InvalidConfigException('The "basePath" property must be set.');195         }196     }197 }

http://www.bkjia.com/PHPjc/1134780.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1134780.htmlTechArticleyii2源碼學習筆記(十七),yii2源碼學習筆記 Theme 類,應用的主題,通過替換路徑實現主題的應用,方法為擷取根路徑和根連結:yii2\base\T...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.