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

namespace yii\base;

/**
 * Module is the base class for module and application classes.
 *
Qiang Xue committed
15
 * Module mainly manages application components and sub-modules that belongs to a module.
w  
Qiang Xue committed
16
 *
Qiang Xue committed
17 18
 * @property string $uniqueId An ID that uniquely identifies this module among all modules within
 * the current application.
Qiang Xue committed
19 20 21 22 23 24
 * @property string $basePath The root directory of the module. Defaults to the directory containing the module class.
 * @property array $modules The configuration of the currently installed modules (module ID => configuration).
 * @property array $components The application components (indexed by their IDs).
 * @property array $import List of aliases to be imported. This property is write-only.
 * @property array $aliases List of aliases to be defined. This property is write-only.
 *
w  
Qiang Xue committed
25 26 27
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
28
abstract class Module extends Component implements Initable
w  
Qiang Xue committed
29
{
m  
Qiang Xue committed
30 31 32 33
	/**
	 * @var array custom module parameters (name => value).
	 */
	public $params = array();
w  
Qiang Xue committed
34
	/**
Qiang Xue committed
35
	 * @var array the IDs of the application components that should be preloaded when this module is created.
w  
Qiang Xue committed
36 37
	 */
	public $preload = array();
Qiang Xue committed
38 39 40 41 42 43 44 45
	/**
	 * @var string an ID that uniquely identifies this module among other modules which have the same [[parent]].
	 */
	public $id;
	/**
	 * @var Module the parent module of this module. Null if this module does not have a parent.
	 */
	public $module;
w  
Qiang Xue committed
46 47 48 49 50 51 52 53

	private $_basePath;
	private $_modules = array();
	private $_components = array();

	/**
	 * Constructor.
	 * @param string $id the ID of this module
Qiang Xue committed
54
	 * @param Module $parent the parent module (if any)
w  
Qiang Xue committed
55
	 */
Qiang Xue committed
56
	public function __construct($id, $parent = null)
w  
Qiang Xue committed
57
	{
Qiang Xue committed
58 59
		$this->id = $id;
		$this->module = $parent;
w  
Qiang Xue committed
60 61 62 63 64 65 66 67 68 69 70
	}

	/**
	 * Getter magic method.
	 * This method is overridden to support accessing application components
	 * like reading module properties.
	 * @param string $name application component or property name
	 * @return mixed the named property value
	 */
	public function __get($name)
	{
w  
Qiang Xue committed
71
		if ($this->hasComponent($name)) {
w  
Qiang Xue committed
72
			return $this->getComponent($name);
Qiang Xue committed
73
		} else {
w  
Qiang Xue committed
74
			return parent::__get($name);
w  
Qiang Xue committed
75
		}
w  
Qiang Xue committed
76 77 78 79 80 81 82 83 84 85 86
	}

	/**
	 * Checks if a property value is null.
	 * This method overrides the parent implementation by checking
	 * if the named application component is loaded.
	 * @param string $name the property name or the event name
	 * @return boolean whether the property value is null
	 */
	public function __isset($name)
	{
w  
Qiang Xue committed
87
		if ($this->hasComponent($name)) {
w  
Qiang Xue committed
88
			return $this->getComponent($name) !== null;
Qiang Xue committed
89
		} else {
w  
Qiang Xue committed
90
			return parent::__isset($name);
w  
Qiang Xue committed
91 92 93 94
		}
	}

	/**
Qiang Xue committed
95 96
	 * Initializes the module.
	 * This method is called after the module is created and initialized with property values
.  
Qiang Xue committed
97 98
	 * given in configuration. The default implement will create a path alias using the module [[id]]
	 * and then call [[preloadComponents()]] to load components that are declared in [[preload]].
w  
Qiang Xue committed
99
	 */
Qiang Xue committed
100
	public function init()
w  
Qiang Xue committed
101
	{
Qiang Xue committed
102
		\Yii::setAlias('@' . $this->id, $this->getBasePath());
Qiang Xue committed
103
		$this->preloadComponents();
w  
Qiang Xue committed
104 105 106
	}

	/**
Qiang Xue committed
107 108
	 * Returns an ID that uniquely identifies this module among all modules within the current application.
	 * @return string the unique ID of the module.
w  
Qiang Xue committed
109
	 */
Qiang Xue committed
110
	public function getUniqueId()
w  
Qiang Xue committed
111
	{
Qiang Xue committed
112 113 114 115 116
		if ($this->module && !$this->module instanceof Application) {
			return $this->module->getUniqueId() . "/{$this->id}";
		} else {
			return $this->id;
		}
w  
Qiang Xue committed
117 118 119 120
	}

	/**
	 * Returns the root directory of the module.
Qiang Xue committed
121
	 * @return string the root directory of the module. Defaults to the directory containing the module class file.
w  
Qiang Xue committed
122 123 124
	 */
	public function getBasePath()
	{
m  
Qiang Xue committed
125
		if ($this->_basePath === null) {
Qiang Xue committed
126
			$class = new \ReflectionClass($this);
w  
Qiang Xue committed
127 128 129 130 131 132 133 134 135
			$this->_basePath = dirname($class->getFileName());
		}
		return $this->_basePath;
	}

	/**
	 * Sets the root directory of the module.
	 * This method can only be invoked at the beginning of the constructor.
	 * @param string $path the root directory of the module.
m  
Qiang Xue committed
136
	 * @throws Exception if the directory does not exist.
w  
Qiang Xue committed
137 138 139
	 */
	public function setBasePath($path)
	{
Qiang Xue committed
140 141
		$p = \Yii::getAlias($path);
		if ($p === false || !is_dir($p)) {
m  
Qiang Xue committed
142
			throw new Exception('Invalid base path: ' . $path);
Qiang Xue committed
143
		} else {
Qiang Xue committed
144
			$this->_basePath = realpath($p);
w  
Qiang Xue committed
145 146 147 148
		}
	}

	/**
m  
Qiang Xue committed
149
	 * Imports the specified path aliases.
Qiang Xue committed
150 151
	 * This method is provided so that you can import a set of path aliases when configuring a module.
	 * The path aliases will be imported by calling [[\Yii::import()]].
m  
Qiang Xue committed
152
	 * @param array $aliases list of path aliases to be imported
w  
Qiang Xue committed
153 154 155
	 */
	public function setImport($aliases)
	{
m  
Qiang Xue committed
156 157 158
		foreach ($aliases as $alias) {
			\Yii::import($alias);
		}
w  
Qiang Xue committed
159 160 161
	}

	/**
w  
Qiang Xue committed
162
	 * Defines path aliases.
Qiang Xue committed
163 164
	 * This method calls [[\Yii::setAlias()]] to register the path aliases.
	 * This method is provided so that you can define path aliases when configuring a module.
w  
Qiang Xue committed
165
	 * @param array $aliases list of path aliases to be defined. The array keys are alias names
Qiang Xue committed
166
	 * (must start with '@') and the array values are the corresponding paths or aliases.
w  
Qiang Xue committed
167
	 * For example,
w  
Qiang Xue committed
168 169
	 *
	 * ~~~
w  
Qiang Xue committed
170
	 * array(
Qiang Xue committed
171
	 *	'@models' => '@app/models', // an existing alias
Qiang Xue committed
172
	 *	'@backend' => __DIR__ . '/../backend',  // a directory
w  
Qiang Xue committed
173
	 * )
w  
Qiang Xue committed
174
	 * ~~~
w  
Qiang Xue committed
175
	 */
w  
Qiang Xue committed
176
	public function setAliases($aliases)
w  
Qiang Xue committed
177
	{
w  
Qiang Xue committed
178 179
		foreach ($aliases as $name => $alias) {
			\Yii::setAlias($name, $alias);
w  
Qiang Xue committed
180 181 182 183
		}
	}

	/**
Qiang Xue committed
184 185 186 187
	 * Checks whether the named module exists.
	 * @param string $id module ID
	 * @return boolean whether the named module exists. Both loaded and unloaded modules
	 * are considered.
w  
Qiang Xue committed
188
	 */
Qiang Xue committed
189
	public function hasModule($id)
w  
Qiang Xue committed
190
	{
Qiang Xue committed
191 192 193 194 195 196
		return isset($this->_modules[$id]);
	}

	/**
	 * Retrieves the named module.
	 * @param string $id module ID (case-sensitive)
197
	 * @param boolean $load whether to load the module if it is not yet loaded.
Qiang Xue committed
198 199 200 201
	 * @return Module|null the module instance, null if the module
	 * does not exist.
	 * @see hasModule()
	 */
202
	public function getModule($id, $load = true)
Qiang Xue committed
203 204 205 206
	{
		if (isset($this->_modules[$id])) {
			if ($this->_modules[$id] instanceof Module) {
				return $this->_modules[$id];
207
			} elseif ($load) {
Qiang Xue committed
208 209
				\Yii::trace("Loading \"$id\" module", __CLASS__);
				return $this->_modules[$id] = \Yii::createObject($this->_modules[$id], $id, $this);
w  
Qiang Xue committed
210 211
			}
		}
Qiang Xue committed
212
		return null;
w  
Qiang Xue committed
213 214 215
	}

	/**
Qiang Xue committed
216 217 218 219 220 221 222 223 224
	 * Adds a sub-module to this module.
	 * @param string $id module ID
	 * @param Module|array|null $module the sub-module to be added to this module. This can
	 * be one of the followings:
	 *
	 * - a [[Module]] object
	 * - a configuration array: when [[getModule()]] is called initially, the array
	 *   will be used to instantiate the sub-module
	 * - null: the named sub-module will be removed from this module
w  
Qiang Xue committed
225
	 */
Qiang Xue committed
226
	public function setModule($id, $module)
w  
Qiang Xue committed
227
	{
Qiang Xue committed
228 229 230 231 232
		if ($module === null) {
			unset($this->_modules[$id]);
		} else {
			$this->_modules[$id] = $module;
		}
w  
Qiang Xue committed
233 234 235
	}

	/**
Qiang Xue committed
236 237 238 239 240
	 * Returns the sub-modules in this module.
	 * @param boolean $loadedOnly whether to return the loaded sub-modules only. If this is set false,
	 * then all sub-modules registered in this module will be returned, whether they are loaded or not.
	 * Loaded modules will be returned as objects, while unloaded modules as configuration arrays.
	 * @return array the modules (indexed by their IDs)
w  
Qiang Xue committed
241
	 */
Qiang Xue committed
242
	public function getModules($loadedOnly = false)
w  
Qiang Xue committed
243
	{
Qiang Xue committed
244 245 246 247 248 249 250 251 252 253 254
		if ($loadedOnly) {
			$modules = array();
			foreach ($this->_modules as $module) {
				if ($module instanceof Module) {
					$modules[] = $module;
				}
			}
			return $modules;
		} else {
			return $this->_modules;
		}
w  
Qiang Xue committed
255 256 257
	}

	/**
Qiang Xue committed
258
	 * Registers sub-modules in the current module.
w  
Qiang Xue committed
259
	 *
Qiang Xue committed
260 261 262 263
	 * Each sub-module should be specified as a name-value pair, where
	 * name refers to the ID of the module and value the module or a configuration
	 * array that can be used to create the module. In the latter case, [[\Yii::createObject()]]
	 * will be used to create the module.
w  
Qiang Xue committed
264
	 *
Qiang Xue committed
265
	 * If a new sub-module has the same ID as an existing one, the existing one will be overwritten silently.
w  
Qiang Xue committed
266
	 *
Qiang Xue committed
267
	 * The following is an example for registering two sub-modules:
w  
Qiang Xue committed
268
	 *
Qiang Xue committed
269 270 271 272 273 274 275 276 277 278 279
	 * ~~~
	 * array(
	 *     'comment' => array(
	 *         'class' => 'app\modules\CommentModule',
	 *         'connectionID' => 'db',
	 *     ),
	 *     'booking' => array(
	 *         'class' => 'app\modules\BookingModule',
	 *     ),
	 * )
	 * ~~~
w  
Qiang Xue committed
280
	 *
Qiang Xue committed
281
	 * @param array $modules modules (id => module configuration or instances)
w  
Qiang Xue committed
282 283 284
	 */
	public function setModules($modules)
	{
Qiang Xue committed
285 286
		foreach ($modules as $id => $module) {
			$this->_modules[$id] = $module;
w  
Qiang Xue committed
287 288
		}
	}
289

w  
Qiang Xue committed
290 291 292
	/**
	 * Checks whether the named component exists.
	 * @param string $id application component ID
Qiang Xue committed
293 294
	 * @return boolean whether the named application component exists. Both loaded and unloaded components
	 * are considered.
w  
Qiang Xue committed
295 296 297
	 */
	public function hasComponent($id)
	{
Qiang Xue committed
298
		return isset($this->_components[$id]);
w  
Qiang Xue committed
299 300 301 302 303
	}

	/**
	 * Retrieves the named application component.
	 * @param string $id application component ID (case-sensitive)
304
	 * @param boolean $load whether to load the component if it is not yet loaded.
Qiang Xue committed
305 306 307
	 * @return ApplicationComponent|null the application component instance, null if the application component
	 * does not exist.
	 * @see hasComponent()
w  
Qiang Xue committed
308
	 */
309
	public function getComponent($id, $load = true)
w  
Qiang Xue committed
310
	{
Qiang Xue committed
311
		if (isset($this->_components[$id])) {
Qiang Xue committed
312 313
			if ($this->_components[$id] instanceof ApplicationComponent) {
				return $this->_components[$id];
314
			} elseif ($load) {
Qiang Xue committed
315 316
				\Yii::trace("Loading \"$id\" application component", __CLASS__);
				return $this->_components[$id] = \Yii::createObject($this->_components[$id]);
w  
Qiang Xue committed
317 318
			}
		}
Qiang Xue committed
319
		return null;
w  
Qiang Xue committed
320 321 322
	}

	/**
Qiang Xue committed
323
	 * Registers an application component in this module.
w  
Qiang Xue committed
324
	 * @param string $id component ID
Qiang Xue committed
325 326 327 328 329 330 331
	 * @param ApplicationComponent|array|null $component the component to be added to the module. This can
	 * be one of the followings:
	 *
	 * - an [[ApplicationComponent]] object
	 * - a configuration array: when [[getComponent()]] is called initially for this component, the array
	 *   will be used to instantiate the component
	 * - null: the named component will be removed from the module
w  
Qiang Xue committed
332 333 334
	 */
	public function setComponent($id, $component)
	{
Qiang Xue committed
335
		if ($component === null) {
w  
Qiang Xue committed
336
			unset($this->_components[$id]);
Qiang Xue committed
337
		} else {
w  
Qiang Xue committed
338 339 340 341 342 343 344 345 346 347 348
			$this->_components[$id] = $component;
		}
	}

	/**
	 * Returns the application components.
	 * @param boolean $loadedOnly whether to return the loaded components only. If this is set false,
	 * then all components specified in the configuration will be returned, whether they are loaded or not.
	 * Loaded components will be returned as objects, while unloaded components as configuration arrays.
	 * @return array the application components (indexed by their IDs)
	 */
Qiang Xue committed
349
	public function getComponents($loadedOnly = false)
w  
Qiang Xue committed
350
	{
Qiang Xue committed
351
		if ($loadedOnly) {
Qiang Xue committed
352 353 354 355 356 357 358
			$components = array();
			foreach ($this->_components as $component) {
				if ($component instanceof ApplicationComponent) {
					$components[] = $component;
				}
			}
			return $components;
Qiang Xue committed
359
		} else {
Qiang Xue committed
360
			return $this->_components;
Qiang Xue committed
361
		}
w  
Qiang Xue committed
362 363 364
	}

	/**
Qiang Xue committed
365
	 * Registers a set of application components in this module.
w  
Qiang Xue committed
366
	 *
Qiang Xue committed
367 368 369 370
	 * Each application component should be specified as a name-value pair, where
	 * name refers to the ID of the component and value the component or a configuration
	 * array that can be used to create the component. In the latter case, [[\Yii::createObject()]]
	 * will be used to create the component.
w  
Qiang Xue committed
371
	 *
Qiang Xue committed
372
	 * If a new component has the same ID as an existing one, the existing one will be overwritten silently.
w  
Qiang Xue committed
373
	 *
Qiang Xue committed
374 375 376
	 * The following is an example for setting two components:
	 *
	 * ~~~
w  
Qiang Xue committed
377
	 * array(
Qiang Xue committed
378 379 380 381 382 383 384 385
	 *     'db' => array(
	 *         'class' => 'yii\db\dao\Connection',
	 *         'dsn' => 'sqlite:path/to/file.db',
	 *     ),
	 *     'cache' => array(
	 *         'class' => 'yii\caching\DbCache',
	 *         'connectionID' => 'db',
	 *     ),
w  
Qiang Xue committed
386
	 * )
Qiang Xue committed
387
	 * ~~~
w  
Qiang Xue committed
388
	 *
Qiang Xue committed
389
	 * @param array $components application components (id => component configuration or instance)
w  
Qiang Xue committed
390
	 */
Qiang Xue committed
391
	public function setComponents($components)
w  
Qiang Xue committed
392
	{
Qiang Xue committed
393 394
		foreach ($components as $id => $component) {
			$this->_components[$id] = $component;
w  
Qiang Xue committed
395 396 397 398
		}
	}

	/**
Qiang Xue committed
399
	 * Loads application components that are declared in [[preload]].
w  
Qiang Xue committed
400
	 */
w  
Qiang Xue committed
401
	public function preloadComponents()
w  
Qiang Xue committed
402
	{
Qiang Xue committed
403
		foreach ($this->preload as $id) {
w  
Qiang Xue committed
404
			$this->getComponent($id);
Qiang Xue committed
405
		}
w  
Qiang Xue committed
406 407
	}
}