1: <?php
2: /**
3: * CWebModule 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: /**
12: * CWebModule represents an application module.
13: *
14: * An application module may be considered as a self-contained sub-application
15: * that has its own controllers, models and views and can be reused in a different
16: * project as a whole. Controllers inside a module must be accessed with routes
17: * that are prefixed with the module ID.
18: *
19: * @property string $name The name of this module.
20: * @property string $description The description of this module.
21: * @property string $version The version of this module.
22: * @property string $controllerPath The directory that contains the controller classes. Defaults to 'moduleDir/controllers'
23: * where moduleDir is the directory containing the module class.
24: * @property string $viewPath The root directory of view files. Defaults to 'moduleDir/views' where moduleDir is
25: * the directory containing the module class.
26: * @property string $layoutPath The root directory of layout files. Defaults to 'moduleDir/views/layouts' where
27: * moduleDir is the directory containing the module class.
28: *
29: * @author Qiang Xue <qiang.xue@gmail.com>
30: * @package system.web
31: */
32: class CWebModule extends CModule
33: {
34: /**
35: * @var string the ID of the default controller for this module. Defaults to 'default'.
36: */
37: public $defaultController='default';
38: /**
39: * @var mixed the layout that is shared by the controllers inside this module.
40: * If a controller has explicitly declared its own {@link CController::layout layout},
41: * this property will be ignored.
42: * If this is null (default), the application's layout or the parent module's layout (if available)
43: * will be used. If this is false, then no layout will be used.
44: */
45: public $layout;
46: /**
47: * @var string Namespace that should be used when loading controllers.
48: * Default is to use global namespace.
49: * @since 1.1.11
50: */
51: public $controllerNamespace;
52: /**
53: * @var array mapping from controller ID to controller configurations.
54: * Pleaser refer to {@link CWebApplication::controllerMap} for more details.
55: */
56: public $controllerMap=array();
57:
58: private $_controllerPath;
59: private $_viewPath;
60: private $_layoutPath;
61:
62:
63: /**
64: * Returns the name of this module.
65: * The default implementation simply returns {@link id}.
66: * You may override this method to customize the name of this module.
67: * @return string the name of this module.
68: */
69: public function getName()
70: {
71: return basename($this->getId());
72: }
73:
74: /**
75: * Returns the description of this module.
76: * The default implementation returns an empty string.
77: * You may override this method to customize the description of this module.
78: * @return string the description of this module.
79: */
80: public function getDescription()
81: {
82: return '';
83: }
84:
85: /**
86: * Returns the version of this module.
87: * The default implementation returns '1.0'.
88: * You may override this method to customize the version of this module.
89: * @return string the version of this module.
90: */
91: public function getVersion()
92: {
93: return '1.0';
94: }
95:
96: /**
97: * @return string the directory that contains the controller classes. Defaults to 'moduleDir/controllers' where
98: * moduleDir is the directory containing the module class.
99: */
100: public function getControllerPath()
101: {
102: if($this->_controllerPath!==null)
103: return $this->_controllerPath;
104: else
105: return $this->_controllerPath=$this->getBasePath().DIRECTORY_SEPARATOR.'controllers';
106: }
107:
108: /**
109: * @param string $value the directory that contains the controller classes.
110: * @throws CException if the directory is invalid
111: */
112: public function setControllerPath($value)
113: {
114: if(($this->_controllerPath=realpath($value))===false || !is_dir($this->_controllerPath))
115: throw new CException(Yii::t('yii','The controller path "{path}" is not a valid directory.',
116: array('{path}'=>$value)));
117: }
118:
119: /**
120: * @return string the root directory of view files. Defaults to 'moduleDir/views' where
121: * moduleDir is the directory containing the module class.
122: */
123: public function getViewPath()
124: {
125: if($this->_viewPath!==null)
126: return $this->_viewPath;
127: else
128: return $this->_viewPath=$this->getBasePath().DIRECTORY_SEPARATOR.'views';
129: }
130:
131: /**
132: * @param string $path the root directory of view files.
133: * @throws CException if the directory does not exist.
134: */
135: public function setViewPath($path)
136: {
137: if(($this->_viewPath=realpath($path))===false || !is_dir($this->_viewPath))
138: throw new CException(Yii::t('yii','The view path "{path}" is not a valid directory.',
139: array('{path}'=>$path)));
140: }
141:
142: /**
143: * @return string the root directory of layout files. Defaults to 'moduleDir/views/layouts' where
144: * moduleDir is the directory containing the module class.
145: */
146: public function getLayoutPath()
147: {
148: if($this->_layoutPath!==null)
149: return $this->_layoutPath;
150: else
151: return $this->_layoutPath=$this->getViewPath().DIRECTORY_SEPARATOR.'layouts';
152: }
153:
154: /**
155: * @param string $path the root directory of layout files.
156: * @throws CException if the directory does not exist.
157: */
158: public function setLayoutPath($path)
159: {
160: if(($this->_layoutPath=realpath($path))===false || !is_dir($this->_layoutPath))
161: throw new CException(Yii::t('yii','The layout path "{path}" is not a valid directory.',
162: array('{path}'=>$path)));
163: }
164:
165: /**
166: * The pre-filter for controller actions.
167: * This method is invoked before the currently requested controller action and all its filters
168: * are executed. You may override this method in the following way:
169: * <pre>
170: * if(parent::beforeControllerAction($controller,$action))
171: * {
172: * // your code
173: * return true;
174: * }
175: * else
176: * return false;
177: * </pre>
178: * @param CController $controller the controller
179: * @param CAction $action the action
180: * @return boolean whether the action should be executed.
181: */
182: public function beforeControllerAction($controller,$action)
183: {
184: if(($parent=$this->getParentModule())===null)
185: $parent=Yii::app();
186: return $parent->beforeControllerAction($controller,$action);
187: }
188:
189: /**
190: * The post-filter for controller actions.
191: * This method is invoked after the currently requested controller action and all its filters
192: * are executed. If you override this method, make sure you call the parent implementation at the end.
193: * @param CController $controller the controller
194: * @param CAction $action the action
195: */
196: public function afterControllerAction($controller,$action)
197: {
198: if(($parent=$this->getParentModule())===null)
199: $parent=Yii::app();
200: $parent->afterControllerAction($controller,$action);
201: }
202: }
203: