Theme.php 4.88 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\helpers\FileHelper;
Qiang Xue committed
12

Qiang Xue committed
13 14 15
/**
 * Theme represents an application theme.
 *
16 17
 * When [[View]] renders a view file, it will check the [[Application::theme|active theme]]
 * to see if there is a themed version of the view file exists. If so, the themed version will be rendered instead.
Qiang Xue committed
18
 *
Carsten Brandt committed
19
 * A theme is a directory consisting of view files which are meant to replace their non-themed counterparts.
20 21 22 23 24 25 26 27 28
 *
 * Theme uses [[pathMap]] to achieve the view file replacement:
 *
 * 1. It first looks for a key in [[pathMap]] that is a substring of the given view file path;
 * 2. If such a key exists, the corresponding value will be used to replace the corresponding part
 *    in the view file path;
 * 3. It will then check if the updated view file exists or not. If so, that file will be used
 *    to replace the original view file.
 * 4. If Step 2 or 3 fails, the original view file will be used.
Qiang Xue committed
29
 *
Alexander Makarov committed
30
 * For example, if [[pathMap]] is `['/web/views' => '/web/themes/basic']`,
31 32
 * then the themed version for a view file `/web/views/site/index.php` will be
 * `/web/themes/basic/site/index.php`.
Qiang Xue committed
33
 *
34 35 36 37 38 39 40 41 42 43 44 45 46 47
 * It is possible to map a single path to multiple paths. For example,
 *
 * ~~~
 * 'pathMap' => [
 *     '/web/views' => [
 *         '/web/themes/christmas',
 *         '/web/themes/basic',
 *     ],
 * ]
 * ~~~
 *
 * In this case, the themed version could be either `/web/themes/christmas/site/index.php` or
 * `/web/themes/basic/site/index.php`. The former has precedence over the latter if both files exist.
 *
48 49 50 51
 * To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
 * component like the following:
 *
 * ~~~
Alexander Makarov committed
52 53
 * 'view' => [
 *     'theme' => [
54 55
 *         'basePath' => '@webroot/themes/basic',
 *         'baseUrl' => '@web/themes/basic',
Alexander Makarov committed
56 57
 *     ],
 * ],
58 59 60 61 62 63
 * ~~~
 *
 * The above configuration specifies a theme located under the "themes/basic" directory of the Web folder
 * that contains the entry script of the application. If your theme is designed to handle modules,
 * you may configure the [[pathMap]] property like described above.
 *
Qiang Xue committed
64 65 66
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
67
class Theme extends Component
Qiang Xue committed
68
{
Qiang Xue committed
69
	/**
Qiang Xue committed
70 71
	 * @var string the root path or path alias of this theme. All resources of this theme are located
	 * under this directory. This property must be set if [[pathMap]] is not set.
Qiang Xue committed
72 73
	 * @see pathMap
	 */
Qiang Xue committed
74
	public $basePath;
Qiang Xue committed
75 76 77 78 79
	/**
	 * @var string the base URL (or path alias) for this theme. All resources of this theme are considered
	 * to be under this base URL. This property must be set. It is mainly used by [[getUrl()]].
	 */
	public $baseUrl;
Qiang Xue committed
80 81 82
	/**
	 * @var array the mapping between view directories and their corresponding themed versions.
	 * If not set, it will be initialized as a mapping from [[Application::basePath]] to [[basePath]].
Qiang Xue committed
83 84
	 * This property is used by [[applyTo()]] when a view is trying to apply the theme.
	 * Path aliases can be used when specifying directories.
Qiang Xue committed
85 86 87
	 */
	public $pathMap;

Qiang Xue committed
88

Qiang Xue committed
89 90 91 92
	/**
	 * Initializes the theme.
	 * @throws InvalidConfigException if [[basePath]] is not set.
	 */
Qiang Xue committed
93
	public function init()
Qiang Xue committed
94
	{
resurtm committed
95
		parent::init();
Qiang Xue committed
96 97
		if (empty($this->pathMap)) {
			if ($this->basePath !== null) {
Qiang Xue committed
98
				$this->basePath = Yii::getAlias($this->basePath);
99
				$this->pathMap = [Yii::$app->getBasePath() => [$this->basePath]];
Qiang Xue committed
100
			} else {
Qiang Xue committed
101
				throw new InvalidConfigException('The "basePath" property must be set.');
Qiang Xue committed
102
			}
Qiang Xue committed
103
		}
Alexander Makarov committed
104
		$paths = [];
105
		foreach ($this->pathMap as $from => $tos) {
Qiang Xue committed
106
			$from = FileHelper::normalizePath(Yii::getAlias($from));
107 108 109 110
			foreach ((array)$tos as $to) {
				$to = FileHelper::normalizePath(Yii::getAlias($to));
				$paths[$from . DIRECTORY_SEPARATOR][] = $to . DIRECTORY_SEPARATOR;
			}
Qiang Xue committed
111
		}
Qiang Xue committed
112
		$this->pathMap = $paths;
Qiang Xue committed
113
		if ($this->baseUrl === null) {
Qiang Xue committed
114
			throw new InvalidConfigException('The "baseUrl" property must be set.');
Qiang Xue committed
115 116 117
		} else {
			$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
		}
Qiang Xue committed
118 119 120
	}

	/**
Qiang Xue committed
121 122 123 124
	 * Converts a file to a themed file if possible.
	 * If there is no corresponding themed file, the original file will be returned.
	 * @param string $path the file to be themed
	 * @return string the themed file, or the original file if the themed version is not available.
Qiang Xue committed
125
	 */
Qiang Xue committed
126
	public function applyTo($path)
Qiang Xue committed
127
	{
Qiang Xue committed
128
		$path = FileHelper::normalizePath($path);
129
		foreach ($this->pathMap as $from => $tos) {
Qiang Xue committed
130 131
			if (strpos($path, $from) === 0) {
				$n = strlen($from);
132 133 134 135 136
				foreach ($tos as $to) {
					$file = $to . substr($path, $n);
					if (is_file($file)) {
						return $file;
					}
Qiang Xue committed
137 138
				}
			}
Qiang Xue committed
139
		}
Qiang Xue committed
140
		return $path;
Qiang Xue committed
141 142 143
	}

	/**
Qiang Xue committed
144
	 * Converts a relative URL into an absolute URL using [[baseUrl]].
Qiang Xue committed
145 146
	 * @param string $url the relative URL to be converted.
	 * @return string the absolute URL
Qiang Xue committed
147
	 */
Qiang Xue committed
148
	public function getUrl($url)
Qiang Xue committed
149
	{
Qiang Xue committed
150
		return $this->baseUrl . '/' . ltrim($url, '/');
Qiang Xue committed
151 152
	}
}