Theme.php 1.45 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/**
 * Theme class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

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

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

	/**
Qiang Xue committed
38
	 * @param Application|Module|Controller|Object $context
Qiang Xue committed
39
	 * @return string
Qiang Xue committed
40
	 */
Qiang Xue committed
41
	public function getViewPath($context = null)
Qiang Xue committed
42
	{
Qiang Xue committed
43 44 45 46 47 48 49 50
		$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
51 52 53
	}

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