Theme.php 1.49 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 13
use yii\base\InvalidConfigException;

Qiang Xue committed
14 15 16 17 18 19 20 21
/**
 * Theme represents an application theme.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Theme extends ApplicationComponent
{
Qiang Xue committed
22 23
	public $basePath;
	public $baseUrl;
Qiang Xue committed
24

Qiang Xue committed
25
	public function init()
Qiang Xue committed
26
	{
Qiang Xue committed
27
		if ($this->basePath !== null) {
Qiang Xue committed
28
			$this->basePath = \Yii::getAlias($this->basePath, true);
Qiang Xue committed
29
		} else {
Qiang Xue committed
30
			throw new InvalidConfigException("Theme.basePath must be set.");
Qiang Xue committed
31 32
		}
		if ($this->baseUrl !== null) {
Qiang Xue committed
33
			$this->baseUrl = \Yii::getAlias($this->baseUrl, true);
Qiang Xue committed
34
		} else {
Qiang Xue committed
35
			throw new InvalidConfigException("Theme.baseUrl must be set.");
Qiang Xue committed
36
		}
Qiang Xue committed
37 38 39
	}

	/**
Qiang Xue committed
40
	 * @param Application|Module|Controller|Object $context
Qiang Xue committed
41
	 * @return string
Qiang Xue committed
42
	 */
Qiang Xue committed
43
	public function getViewPath($context = null)
Qiang Xue committed
44
	{
Qiang Xue committed
45 46 47 48 49 50 51 52
		$viewPath = $this->basePath . DIRECTORY_SEPARATOR . 'views';
		if ($context === null || $context instanceof Application) {
			return $viewPath;
		} elseif ($context instanceof Controller || $context instanceof Module) {
			return $viewPath . DIRECTORY_SEPARATOR . $context->getUniqueId();
		} else {
			return $viewPath . DIRECTORY_SEPARATOR . str_replace('\\', '_', get_class($context));
		}
Qiang Xue committed
53 54 55
	}

	/**
Qiang Xue committed
56 57
	 * @param Module $module
	 * @return string
Qiang Xue committed
58
	 */
Qiang Xue committed
59
	public function getLayoutPath($module = null)
Qiang Xue committed
60
	{
Qiang Xue committed
61
		return $this->getViewPath($module) . DIRECTORY_SEPARATOR . 'layouts';
Qiang Xue committed
62 63
	}
}