Module.php 14.6 KB
Newer Older
w  
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/**
 * Module class file.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @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.
 *
 * Module mainly manages application components and sub-modules.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
abstract class Module extends Component
{
m  
Qiang Xue committed
23 24 25 26
	/**
	 * @var array custom module parameters (name => value).
	 */
	public $params = array();
w  
Qiang Xue committed
27 28 29 30 31 32
	/**
	 * @var array the IDs of the application components that should be preloaded.
	 */
	public $preload = array();
	/**
	 * @var array the behaviors that should be attached to the module.
m  
Qiang Xue committed
33 34
	 * The behaviors will be attached to the module when [[init]] is called.
	 * Please refer to [[Model::behaviors]] on how to specify the value of this property.
w  
Qiang Xue committed
35 36 37
	 */
	public $behaviors = array();

w  
Qiang Xue committed
38
	private $_id;
w  
Qiang Xue committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	private $_parentModule;
	private $_basePath;
	private $_modulePath;
	private $_params;
	private $_modules = array();
	private $_moduleConfig = array();
	private $_components = array();
	private $_componentConfig = array();


	/**
	 * Constructor.
	 * @param string $id the ID of this module
	 * @param CModule $parent the parent module (if any)
	 * @param mixed $config the module configuration. It can be either an array or
	 * the path of a PHP file returning the configuration array.
	 */
	public function __construct($id, $parent, $config = null)
	{
		$this->_id = $id;
		$this->_parentModule = $parent;

		// set basePath at early as possible to avoid trouble
		if (is_string($config))
			$config = require($config);
		if (isset($config['basePath']))
		{
			$this->setBasePath($config['basePath']);
			unset($config['basePath']);
		}
		Yii::setPathOfAlias($id, $this->getBasePath());

		$this->preinit();

		$this->configure($config);
		$this->attachBehaviors($this->behaviors);
		$this->preloadComponents();

		$this->init();
	}

	/**
	 * 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
89
		if ($this->hasComponent($name)) {
w  
Qiang Xue committed
90
			return $this->getComponent($name);
w  
Qiang Xue committed
91 92
		}
		else {
w  
Qiang Xue committed
93
			return parent::__get($name);
w  
Qiang Xue committed
94
		}
w  
Qiang Xue committed
95 96 97 98 99 100 101 102 103 104 105
	}

	/**
	 * 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
106
		if ($this->hasComponent($name)) {
w  
Qiang Xue committed
107
			return $this->getComponent($name) !== null;
w  
Qiang Xue committed
108 109
		}
		else {
w  
Qiang Xue committed
110
			return parent::__isset($name);
w  
Qiang Xue committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		}
	}

	/**
	 * Returns a list of behaviors that this model should behave as.
	 * The return value of this method should be an array of behavior configurations
	 * indexed by behavior names. For more details, please refer to [[Model::behaviors]].
	 *
	 * The declared behaviors will be attached to the module when [[init]] is called.
	 * @return array the behavior configurations.
	 */
	public function behaviors()
	{
		return array();
	}

	/**
	 * 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)
	{
		$this->_id=$id;
w  
Qiang Xue committed
143 144 145 146 147 148 149 150
	}

	/**
	 * Returns the root directory of the module.
	 * @return string the root directory of the module. Defaults to the directory containing the module class.
	 */
	public function getBasePath()
	{
m  
Qiang Xue committed
151 152
		if ($this->_basePath === null) {
			$class = new ReflectionClass($this);
w  
Qiang Xue committed
153 154 155 156 157 158 159 160 161
			$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
162
	 * @throws Exception if the directory does not exist.
w  
Qiang Xue committed
163 164 165
	 */
	public function setBasePath($path)
	{
m  
Qiang Xue committed
166 167
		if (($this->_basePath = realpath($path)) === false || !is_dir($this->_basePath)) {
			throw new Exception('Invalid base path: ' . $path);
w  
Qiang Xue committed
168 169 170 171
		}
	}

	/**
m  
Qiang Xue committed
172 173
	 * Returns the directory that contains child modules.
	 * @return string the directory that contains child modules. Defaults to the `modules` subdirectory under [[basePath]].
w  
Qiang Xue committed
174 175 176
	 */
	public function getModulePath()
	{
m  
Qiang Xue committed
177
		if ($this->_modulePath !== null) {
w  
Qiang Xue committed
178
			return $this->_modulePath;
m  
Qiang Xue committed
179 180
		}
		else {
w  
Qiang Xue committed
181
			return $this->_modulePath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'modules';
m  
Qiang Xue committed
182
		}
w  
Qiang Xue committed
183 184 185
	}

	/**
m  
Qiang Xue committed
186 187 188
	 * Sets the directory that contains child modules.
	 * @param string $value the directory that contains child modules.
	 * @throws Exception if the directory is invalid
w  
Qiang Xue committed
189 190 191
	 */
	public function setModulePath($value)
	{
m  
Qiang Xue committed
192 193 194
		if (($this->_modulePath = realpath($value)) === false || !is_dir($this->_modulePath)) {
			throw new Exception('Invalid module path: ' . $value);
		}
w  
Qiang Xue committed
195 196 197
	}

	/**
m  
Qiang Xue committed
198 199 200
	 * Imports the specified path aliases.
	 * This method is provided so that you can import a set of path aliases by module configuration.
	 * @param array $aliases list of path aliases to be imported
w  
Qiang Xue committed
201 202 203
	 */
	public function setImport($aliases)
	{
m  
Qiang Xue committed
204 205 206
		foreach ($aliases as $alias) {
			\Yii::import($alias);
		}
w  
Qiang Xue committed
207 208 209
	}

	/**
w  
Qiang Xue committed
210 211 212 213 214
	 * Defines path aliases.
	 * This method calls [[\Yii::setPathOfAlias]] to register the path aliases.
	 * This method is provided so that you can define path aliases by module configuration.
	 * @param array $aliases list of path aliases to be defined. The array keys are alias names
	 * (must start with '@') while the array values are the corresponding paths or aliases.
w  
Qiang Xue committed
215
	 * For example,
w  
Qiang Xue committed
216 217
	 *
	 * ~~~
w  
Qiang Xue committed
218
	 * array(
w  
Qiang Xue committed
219 220
	 *    '@models' => '@app/models',             // an existing alias
	 *    '@backend' => __DIR__ . '/../backend',  // a directory
w  
Qiang Xue committed
221
	 * )
w  
Qiang Xue committed
222
	 * ~~~
w  
Qiang Xue committed
223
	 */
w  
Qiang Xue committed
224
	public function setAliases($aliases)
w  
Qiang Xue committed
225
	{
w  
Qiang Xue committed
226 227
		foreach ($aliases as $name => $alias) {
			\Yii::setAlias($name, $alias);
w  
Qiang Xue committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		}
	}

	/**
	 * Returns the parent module.
	 * @return CModule the parent module. Null if this module does not have a parent.
	 */
	public function getParentModule()
	{
		return $this->_parentModule;
	}

	/**
	 * Retrieves the named application module.
	 * The module has to be declared in {@link modules}. A new instance will be created
	 * when calling this method with the given ID for the first time.
	 * @param string $id application module ID (case-sensitive)
	 * @return CModule the module instance, null if the module is disabled or does not exist.
	 */
	public function getModule($id)
	{
		if (isset($this->_modules[$id]) || array_key_exists($id, $this->_modules))
			return $this->_modules[$id];
		elseif (isset($this->_moduleConfig[$id]))
		{
			$config = $this->_moduleConfig[$id];
			if (!isset($config['enabled']) || $config['enabled'])
			{
Alexander Makarov committed
256
				\Yii::trace("Loading \"$id\" module", 'system.base.CModule');
w  
Qiang Xue committed
257 258
				$class = $config['class'];
				unset($config['class'], $config['enabled']);
Alexander Makarov committed
259 260
				if ($this === \Yii::$app)
				{
261
					$module = Yii::create($class, $id, null, $config);
Alexander Makarov committed
262
				}
w  
Qiang Xue committed
263
				else
Alexander Makarov committed
264
				{
265
					$module = Yii::create($class, $this->getId() . '/' . $id, $this, $config);
Alexander Makarov committed
266
				}
w  
Qiang Xue committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
				return $this->_modules[$id] = $module;
			}
		}
	}

	/**
	 * Returns a value indicating whether the specified module is installed.
	 * @param string $id the module ID
	 * @return boolean whether the specified module is installed.
	 */
	public function hasModule($id)
	{
		return isset($this->_moduleConfig[$id]) || isset($this->_modules[$id]);
	}

	/**
	 * Returns the configuration of the currently installed modules.
	 * @return array the configuration of the currently installed modules (module ID => configuration)
	 */
	public function getModules()
	{
		return $this->_moduleConfig;
	}

	/**
	 * Configures the sub-modules of this module.
	 *
	 * Call this method to declare sub-modules and configure them with their initial property values.
	 * The parameter should be an array of module configurations. Each array element represents a single module,
	 * which can be either a string representing the module ID or an ID-configuration pair representing
	 * a module with the specified ID and the initial property values.
	 *
	 * For example, the following array declares two modules:
	 * <pre>
	 * array(
	 *     'admin',                // a single module ID
	 *     'payment'=>array(       // ID-configuration pair
	 *         'server'=>'paymentserver.com',
	 *     ),
	 * )
	 * </pre>
	 *
	 * By default, the module class is determined using the expression <code>ucfirst($moduleID).'Module'</code>.
	 * And the class file is located under <code>modules/$moduleID</code>.
	 * You may override this default by explicitly specifying the 'class' option in the configuration.
	 *
	 * You may also enable or disable a module by specifying the 'enabled' option in the configuration.
	 *
	 * @param array $modules module configurations.
	 */
	public function setModules($modules)
	{
		foreach ($modules as $id => $module)
		{
			if (is_int($id))
			{
				$id = $module;
				$module = array();
			}
			if (!isset($module['class']))
			{
				Yii::setPathOfAlias($id, $this->getModulePath() . DIRECTORY_SEPARATOR . $id);
				$module['class'] = $id . '.' . ucfirst($id) . 'Module';
			}

			if (isset($this->_moduleConfig[$id]))
				$this->_moduleConfig[$id] = CMap::mergeArray($this->_moduleConfig[$id], $module);
			else
				$this->_moduleConfig[$id] = $module;
		}
	}

	/**
	 * Checks whether the named component exists.
	 * @param string $id application component ID
	 * @return boolean whether the named application component exists (including both loaded and disabled.)
	 */
	public function hasComponent($id)
	{
		return isset($this->_components[$id]) || isset($this->_componentConfig[$id]);
	}

	/**
	 * Retrieves the named application component.
	 * @param string $id application component ID (case-sensitive)
	 * @param boolean $createIfNull whether to create the component if it doesn't exist yet. This parameter
	 * has been available since version 1.0.6.
	 * @return IApplicationComponent the application component instance, null if the application component is disabled or does not exist.
	 * @see hasComponent
	 */
	public function getComponent($id, $createIfNull = true)
	{
		if (isset($this->_components[$id]))
			return $this->_components[$id];
		elseif (isset($this->_componentConfig[$id]) && $createIfNull)
		{
			$config = $this->_componentConfig[$id];
			if (!isset($config['enabled']) || $config['enabled'])
			{
Alexander Makarov committed
366
				\Yii::trace("Loading \"$id\" application component", 'system.CModule');
w  
Qiang Xue committed
367
				unset($config['enabled']);
Alexander Makarov committed
368
				$component = \Yii::create($config);
w  
Qiang Xue committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
				return $this->_components[$id] = $component;
			}
		}
	}

	/**
	 * Puts a component under the management of the module.
	 * The component will be initialized by calling its {@link CApplicationComponent::init() init()}
	 * method if it has not done so.
	 * @param string $id component ID
	 * @param IApplicationComponent $component the component to be added to the module.
	 * If this parameter is null, it will unload the component from the module.
	 */
	public function setComponent($id, $component)
	{
		if ($component === null)
			unset($this->_components[$id]);
		else
		{
			$this->_components[$id] = $component;
			if (!$component->getIsInitialized())
				$component->init();
		}
	}

	/**
	 * 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.
	 * This parameter has been available since version 1.1.3.
	 * @return array the application components (indexed by their IDs)
	 */
	public function getComponents($loadedOnly = true)
	{
		if ($loadedOnly)
			return $this->_components;
		else
			return array_merge($this->_componentConfig, $this->_components);
	}

	/**
	 * Sets the application components.
	 *
	 * When a configuration is used to specify a component, it should consist of
	 * the component's initial property values (name-value pairs). Additionally,
	 * a component can be enabled (default) or disabled by specifying the 'enabled' value
	 * in the configuration.
	 *
	 * If a configuration is specified with an ID that is the same as an existing
	 * component or configuration, the existing one will be replaced silently.
	 *
	 * The following is the configuration for two components:
	 * <pre>
	 * array(
	 *     'db'=>array(
	 *         'class'=>'CDbConnection',
	 *         'connectionString'=>'sqlite:path/to/file.db',
	 *     ),
	 *     'cache'=>array(
	 *         'class'=>'CDbCache',
	 *         'connectionID'=>'db',
	 *         'enabled'=>!YII_DEBUG,  // enable caching in non-debug mode
	 *     ),
	 * )
	 * </pre>
	 *
	 * @param array $components application components(id=>component configuration or instances)
	 * @param boolean $merge whether to merge the new component configuration with the existing one.
	 * Defaults to true, meaning the previously registered component configuration of the same ID
	 * will be merged with the new configuration. If false, the existing configuration will be replaced completely.
	 */
	public function setComponents($components, $merge = true)
	{
		foreach ($components as $id => $component)
		{
			if ($component instanceof IApplicationComponent)
				$this->setComponent($id, $component);
			elseif (isset($this->_componentConfig[$id]) && $merge)
				$this->_componentConfig[$id] = CMap::mergeArray($this->_componentConfig[$id], $component);
			else
				$this->_componentConfig[$id] = $component;
		}
	}

	/**
	 * Configures the module with the specified configuration.
	 * @param array $config the configuration array
	 */
	public function configure($config)
	{
		if (is_array($config))
		{
			foreach ($config as $key => $value)
				$this->$key = $value;
		}
	}

	/**
	 * Loads static application components.
	 */
w  
Qiang Xue committed
470
	public function preloadComponents()
w  
Qiang Xue committed
471 472 473 474 475 476 477 478 479 480 481 482
	{
		foreach ($this->preload as $id)
			$this->getComponent($id);
	}

	/**
	 * Preinitializes the module.
	 * This method is called at the beginning of the module constructor.
	 * You may override this method to do some customized preinitialization work.
	 * Note that at this moment, the module is not configured yet.
	 * @see init
	 */
w  
Qiang Xue committed
483
	public function preinit()
w  
Qiang Xue committed
484 485 486 487 488 489 490 491 492 493
	{
	}

	/**
	 * Initializes the module.
	 * This method is called at the end of the module constructor.
	 * Note that at this moment, the module has been configured, the behaviors
	 * have been attached and the application components have been registered.
	 * @see preinit
	 */
w  
Qiang Xue committed
494
	public function init()
w  
Qiang Xue committed
495 496 497
	{
	}
}