Theme.php 3.56 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\base\InvalidConfigException;
Qiang Xue committed
12
use yii\helpers\FileHelper;
Qiang Xue committed
13

Qiang Xue committed
14 15 16
/**
 * Theme represents an application theme.
 *
Qiang Xue committed
17 18 19 20 21 22 23 24 25 26 27 28 29
 * A theme is directory consisting of view and layout files which are meant to replace their
 * non-themed counterparts.
 *
 * Theme uses [[pathMap]] to achieve the file replacement. A view or layout file will be replaced
 * with its themed version if part of its path matches one of the keys in [[pathMap]].
 * Then the matched part will be replaced with the corresponding array value.
 *
 * For example, if [[pathMap]] is `array('/www/views' => '/www/themes/basic')`,
 * then the themed version for a view file `/www/views/site/index.php` will be
 * `/www/themes/basic/site/index.php`.
 *
 * @property string $baseUrl the base URL for this theme. This is mainly used by [[getUrl()]].
 *
Qiang Xue committed
30 31 32
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
33
class Theme extends Component
Qiang Xue committed
34
{
Qiang Xue committed
35
	/**
Qiang Xue committed
36 37
	 * @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
38 39
	 * @see pathMap
	 */
Qiang Xue committed
40
	public $basePath;
Qiang Xue committed
41 42 43 44 45
	/**
	 * @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
46 47 48
	/**
	 * @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
49 50
	 * 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
51 52 53
	 */
	public $pathMap;

Qiang Xue committed
54

Qiang Xue committed
55 56 57 58
	/**
	 * Initializes the theme.
	 * @throws InvalidConfigException if [[basePath]] is not set.
	 */
Qiang Xue committed
59
	public function init()
Qiang Xue committed
60
	{
Qiang Xue committed
61 62 63
	 	parent::init();
		if (empty($this->pathMap)) {
			if ($this->basePath !== null) {
Qiang Xue committed
64
				$this->basePath = Yii::getAlias($this->basePath);
Qiang Xue committed
65
				$this->pathMap = array(Yii::$app->getBasePath() => $this->basePath);
Qiang Xue committed
66
			} else {
Qiang Xue committed
67
				throw new InvalidConfigException('The "basePath" property must be set.');
Qiang Xue committed
68
			}
Qiang Xue committed
69
		}
Qiang Xue committed
70 71
		$paths = array();
		foreach ($this->pathMap as $from => $to) {
Qiang Xue committed
72 73 74
			$from = FileHelper::normalizePath(Yii::getAlias($from));
			$to = FileHelper::normalizePath(Yii::getAlias($to));
			$paths[$from . DIRECTORY_SEPARATOR] = $to . DIRECTORY_SEPARATOR;
Qiang Xue committed
75
		}
Qiang Xue committed
76
		$this->pathMap = $paths;
Qiang Xue committed
77
		if ($this->baseUrl === null) {
Qiang Xue committed
78
			throw new InvalidConfigException('The "baseUrl" property must be set.');
Qiang Xue committed
79 80 81
		} else {
			$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
		}
Qiang Xue committed
82 83 84
	}

	/**
Qiang Xue committed
85 86 87 88
	 * 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
89
	 */
Qiang Xue committed
90
	public function applyTo($path)
Qiang Xue committed
91
	{
Qiang Xue committed
92 93 94 95 96 97 98 99 100
		$path = FileHelper::normalizePath($path);
		foreach ($this->pathMap as $from => $to) {
			if (strpos($path, $from) === 0) {
				$n = strlen($from);
				$file = $to . substr($path, $n);
				if (is_file($file)) {
					return $file;
				}
			}
Qiang Xue committed
101
		}
Qiang Xue committed
102
		return $path;
Qiang Xue committed
103 104 105
	}

	/**
Qiang Xue committed
106
	 * Converts a relative URL into an absolute URL using [[baseUrl]].
Qiang Xue committed
107 108
	 * @param string $url the relative URL to be converted.
	 * @return string the absolute URL
Qiang Xue committed
109
	 */
Qiang Xue committed
110
	public function getUrl($url)
Qiang Xue committed
111
	{
Qiang Xue committed
112
		return $this->baseUrl . '/' . ltrim($url, '/');
Qiang Xue committed
113 114
	}
}