Yii2 Bootstrap 3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

498 lines
14 KiB

14 years ago
<?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
{
/**
14 years ago
* @var array custom module parameters (name => value).
*/
public $params = array();
/**
14 years ago
* @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.
14 years ago
* 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.
14 years ago
*/
public $behaviors = array();
14 years ago
private $_id;
14 years ago
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
13 years ago
if (is_string($config)) {
14 years ago
$config = require($config);
13 years ago
}
if (isset($config['basePath'])) {
14 years ago
$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)
{
14 years ago
if ($this->hasComponent($name)) {
14 years ago
return $this->getComponent($name);
13 years ago
} else {
14 years ago
return parent::__get($name);
14 years ago
}
14 years ago
}
/**
* 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)
{
14 years ago
if ($this->hasComponent($name)) {
14 years ago
return $this->getComponent($name) !== null;
13 years ago
} else {
14 years ago
return parent::__isset($name);
14 years ago
}
}
/**
* 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)
{
13 years ago
$this->_id = $id;
14 years ago
}
/**
* 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()
{
14 years ago
if ($this->_basePath === null) {
$class = new ReflectionClass($this);
14 years ago
$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.
14 years ago
* @throws Exception if the directory does not exist.
14 years ago
*/
public function setBasePath($path)
{
14 years ago
if (($this->_basePath = realpath($path)) === false || !is_dir($this->_basePath)) {
throw new Exception('Invalid base path: ' . $path);
14 years ago
}
}
/**
14 years ago
* Returns the directory that contains child modules.
* @return string the directory that contains child modules. Defaults to the `modules` subdirectory under [[basePath]].
14 years ago
*/
public function getModulePath()
{
14 years ago
if ($this->_modulePath !== null) {
14 years ago
return $this->_modulePath;
13 years ago
} else {
14 years ago
return $this->_modulePath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'modules';
14 years ago
}
14 years ago
}
/**
14 years ago
* Sets the directory that contains child modules.
* @param string $value the directory that contains child modules.
* @throws Exception if the directory is invalid
14 years ago
*/
public function setModulePath($value)
{
14 years ago
if (($this->_modulePath = realpath($value)) === false || !is_dir($this->_modulePath)) {
throw new Exception('Invalid module path: ' . $value);
}
14 years ago
}
/**
14 years ago
* 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
14 years ago
*/
public function setImport($aliases)
{
14 years ago
foreach ($aliases as $alias) {
\Yii::import($alias);
}
14 years ago
}
/**
14 years ago
* 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.
14 years ago
* For example,
14 years ago
*
* ~~~
14 years ago
* array(
13 years ago
* '@models' => '@app/models', // an existing alias
* '@backend' => __DIR__ . '/../backend', // a directory
14 years ago
* )
14 years ago
* ~~~
14 years ago
*/
14 years ago
public function setAliases($aliases)
14 years ago
{
14 years ago
foreach ($aliases as $name => $alias) {
\Yii::setAlias($name, $alias);
14 years ago
}
}
/**
* 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)
{
13 years ago
if (isset($this->_modules[$id]) || array_key_exists($id, $this->_modules)) {
14 years ago
return $this->_modules[$id];
13 years ago
} elseif (isset($this->_moduleConfig[$id]))
14 years ago
{
$config = $this->_moduleConfig[$id];
13 years ago
if (!isset($config['enabled']) || $config['enabled']) {
\Yii::trace("Loading \"$id\" module", 'system.base.CModule');
14 years ago
$class = $config['class'];
unset($config['class'], $config['enabled']);
13 years ago
if ($this === \Yii::$app) {
$module = \Yii::createObject($class, $id, null, $config);
13 years ago
} else
{
13 years ago
$module = \Yii::createObject($class, $this->getId() . '/' . $id, $this, $config);
}
14 years ago
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(
13 years ago
* 'admin', // a single module ID
* 'payment'=>array( // ID-configuration pair
* 'server'=>'paymentserver.com',
* ),
14 years ago
* )
* </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)
{
13 years ago
if (is_int($id)) {
14 years ago
$id = $module;
$module = array();
}
13 years ago
if (!isset($module['class'])) {
14 years ago
Yii::setPathOfAlias($id, $this->getModulePath() . DIRECTORY_SEPARATOR . $id);
$module['class'] = $id . '.' . ucfirst($id) . 'Module';
}
13 years ago
if (isset($this->_moduleConfig[$id])) {
14 years ago
$this->_moduleConfig[$id] = CMap::mergeArray($this->_moduleConfig[$id], $module);
13 years ago
} else
13 years ago
{
14 years ago
$this->_moduleConfig[$id] = $module;
13 years ago
}
14 years ago
}
}
/**
* 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)
{
13 years ago
if (isset($this->_components[$id])) {
14 years ago
return $this->_components[$id];
13 years ago
} elseif (isset($this->_componentConfig[$id]) && $createIfNull)
14 years ago
{
$config = $this->_componentConfig[$id];
13 years ago
if (!isset($config['enabled']) || $config['enabled']) {
\Yii::trace("Loading \"$id\" application component", 'system.CModule');
14 years ago
unset($config['enabled']);
13 years ago
$component = \Yii::createObject($config);
14 years ago
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)
{
13 years ago
if ($component === null) {
14 years ago
unset($this->_components[$id]);
13 years ago
} else {
14 years ago
$this->_components[$id] = $component;
13 years ago
if (!$component->getIsInitialized()) {
14 years ago
$component->init();
13 years ago
}
14 years ago
}
}
/**
* 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)
{
13 years ago
if ($loadedOnly) {
14 years ago
return $this->_components;
13 years ago
} else {
14 years ago
return array_merge($this->_componentConfig, $this->_components);
13 years ago
}
14 years ago
}
/**
* 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(
13 years ago
* '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
* ),
14 years ago
* )
* </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)
{
13 years ago
if ($component instanceof IApplicationComponent) {
14 years ago
$this->setComponent($id, $component);
13 years ago
} elseif (isset($this->_componentConfig[$id]) && $merge)
13 years ago
{
14 years ago
$this->_componentConfig[$id] = CMap::mergeArray($this->_componentConfig[$id], $component);
13 years ago
} else
13 years ago
{
14 years ago
$this->_componentConfig[$id] = $component;
13 years ago
}
14 years ago
}
}
/**
* Configures the module with the specified configuration.
* @param array $config the configuration array
*/
public function configure($config)
{
13 years ago
if (is_array($config)) {
14 years ago
foreach ($config as $key => $value)
13 years ago
{
14 years ago
$this->$key = $value;
13 years ago
}
14 years ago
}
}
/**
* Loads static application components.
*/
14 years ago
public function preloadComponents()
14 years ago
{
foreach ($this->preload as $id)
13 years ago
{
14 years ago
$this->getComponent($id);
13 years ago
}
14 years ago
}
/**
* 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
*/
14 years ago
public function preinit()
14 years ago
{
}
/**
* 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
*/
14 years ago
public function init()
14 years ago
{
}
}