Theme.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. * For more details and usage information on Theme, see the [guide article on theming](guide:output-theming).
  62. *
  63. * @property string $basePath The root path of this theme. All resources of this theme are located under this
  64. * directory.
  65. * @property string $baseUrl The base URL (without ending slash) for this theme. All resources of this theme
  66. * are considered to be under this base URL.
  67. *
  68. * @author Qiang Xue <qiang.xue@gmail.com>
  69. * @since 2.0
  70. */
  71. class Theme extends Component
  72. {
  73. /**
  74. * @var array the mapping between view directories and their corresponding themed versions.
  75. * This property is used by [[applyTo()]] when a view is trying to apply the theme.
  76. * [Path aliases](guide:concept-aliases) can be used when specifying directories.
  77. * If this property is empty or not set, a mapping [[Application::basePath]] to [[basePath]] will be used.
  78. */
  79. public $pathMap;
  80. private $_baseUrl;
  81. /**
  82. * @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
  83. * to be under this base URL.
  84. */
  85. public function getBaseUrl()
  86. {
  87. return $this->_baseUrl;
  88. }
  89. /**
  90. * @param string $url the base URL or [path alias](guide:concept-aliases) for this theme. All resources of this theme are considered
  91. * to be under this base URL.
  92. */
  93. public function setBaseUrl($url)
  94. {
  95. $this->_baseUrl = $url === null ? null : rtrim(Yii::getAlias($url), '/');
  96. }
  97. private $_basePath;
  98. /**
  99. * @return string the root path of this theme. All resources of this theme are located under this directory.
  100. * @see pathMap
  101. */
  102. public function getBasePath()
  103. {
  104. return $this->_basePath;
  105. }
  106. /**
  107. * @param string $path the root path or [path alias](guide:concept-aliases) of this theme. All resources of this theme are located
  108. * under this directory.
  109. * @see pathMap
  110. */
  111. public function setBasePath($path)
  112. {
  113. $this->_basePath = Yii::getAlias($path);
  114. }
  115. /**
  116. * Converts a file to a themed file if possible.
  117. * If there is no corresponding themed file, the original file will be returned.
  118. * @param string $path the file to be themed
  119. * @return string the themed file, or the original file if the themed version is not available.
  120. * @throws InvalidConfigException if [[basePath]] is not set
  121. */
  122. public function applyTo($path)
  123. {
  124. $pathMap = $this->pathMap;
  125. if (empty($pathMap)) {
  126. if (($basePath = $this->getBasePath()) === null) {
  127. throw new InvalidConfigException('The "basePath" property must be set.');
  128. }
  129. $pathMap = [Yii::$app->getBasePath() => [$basePath]];
  130. }
  131. $path = FileHelper::normalizePath($path);
  132. foreach ($pathMap as $from => $tos) {
  133. $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
  134. if (strpos($path, $from) === 0) {
  135. $n = strlen($from);
  136. foreach ((array) $tos as $to) {
  137. $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
  138. $file = $to . substr($path, $n);
  139. if (is_file($file)) {
  140. return $file;
  141. }
  142. }
  143. }
  144. }
  145. return $path;
  146. }
  147. /**
  148. * Converts a relative URL into an absolute URL using [[baseUrl]].
  149. * @param string $url the relative URL to be converted.
  150. * @return string the absolute URL
  151. * @throws InvalidConfigException if [[baseUrl]] is not set
  152. */
  153. public function getUrl($url)
  154. {
  155. if (($baseUrl = $this->getBaseUrl()) !== null) {
  156. return $baseUrl . '/' . ltrim($url, '/');
  157. }
  158. throw new InvalidConfigException('The "baseUrl" property must be set.');
  159. }
  160. /**
  161. * Converts a relative file path into an absolute one using [[basePath]].
  162. * @param string $path the relative file path to be converted.
  163. * @return string the absolute file path
  164. * @throws InvalidConfigException if [[basePath]] is not set
  165. */
  166. public function getPath($path)
  167. {
  168. if (($basePath = $this->getBasePath()) !== null) {
  169. return $basePath . DIRECTORY_SEPARATOR . ltrim($path, '/\\');
  170. }
  171. throw new InvalidConfigException('The "basePath" property must be set.');
  172. }
  173. }