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

Qiang Xue committed
69

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

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

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