Theme.php 4.04 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
 * 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.
 *
24 25 26
 * For example, if [[pathMap]] is `array('/web/views' => '/web/themes/basic')`,
 * then the themed version for a view file `/web/views/site/index.php` will be
 * `/web/themes/basic/site/index.php`.
Qiang Xue committed
27
 *
28 29 30 31 32 33
 * To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
 * component like the following:
 *
 * ~~~
 * 'view' => array(
 *     'theme' => array(
34 35
 *         'basePath' => '@webroot/themes/basic',
 *         'baseUrl' => '@web/themes/basic',
36 37 38 39 40 41 42 43
 *     ),
 * ),
 * ~~~
 *
 * 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
44 45 46
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
47
class Theme extends Component
Qiang Xue committed
48
{
Qiang Xue committed
49
	/**
Qiang Xue committed
50 51
	 * @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
52 53
	 * @see pathMap
	 */
Qiang Xue committed
54
	public $basePath;
Qiang Xue committed
55 56 57 58 59
	/**
	 * @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
60 61 62
	/**
	 * @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
63 64
	 * 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
65 66 67
	 */
	public $pathMap;

Qiang Xue committed
68

Qiang Xue committed
69 70 71 72
	/**
	 * Initializes the theme.
	 * @throws InvalidConfigException if [[basePath]] is not set.
	 */
Qiang Xue committed
73
	public function init()
Qiang Xue committed
74
	{
resurtm committed
75
		parent::init();
Qiang Xue committed
76 77
		if (empty($this->pathMap)) {
			if ($this->basePath !== null) {
Qiang Xue committed
78
				$this->basePath = Yii::getAlias($this->basePath);
Qiang Xue committed
79
				$this->pathMap = array(Yii::$app->getBasePath() => $this->basePath);
Qiang Xue committed
80
			} else {
Qiang Xue committed
81
				throw new InvalidConfigException('The "basePath" property must be set.');
Qiang Xue committed
82
			}
Qiang Xue committed
83
		}
Qiang Xue committed
84 85
		$paths = array();
		foreach ($this->pathMap as $from => $to) {
Qiang Xue committed
86 87 88
			$from = FileHelper::normalizePath(Yii::getAlias($from));
			$to = FileHelper::normalizePath(Yii::getAlias($to));
			$paths[$from . DIRECTORY_SEPARATOR] = $to . DIRECTORY_SEPARATOR;
Qiang Xue committed
89
		}
Qiang Xue committed
90
		$this->pathMap = $paths;
Qiang Xue committed
91
		if ($this->baseUrl === null) {
Qiang Xue committed
92
			throw new InvalidConfigException('The "baseUrl" property must be set.');
Qiang Xue committed
93 94 95
		} else {
			$this->baseUrl = rtrim(Yii::getAlias($this->baseUrl), '/');
		}
Qiang Xue committed
96 97 98
	}

	/**
Qiang Xue committed
99 100 101 102
	 * 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
103
	 */
Qiang Xue committed
104
	public function applyTo($path)
Qiang Xue committed
105
	{
Qiang Xue committed
106 107 108 109 110 111 112 113 114
		$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
115
		}
Qiang Xue committed
116
		return $path;
Qiang Xue committed
117 118 119
	}

	/**
Qiang Xue committed
120
	 * Converts a relative URL into an absolute URL using [[baseUrl]].
Qiang Xue committed
121 122
	 * @param string $url the relative URL to be converted.
	 * @return string the absolute URL
Qiang Xue committed
123
	 */
Qiang Xue committed
124
	public function getUrl($url)
Qiang Xue committed
125
	{
Qiang Xue committed
126
		return $this->baseUrl . '/' . ltrim($url, '/');
Qiang Xue committed
127 128
	}
}