~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/web/CThemeManager.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CThemeManager class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 
8
 * @license http://www.yiiframework.com/license/
 
9
 */
 
10
 
 
11
/**
 
12
 * CThemeManager manages the themes for the Web application.
 
13
 *
 
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.
 
19
 *
 
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.
 
22
 *
 
23
 * To activate a theme, set the {@link CWebApplication::setTheme theme} property
 
24
 * to be the name of that theme.
 
25
 *
 
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.
 
28
 *
 
29
 * @author Qiang Xue <qiang.xue@gmail.com>
 
30
 * @version $Id: CThemeManager.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
31
 * @package system.web
 
32
 * @since 1.0
 
33
 */
 
34
class CThemeManager extends CApplicationComponent
 
35
{
 
36
        /**
 
37
         * default themes base path
 
38
         */
 
39
        const DEFAULT_BASEPATH='themes';
 
40
 
 
41
        /**
 
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.
 
44
         */
 
45
        public $themeClass='CTheme';
 
46
 
 
47
        private $_basePath=null;
 
48
        private $_baseUrl=null;
 
49
 
 
50
 
 
51
        /**
 
52
         * @param string $name name of the theme to be retrieved
 
53
         * @return CTheme the theme retrieved. Null if the theme does not exist.
 
54
         */
 
55
        public function getTheme($name)
 
56
        {
 
57
                $themePath=$this->getBasePath().DIRECTORY_SEPARATOR.$name;
 
58
                if(is_dir($themePath))
 
59
                {
 
60
                        $class=Yii::import($this->themeClass);
 
61
                        return new $class($name,$themePath,$this->getBaseUrl().'/'.$name);
 
62
                }
 
63
                else
 
64
                        return null;
 
65
        }
 
66
 
 
67
        /**
 
68
         * @return array list of available theme names
 
69
         */
 
70
        public function getThemeNames()
 
71
        {
 
72
                static $themes;
 
73
                if($themes===null)
 
74
                {
 
75
                        $themes=array();
 
76
                        $basePath=$this->getBasePath();
 
77
                        $folder=@opendir($basePath);
 
78
                        while(($file=@readdir($folder))!==false)
 
79
                        {
 
80
                                if($file!=='.' && $file!=='..' && $file!=='.svn' && is_dir($basePath.DIRECTORY_SEPARATOR.$file))
 
81
                                        $themes[]=$file;
 
82
                        }
 
83
                        closedir($folder);
 
84
                        sort($themes);
 
85
                }
 
86
                return $themes;
 
87
        }
 
88
 
 
89
        /**
 
90
         * @return string the base path for all themes. Defaults to "WebRootPath/themes".
 
91
         */
 
92
        public function getBasePath()
 
93
        {
 
94
                if($this->_basePath===null)
 
95
                        $this->setBasePath(dirname(Yii::app()->getRequest()->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
 
96
                return $this->_basePath;
 
97
        }
 
98
 
 
99
        /**
 
100
         * @param string $value the base path for all themes.
 
101
         * @throws CException if the base path does not exist
 
102
         */
 
103
        public function setBasePath($value)
 
104
        {
 
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)));
 
108
        }
 
109
 
 
110
        /**
 
111
         * @return string the base URL for all themes. Defaults to "/WebRoot/themes".
 
112
         */
 
113
        public function getBaseUrl()
 
114
        {
 
115
                if($this->_baseUrl===null)
 
116
                        $this->_baseUrl=Yii::app()->getBaseUrl().'/'.self::DEFAULT_BASEPATH;
 
117
                return $this->_baseUrl;
 
118
        }
 
119
 
 
120
        /**
 
121
         * @param string $value the base URL for all themes.
 
122
         */
 
123
        public function setBaseUrl($value)
 
124
        {
 
125
                $this->_baseUrl=rtrim($value,'/');
 
126
        }
 
127
}