3
* CThemeManager class file.
5
* @author Qiang Xue <qiang.xue@gmail.com>
6
* @link http://www.yiiframework.com/
7
* @copyright Copyright © 2008-2011 Yii Software LLC
8
* @license http://www.yiiframework.com/license/
12
* CThemeManager manages the themes for the Web application.
14
* A theme is a collection of view/layout files and resource files
15
* (e.g. css, image, js files). When a theme is active, {@link CController}
16
* will look for the specified view/layout under the theme folder first.
17
* The corresponding view/layout files will be used if the theme provides them.
18
* Otherwise, the default view/layout files will be used.
20
* By default, each theme is organized as a directory whose name is the theme name.
21
* All themes are located under the "WebRootPath/themes" directory.
23
* To activate a theme, set the {@link CWebApplication::setTheme theme} property
24
* to be the name of that theme.
26
* Since a self-contained theme often contains resource files that are made
27
* Web accessible, please make sure the view/layout files are protected from Web access.
29
* @author Qiang Xue <qiang.xue@gmail.com>
30
* @version $Id: CThemeManager.php 2799 2011-01-01 19:31:13Z qiang.xue $
34
class CThemeManager extends CApplicationComponent
37
* default themes base path
39
const DEFAULT_BASEPATH='themes';
42
* @var string the name of the theme class for representing a theme.
43
* Defaults to {@link CTheme}. This can also be a class name in dot syntax.
45
public $themeClass='CTheme';
47
private $_basePath=null;
48
private $_baseUrl=null;
52
* @param string $name name of the theme to be retrieved
53
* @return CTheme the theme retrieved. Null if the theme does not exist.
55
public function getTheme($name)
57
$themePath=$this->getBasePath().DIRECTORY_SEPARATOR.$name;
58
if(is_dir($themePath))
60
$class=Yii::import($this->themeClass);
61
return new $class($name,$themePath,$this->getBaseUrl().'/'.$name);
68
* @return array list of available theme names
70
public function getThemeNames()
76
$basePath=$this->getBasePath();
77
$folder=@opendir($basePath);
78
while(($file=@readdir($folder))!==false)
80
if($file!=='.' && $file!=='..' && $file!=='.svn' && is_dir($basePath.DIRECTORY_SEPARATOR.$file))
90
* @return string the base path for all themes. Defaults to "WebRootPath/themes".
92
public function getBasePath()
94
if($this->_basePath===null)
95
$this->setBasePath(dirname(Yii::app()->getRequest()->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
96
return $this->_basePath;
100
* @param string $value the base path for all themes.
101
* @throws CException if the base path does not exist
103
public function setBasePath($value)
105
$this->_basePath=realpath($value);
106
if($this->_basePath===false || !is_dir($this->_basePath))
107
throw new CException(Yii::t('yii','Theme directory "{directory}" does not exist.',array('{directory}'=>$value)));
111
* @return string the base URL for all themes. Defaults to "/WebRoot/themes".
113
public function getBaseUrl()
115
if($this->_baseUrl===null)
116
$this->_baseUrl=Yii::app()->getBaseUrl().'/'.self::DEFAULT_BASEPATH;
117
return $this->_baseUrl;
121
* @param string $value the base URL for all themes.
123
public function setBaseUrl($value)
125
$this->_baseUrl=rtrim($value,'/');