Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Theme.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. namespace yii\base;
  8. use Yii;
  9. use yii\helpers\FileHelper;
  10. /**
  11. * Theme represents an application theme.
  12. *
  13. * When [[View]] renders a view file, it will check the [[View::theme|active theme]]
  14. * to see if there is a themed version of the view file exists. If so, the themed version will be rendered instead.
  15. *
  16. * A theme is a directory consisting of view files which are meant to replace their non-themed counterparts.
  17. *
  18. * Theme uses [[pathMap]] to achieve the view file replacement:
  19. *
  20. * 1. It first looks for a key in [[pathMap]] that is a substring of the given view file path;
  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. * 3. It will then check if the updated view file exists or not. If so, that file will be used
  24. * to replace the original view file.
  25. * 4. If Step 2 or 3 fails, the original view file will be used.
  26. *
  27. * For example, if [[pathMap]] is `['@app/views' => '@app/themes/basic']`,
  28. * then the themed version for a view file `@app/views/site/index.php` will be
  29. * `@app/themes/basic/site/index.php`.
  30. *
  31. * It is possible to map a single path to multiple paths. For example,
  32. *
  33. * ```php
  34. * 'pathMap' => [
  35. * '@app/views' => [
  36. * '@app/themes/christmas',
  37. * '@app/themes/basic',
  38. * ],
  39. * ]
  40. * ```
  41. *
  42. * In this case, the themed version could be either `@app/themes/christmas/site/index.php` or
  43. * `@app/themes/basic/site/index.php`. The former has precedence over the latter if both files exist.
  44. *
  45. * To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
  46. * component like the following:
  47. *
  48. * ```php
  49. * 'view' => [
  50. * 'theme' => [
  51. * 'basePath' => '@app/themes/basic',
  52. * 'baseUrl' => '@web/themes/basic',
  53. * ],
  54. * ],
  55. * ```
  56. *
  57. * The above configuration specifies a theme located under the "themes/basic" directory of the Web folder
  58. * that contains the entry script of the application. If your theme is designed to handle modules,
  59. * you may configure the [[pathMap]] property like described above.
  60. *
  61. * @property string $basePath The root path of this theme. All resources of this theme are located under this
  62. * directory.
  63. * @property string $baseUrl The base URL (without ending slash) for this theme. All resources of this theme
  64. * are considered to be under this base URL.
  65. *
  66. * @author Qiang Xue <qiang.xue@gmail.com>
  67. * @since 2.0
  68. */
  69. class Theme extends Component
  70. {
  71. /**
  72. * @var array the mapping between view directories and their corresponding themed versions.
  73. * This property is used by [[applyTo()]] when a view is trying to apply the theme.
  74. * Path aliases can be used when specifying directories.
  75. * If this property is empty or not set, a mapping [[Application::basePath]] to [[basePath]] will be used.
  76. */
  77. public $pathMap;
  78. private $_baseUrl;
  79. /**
  80. * @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
  81. * to be under this base URL.
  82. */
  83. public function getBaseUrl()
  84. {
  85. return $this->_baseUrl;
  86. }
  87. /**
  88. * @param string $url the base URL or path alias for this theme. All resources of this theme are considered
  89. * to be under this base URL.
  90. */
  91. public function setBaseUrl($url)
  92. {
  93. $this->_baseUrl = rtrim(Yii::getAlias($url), '/');
  94. }
  95. private $_basePath;
  96. /**
  97. * @return string the root path of this theme. All resources of this theme are located under this directory.
  98. * @see pathMap
  99. */
  100. public function getBasePath()
  101. {
  102. return $this->_basePath;
  103. }
  104. /**
  105. * @param string $path the root path or path alias of this theme. All resources of this theme are located
  106. * under this directory.
  107. * @see pathMap
  108. */
  109. public function setBasePath($path)
  110. {
  111. $this->_basePath = Yii::getAlias($path);
  112. }
  113. /**
  114. * Converts a file to a themed file if possible.
  115. * If there is no corresponding themed file, the original file will be returned.
  116. * @param string $path the file to be themed
  117. * @return string the themed file, or the original file if the themed version is not available.
  118. * @throws InvalidConfigException if [[basePath]] is not set
  119. */
  120. public function applyTo($path)
  121. {
  122. $pathMap = $this->pathMap;
  123. if (empty($pathMap)) {
  124. if (($basePath = $this->getBasePath()) === null) {
  125. throw new InvalidConfigException('The "basePath" property must be set.');
  126. }
  127. $pathMap = [Yii::$app->getBasePath() => [$basePath]];
  128. }
  129. $path = FileHelper::normalizePath($path);
  130. foreach ($pathMap as $from => $tos) {
  131. $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
  132. if (strpos($path, $from) === 0) {
  133. $n = strlen($from);
  134. foreach ((array) $tos as $to) {
  135. $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
  136. $file = $to . substr($path, $n);
  137. if (is_file($file)) {
  138. return $file;
  139. }
  140. }
  141. }
  142. }
  143. return $path;
  144. }
  145. /**
  146. * Converts a relative URL into an absolute URL using [[baseUrl]].
  147. * @param string $url the relative URL to be converted.
  148. * @return string the absolute URL
  149. * @throws InvalidConfigException if [[baseUrl]] is not set
  150. */
  151. public function getUrl($url)
  152. {
  153. if (($baseUrl = $this->getBaseUrl()) !== null) {
  154. return $baseUrl . '/' . ltrim($url, '/');
  155. } else {
  156. throw new InvalidConfigException('The "baseUrl" property must be set.');
  157. }
  158. }
  159. /**
  160. * Converts a relative file path into an absolute one using [[basePath]].
  161. * @param string $path the relative file path to be converted.
  162. * @return string the absolute file path
  163. * @throws InvalidConfigException if [[baseUrl]] is not set
  164. */
  165. public function getPath($path)
  166. {
  167. if (($basePath = $this->getBasePath()) !== null) {
  168. return $basePath . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
  169. } else {
  170. throw new InvalidConfigException('The "basePath" property must be set.');
  171. }
  172. }
  173. }