Module.php 12 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 19 20 21 22 23 24
 * @property string $id The module ID.
 * @property string $basePath The root directory of the module. Defaults to the directory containing the module class.
 * @property Module|null $parentModule The parent module. Null if this module does not have a parent.
 * @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 38
	 */
	public $preload = array();

w  
Qiang Xue committed
39
	private $_id;
w  
Qiang Xue committed
40
	private $_basePath;
Qiang Xue committed
41
	private $_parentModule;
w  
Qiang Xue committed
42 43 44 45 46 47
	private $_modules = array();
	private $_components = array();

	/**
	 * Constructor.
	 * @param string $id the ID of this module
Qiang Xue committed
48
	 * @param Module $parent the parent module (if any)
w  
Qiang Xue committed
49
	 */
Qiang Xue committed
50
	public function __construct($id, $parent = null)
w  
Qiang Xue committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
	{
		$this->_id = $id;
		$this->_parentModule = $parent;
	}

	/**
	 * 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
65
		if ($this->hasComponent($name)) {
w  
Qiang Xue committed
66
			return $this->getComponent($name);
Qiang Xue committed
67
		} else {
w  
Qiang Xue committed
68
			return parent::__get($name);
w  
Qiang Xue committed
69
		}
w  
Qiang Xue committed
70 71 72 73 74 75 76 77 78 79 80
	}

	/**
	 * 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
81
		if ($this->hasComponent($name)) {
w  
Qiang Xue committed
82
			return $this->getComponent($name) !== null;
Qiang Xue committed
83
		} else {
w  
Qiang Xue committed
84
			return parent::__isset($name);
w  
Qiang Xue committed
85 86 87 88
		}
	}

	/**
Qiang Xue committed
89 90
	 * Initializes the module.
	 * This method is called after the module is created and initialized with property values
.  
Qiang Xue committed
91 92
	 * 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
93
	 */
Qiang Xue committed
94
	public function init()
w  
Qiang Xue committed
95
	{
Qiang Xue committed
96 97
		\Yii::setAlias('@' . $this->getId(), $this->getBasePath());
		$this->preloadComponents();
w  
Qiang Xue committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	}

	/**
	 * Returns the module ID.
	 * @return string the module ID.
	 */
	public function getId()
	{
		return $this->_id;
	}

	/**
	 * Sets the module ID.
	 * @param string $id the module ID
	 */
	public function setId($id)
	{
Qiang Xue committed
115
		$this->_id = $id;
w  
Qiang Xue committed
116 117 118 119
	}

	/**
	 * Returns the root directory of the module.
Qiang Xue committed
120
	 * @return string the root directory of the module. Defaults to the directory containing the module class file.
w  
Qiang Xue committed
121 122 123
	 */
	public function getBasePath()
	{
m  
Qiang Xue committed
124
		if ($this->_basePath === null) {
Qiang Xue committed
125
			$class = new \ReflectionClass($this);
w  
Qiang Xue committed
126 127 128 129 130 131 132 133 134
			$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
135
	 * @throws Exception if the directory does not exist.
w  
Qiang Xue committed
136 137 138
	 */
	public function setBasePath($path)
	{
Qiang Xue committed
139 140
		$p = \Yii::getAlias($path);
		if ($p === false || !is_dir($p)) {
m  
Qiang Xue committed
141
			throw new Exception('Invalid base path: ' . $path);
Qiang Xue committed
142
		} else {
Qiang Xue committed
143
			$this->_basePath = realpath($p);
w  
Qiang Xue committed
144 145 146 147
		}
	}

	/**
m  
Qiang Xue committed
148
	 * Imports the specified path aliases.
Qiang Xue committed
149 150
	 * 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
151
	 * @param array $aliases list of path aliases to be imported
w  
Qiang Xue committed
152 153 154
	 */
	public function setImport($aliases)
	{
m  
Qiang Xue committed
155 156 157
		foreach ($aliases as $alias) {
			\Yii::import($alias);
		}
w  
Qiang Xue committed
158 159 160
	}

	/**
w  
Qiang Xue committed
161
	 * Defines path aliases.
Qiang Xue committed
162 163
	 * 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
164
	 * @param array $aliases list of path aliases to be defined. The array keys are alias names
Qiang Xue committed
165
	 * (must start with '@') and the array values are the corresponding paths or aliases.
w  
Qiang Xue committed
166
	 * For example,
w  
Qiang Xue committed
167 168
	 *
	 * ~~~
w  
Qiang Xue committed
169
	 * array(
Qiang Xue committed
170
	 *	'@models' => '@app/models', // an existing alias
Qiang Xue committed
171
	 *	'@backend' => __DIR__ . '/../backend',  // a directory
w  
Qiang Xue committed
172
	 * )
w  
Qiang Xue committed
173
	 * ~~~
w  
Qiang Xue committed
174
	 */
w  
Qiang Xue committed
175
	public function setAliases($aliases)
w  
Qiang Xue committed
176
	{
w  
Qiang Xue committed
177 178
		foreach ($aliases as $name => $alias) {
			\Yii::setAlias($name, $alias);
w  
Qiang Xue committed
179 180 181 182 183
		}
	}

	/**
	 * Returns the parent module.
Qiang Xue committed
184
	 * @return Module|null the parent module. Null is returned if this module does not have a parent.
w  
Qiang Xue committed
185 186 187 188 189 190 191
	 */
	public function getParentModule()
	{
		return $this->_parentModule;
	}

	/**
Qiang Xue committed
192 193 194 195
	 * 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
196
	 */
Qiang Xue committed
197
	public function hasModule($id)
w  
Qiang Xue committed
198
	{
Qiang Xue committed
199 200 201 202 203 204
		return isset($this->_modules[$id]);
	}

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

	/**
Qiang Xue committed
224 225 226 227 228 229 230 231 232
	 * 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
233
	 */
Qiang Xue committed
234
	public function setModule($id, $module)
w  
Qiang Xue committed
235
	{
Qiang Xue committed
236 237 238 239 240
		if ($module === null) {
			unset($this->_modules[$id]);
		} else {
			$this->_modules[$id] = $module;
		}
w  
Qiang Xue committed
241 242 243
	}

	/**
Qiang Xue committed
244 245 246 247 248
	 * 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
249
	 */
Qiang Xue committed
250
	public function getModules($loadedOnly = false)
w  
Qiang Xue committed
251
	{
Qiang Xue committed
252 253 254 255 256 257 258 259 260 261 262
		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
263 264 265
	}

	/**
Qiang Xue committed
266
	 * Registers sub-modules in the current module.
w  
Qiang Xue committed
267
	 *
Qiang Xue committed
268 269 270 271
	 * 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
272
	 *
Qiang Xue committed
273
	 * If a new sub-module has the same ID as an existing one, the existing one will be overwritten silently.
w  
Qiang Xue committed
274
	 *
Qiang Xue committed
275
	 * The following is an example for registering two sub-modules:
w  
Qiang Xue committed
276
	 *
Qiang Xue committed
277 278 279 280 281 282 283 284 285 286 287
	 * ~~~
	 * array(
	 *     'comment' => array(
	 *         'class' => 'app\modules\CommentModule',
	 *         'connectionID' => 'db',
	 *     ),
	 *     'booking' => array(
	 *         'class' => 'app\modules\BookingModule',
	 *     ),
	 * )
	 * ~~~
w  
Qiang Xue committed
288
	 *
Qiang Xue committed
289
	 * @param array $modules modules (id => module configuration or instances)
w  
Qiang Xue committed
290 291 292
	 */
	public function setModules($modules)
	{
Qiang Xue committed
293 294
		foreach ($modules as $id => $module) {
			$this->_modules[$id] = $module;
w  
Qiang Xue committed
295 296
		}
	}
297

w  
Qiang Xue committed
298 299 300
	/**
	 * Checks whether the named component exists.
	 * @param string $id application component ID
Qiang Xue committed
301 302
	 * @return boolean whether the named application component exists. Both loaded and unloaded components
	 * are considered.
w  
Qiang Xue committed
303 304 305
	 */
	public function hasComponent($id)
	{
Qiang Xue committed
306
		return isset($this->_components[$id]);
w  
Qiang Xue committed
307 308 309 310 311
	}

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

	/**
Qiang Xue committed
331
	 * Registers an application component in this module.
w  
Qiang Xue committed
332
	 * @param string $id component ID
Qiang Xue committed
333 334 335 336 337 338 339
	 * @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
340 341 342
	 */
	public function setComponent($id, $component)
	{
Qiang Xue committed
343
		if ($component === null) {
w  
Qiang Xue committed
344
			unset($this->_components[$id]);
Qiang Xue committed
345
		} else {
w  
Qiang Xue committed
346 347 348 349 350 351 352 353 354 355 356
			$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
357
	public function getComponents($loadedOnly = false)
w  
Qiang Xue committed
358
	{
Qiang Xue committed
359
		if ($loadedOnly) {
Qiang Xue committed
360 361 362 363 364 365 366
			$components = array();
			foreach ($this->_components as $component) {
				if ($component instanceof ApplicationComponent) {
					$components[] = $component;
				}
			}
			return $components;
Qiang Xue committed
367
		} else {
Qiang Xue committed
368
			return $this->_components;
Qiang Xue committed
369
		}
w  
Qiang Xue committed
370 371 372
	}

	/**
Qiang Xue committed
373
	 * Registers a set of application components in this module.
w  
Qiang Xue committed
374
	 *
Qiang Xue committed
375 376 377 378
	 * 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
379
	 *
Qiang Xue committed
380
	 * If a new component has the same ID as an existing one, the existing one will be overwritten silently.
w  
Qiang Xue committed
381
	 *
Qiang Xue committed
382 383 384
	 * The following is an example for setting two components:
	 *
	 * ~~~
w  
Qiang Xue committed
385
	 * array(
Qiang Xue committed
386 387 388 389 390 391 392 393
	 *     '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
394
	 * )
Qiang Xue committed
395
	 * ~~~
w  
Qiang Xue committed
396
	 *
Qiang Xue committed
397
	 * @param array $components application components (id => component configuration or instance)
w  
Qiang Xue committed
398
	 */
Qiang Xue committed
399
	public function setComponents($components)
w  
Qiang Xue committed
400
	{
Qiang Xue committed
401 402
		foreach ($components as $id => $component) {
			$this->_components[$id] = $component;
w  
Qiang Xue committed
403 404 405 406
		}
	}

	/**
Qiang Xue committed
407
	 * Loads application components that are declared in [[preload]].
w  
Qiang Xue committed
408
	 */
w  
Qiang Xue committed
409
	public function preloadComponents()
w  
Qiang Xue committed
410
	{
Qiang Xue committed
411
		foreach ($this->preload as $id) {
w  
Qiang Xue committed
412
			$this->getComponent($id);
Qiang Xue committed
413
		}
w  
Qiang Xue committed
414 415
	}
}