Theme.php 3.38 KB
Newer Older
Qiang Xue committed
1 2 3 4 5
<?php
/**
 * Theme class file.
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
Qiang Xue committed
7 8 9 10 11
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

Qiang Xue committed
12
use Yii;
Qiang Xue committed
13
use yii\base\InvalidConfigException;
Qiang Xue committed
14
use yii\util\FileHelper;
Qiang Xue committed
15

Qiang Xue committed
16 17 18
/**
 * Theme represents an application theme.
 *
Qiang Xue committed
19 20 21 22 23 24 25 26 27 28 29 30 31
 * 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
32 33 34
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
35
class Theme extends Component
Qiang Xue committed
36
{
Qiang Xue committed
37 38 39 40
	/**
	 * @var string the root path of this theme.
	 * @see pathMap
	 */
Qiang Xue committed
41
	public $basePath;
Qiang Xue committed
42 43 44 45 46 47 48 49
	/**
	 * @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]].
	 * This property is used by [[apply()]] when a view is trying to apply the theme.
	 */
	public $pathMap;

	private $_baseUrl;
Qiang Xue committed
50

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

	/**
	 * Returns the base URL for this theme.
	 * The method [[getUrl()]] will prefix this to the given URL.
	 * @return string the base URL for this theme.
	 */
	public function getBaseUrl()
	{
		return $this->_baseUrl;
	}

	/**
	 * Sets the base URL for this theme.
	 * @param string $value the base URL for this theme.
	 */
	public function setBaseUrl($value)
	{
		$this->_baseUrl = rtrim(Yii::getAlias($value), '/');
Qiang Xue committed
90 91 92
	}

	/**
Qiang Xue committed
93 94 95 96
	 * 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
97
	 */
Qiang Xue committed
98
	public function apply($path)
Qiang Xue committed
99
	{
Qiang Xue committed
100 101 102 103 104 105 106 107 108
		$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
109
		}
Qiang Xue committed
110
		return $path;
Qiang Xue committed
111 112 113
	}

	/**
Qiang Xue committed
114 115 116
	 * Converts a relative URL into an absolute URL using [[basePath]].
	 * @param string $url the relative URL to be converted.
	 * @return string the absolute URL
Qiang Xue committed
117
	 */
Qiang Xue committed
118
	public function getUrl($url)
Qiang Xue committed
119
	{
Qiang Xue committed
120
		return $this->baseUrl . '/' . ltrim($url, '/');
Qiang Xue committed
121 122
	}
}