Theme.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 [[Application::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 `['/web/views' => '/web/themes/basic']`,
  28. * then the themed version for a view file `/web/views/site/index.php` will be
  29. * `/web/themes/basic/site/index.php`.
  30. *
  31. * It is possible to map a single path to multiple paths. For example,
  32. *
  33. * ~~~
  34. * 'pathMap' => [
  35. * '/web/views' => [
  36. * '/web/themes/christmas',
  37. * '/web/themes/basic',
  38. * ],
  39. * ]
  40. * ~~~
  41. *
  42. * In this case, the themed version could be either `/web/themes/christmas/site/index.php` or
  43. * `/web/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. * ~~~
  49. * 'view' => [
  50. * 'theme' => [
  51. * 'basePath' => '@webroot/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. * @author Qiang Xue <[email protected]>
  62. * @since 2.0
  63. */
  64. class Theme extends Component
  65. {
  66. /**
  67. * @var string the root path or path alias of this theme. All resources of this theme are located
  68. * under this directory. This property must be set if [[pathMap]] is not set.
  69. * @see pathMap
  70. */
  71. public $basePath;
  72. /**
  73. * @var string the base URL (or path alias) for this theme. All resources of this theme are considered
  74. * to be under this base URL. This property must be set. It is mainly used by [[getUrl()]].
  75. */
  76. public $baseUrl;
  77. /**
  78. * @var array the mapping between view directories and their corresponding themed versions.
  79. * If not set, it will be initialized as a mapping from [[Application::basePath]] to [[basePath]].
  80. * This property is used by [[applyTo()]] when a view is trying to apply the theme.
  81. * Path aliases can be used when specifying directories.
  82. */
  83. public $pathMap;
  84. /**
  85. * Initializes the theme.
  86. * @throws InvalidConfigException if [[basePath]] is not set.
  87. */
  88. public function init()
  89. {
  90. parent::init();
  91. if (empty($this->pathMap)) {
  92. if ($this->basePath !== null) {
  93. $this->basePath = Yii::getAlias($this->basePath);
  94. $this->pathMap = [Yii::$app->getBasePath() => [$this->basePath]];
  95. } else {
  96. throw new InvalidConfigException('The "basePath" property must be set.');
  97. }
  98. }
  99. $paths = [];
  100. foreach ($this->pathMap as $from => $tos) {
  101. $from = FileHelper::normalizePath(Yii::getAlias($from));
  102. foreach ((array)$tos as $to) {
  103. $to = FileHelper::normalizePath(Yii::getAlias($to));
  104. $paths[$from . DIRECTORY_SEPARATOR][] = $to . DIRECTORY_SEPARATOR;
  105. }
  106. }
  107. $this->pathMap = $paths;
  108. if ($this->baseUrl === null) {
  109. throw new InvalidConfigException('The "baseUrl" property must be set.');
  110. } else {
  111. $this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
  112. }
  113. }
  114. /**
  115. * Converts a file to a themed file if possible.
  116. * If there is no corresponding themed file, the original file will be returned.
  117. * @param string $path the file to be themed
  118. * @return string the themed file, or the original file if the themed version is not available.
  119. */
  120. public function applyTo($path)
  121. {
  122. $path = FileHelper::normalizePath($path);
  123. foreach ($this->pathMap as $from => $tos) {
  124. if (strpos($path, $from) === 0) {
  125. $n = strlen($from);
  126. foreach ($tos as $to) {
  127. $file = $to . substr($path, $n);
  128. if (is_file($file)) {
  129. return $file;
  130. }
  131. }
  132. }
  133. }
  134. return $path;
  135. }
  136. /**
  137. * Converts a relative URL into an absolute URL using [[baseUrl]].
  138. * @param string $url the relative URL to be converted.
  139. * @return string the absolute URL
  140. */
  141. public function getUrl($url)
  142. {
  143. return $this->baseUrl . '/' . ltrim($url, '/');
  144. }
  145. }