CThemeManager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * CThemeManager class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CThemeManager manages the themes for the Web application.
  12. *
  13. * A theme is a collection of view/layout files and resource files
  14. * (e.g. css, image, js files). When a theme is active, {@link CController}
  15. * will look for the specified view/layout under the theme folder first.
  16. * The corresponding view/layout files will be used if the theme provides them.
  17. * Otherwise, the default view/layout files will be used.
  18. *
  19. * By default, each theme is organized as a directory whose name is the theme name.
  20. * All themes are located under the "WebRootPath/themes" directory.
  21. *
  22. * To activate a theme, set the {@link CWebApplication::setTheme theme} property
  23. * to be the name of that theme.
  24. *
  25. * Since a self-contained theme often contains resource files that are made
  26. * Web accessible, please make sure the view/layout files are protected from Web access.
  27. *
  28. * @property array $themeNames List of available theme names.
  29. * @property string $basePath The base path for all themes. Defaults to "WebRootPath/themes".
  30. * @property string $baseUrl The base URL for all themes. Defaults to "/WebRoot/themes".
  31. *
  32. * @author Qiang Xue <qiang.xue@gmail.com>
  33. * @package system.web
  34. * @since 1.0
  35. */
  36. class CThemeManager extends CApplicationComponent
  37. {
  38. /**
  39. * default themes base path
  40. */
  41. const DEFAULT_BASEPATH='themes';
  42. /**
  43. * @var string the name of the theme class for representing a theme.
  44. * Defaults to {@link CTheme}. This can also be a class name in dot syntax.
  45. */
  46. public $themeClass='CTheme';
  47. private $_basePath=null;
  48. private $_baseUrl=null;
  49. /**
  50. * @param string $name name of the theme to be retrieved
  51. * @return CTheme the theme retrieved. Null if the theme does not exist.
  52. */
  53. public function getTheme($name)
  54. {
  55. $themePath=$this->getBasePath().DIRECTORY_SEPARATOR.$name;
  56. if(is_dir($themePath))
  57. {
  58. $class=Yii::import($this->themeClass, true);
  59. return new $class($name,$themePath,$this->getBaseUrl().'/'.$name);
  60. }
  61. else
  62. return null;
  63. }
  64. /**
  65. * @return array list of available theme names
  66. */
  67. public function getThemeNames()
  68. {
  69. static $themes;
  70. if($themes===null)
  71. {
  72. $themes=array();
  73. $basePath=$this->getBasePath();
  74. $folder=@opendir($basePath);
  75. while(($file=@readdir($folder))!==false)
  76. {
  77. if($file!=='.' && $file!=='..' && $file!=='.svn' && $file!=='.gitignore' && is_dir($basePath.DIRECTORY_SEPARATOR.$file))
  78. $themes[]=$file;
  79. }
  80. closedir($folder);
  81. sort($themes);
  82. }
  83. return $themes;
  84. }
  85. /**
  86. * @return string the base path for all themes. Defaults to "WebRootPath/themes".
  87. */
  88. public function getBasePath()
  89. {
  90. if($this->_basePath===null)
  91. $this->setBasePath(dirname(Yii::app()->getRequest()->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
  92. return $this->_basePath;
  93. }
  94. /**
  95. * @param string $value the base path for all themes.
  96. * @throws CException if the base path does not exist
  97. */
  98. public function setBasePath($value)
  99. {
  100. $this->_basePath=realpath($value);
  101. if($this->_basePath===false || !is_dir($this->_basePath))
  102. throw new CException(Yii::t('yii','Theme directory "{directory}" does not exist.',array('{directory}'=>$value)));
  103. }
  104. /**
  105. * @return string the base URL for all themes. Defaults to "/WebRoot/themes".
  106. */
  107. public function getBaseUrl()
  108. {
  109. if($this->_baseUrl===null)
  110. $this->_baseUrl=Yii::app()->getBaseUrl().'/'.self::DEFAULT_BASEPATH;
  111. return $this->_baseUrl;
  112. }
  113. /**
  114. * @param string $value the base URL for all themes.
  115. */
  116. public function setBaseUrl($value)
  117. {
  118. $this->_baseUrl=rtrim($value,'/');
  119. }
  120. }