CModule

优质
小牛编辑
131浏览
2023-12-01
所有包 | 属性 | 方法
system.base
继承abstract class CModule » CComponent
子类CApplication, CWebModule
版本$Id: CModule.php 3515 2011-12-28 12:29:24Z mdomba $
源码framework/base/CModule.php
CModule是模块和应用程序类的基类。

CModule主要管理应用组件和子模块。

公共属性

隐藏继承属性

属性类型描述定义在
basePathstring返回模块根目录。CModule
behaviorsarraythe behaviors that should be attached to the module.CModule
componentsarray返回应用组件。CModule
idstring返回模块ID。CModule
modulePathstring返回包含应用程序模块的目录。CModule
modulesarray返回应用程序当前已经安装的模块。CModule
paramsCAttributeCollection返回用户定义的参数。CModule
parentModuleCModule返回父模块。CModule
preloadarraythe IDs of the application components that should be preloaded.CModule

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()构造方法。CModule
__get()getter魔术方法。CModule
__isset()检查一个属性值是否为null。CModule
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
configure()为模块指定配置。CModule
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getBasePath()返回模块根目录。CModule
getComponent()Retrieves the named application component.CModule
getComponents()返回应用组件。CModule
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getId()返回模块ID。CModule
getModule()检索指定的应用模块。CModule
getModulePath()返回包含应用程序模块的目录。CModule
getModules()返回应用程序当前已经安装的模块。CModule
getParams()返回用户定义的参数。CModule
getParentModule()返回父模块。CModule
hasComponent()检查是否存在指定组件。CModule
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasModule()检查是否已经安装了指定模块。CModule
hasProperty()确定属性是否被定义。CComponent
raiseEvent()发起一个事件。CComponent
setAliases()定义根目录的别名。CModule
setBasePath()设置块根目录。CModule
setComponent()添加一个组件到模块中CModule
setComponents()设置应用组件。CModule
setId()设置模块ID。CModule
setImport()设置在模块中使用的路径别名。CModule
setModulePath()设置包含应用程序模块的目录。CModule
setModules()配置模块的子模块。CModule
setParams()设置用户定义的参数。CModule

受保护方法

隐藏继承方法

方法描述定义在
init()初始化模块。CModule
preinit()模块预初始化。CModule
preloadComponents()加载静态应用组件。CModule

属性详细

basePath 属性 public string getBasePath()
public void setBasePath(string $path)

返回模块根目录。

behaviors 属性 public array $behaviors;

the behaviors that should be attached to the module. The behaviors will be attached to the module when init is called. Please refer to CModel::behaviors on how to specify the value of this property.

components 属性 public array getComponents(boolean $loadedOnly=true)
public void setComponents(array $components, boolean $merge=true)

返回应用组件。

id 属性 public string getId()
public void setId(string $id)

返回模块ID。

modulePath 属性 public string getModulePath()
public void setModulePath(string $value)

返回包含应用程序模块的目录。

modules 属性 public array getModules()
public void setModules(array $modules)

返回应用程序当前已经安装的模块。

params 属性 public CAttributeCollection getParams()
public void setParams(array $value)

返回用户定义的参数。

parentModule 属性 只读 public CModule getParentModule()

返回父模块。

preload 属性 public array $preload;

the IDs of the application components that should be preloaded.

方法详细

__construct() 方法
public void __construct(string $id, CModule $parent, mixed $config=NULL)
$idstringthe ID of this module
$parentCModulethe parent module (if any)
$configmixedthe module configuration. It can be either an array or the path of a PHP file returning the configuration array.
源码: framework/base/CModule.php#70 (显示) publicfunction__construct($id,$parent,$config=null)
{
$this->_id=$id;
$this->_parentModule=$parent;

//setbasePathatearlyaspossibletoavoidtrouble
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();
}

构造方法。

__get() 方法
public mixed __get(string $name)
$namestringapplication component or property name
{return}mixedthe named property value
源码: framework/base/CModule.php#101 (显示) publicfunction__get($name)
{
if($this->hasComponent($name))
return$this->getComponent($name);
else
returnparent::__get($name);
}

getter魔术方法。 This method is overridden to support accessing application components like reading module properties.

__isset() 方法
public boolean __isset(string $name)
$namestringthe property name or the event name
{return}booleanwhether the property value is null
源码: framework/base/CModule.php#116 (显示) publicfunction__isset($name)
{
if($this->hasComponent($name))
return$this->getComponent($name)!==null;
else
returnparent::__isset($name);
}

检查一个属性值是否为null。 This method overrides the parent implementation by checking if the named application component is loaded.

configure() 方法
public void configure(array $config)
$configarraythe configuration array
源码: framework/base/CModule.php#478 (显示) publicfunctionconfigure($config)
{
if(is_array($config))
{
foreach($configas$key=>$value)
$this->$key=$value;
}
}

为模块指定配置。

getBasePath() 方法
public string getBasePath()
{return}stringthe root directory of the module. Defaults to the directory containing the module class.
源码: framework/base/CModule.php#146 (显示) publicfunctiongetBasePath()
{
if($this->_basePath===null)
{
$class=newReflectionClass(get_class($this));
$this->_basePath=dirname($class->getFileName());
}
return$this->_basePath;
}

返回模块根目录。

getComponent() 方法
public IApplicationComponent getComponent(string $id, boolean $createIfNull=true)
$idstringapplication component ID (case-sensitive)
$createIfNullbooleanwhether to create the component if it doesn't exist yet.
{return}IApplicationComponentthe application component instance, null if the application component is disabled or does not exist.
源码: framework/base/CModule.php#376 (显示) publicfunctiongetComponent($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'])
{
Yii::trace("Loading"$id"applicationcomponent",'system.CModule');
unset($config['enabled']);
$component=Yii::createComponent($config);
$component->init();
return$this->_components[$id]=$component;
}
}
}

Retrieves the named application component.

参见

  • hasComponent
getComponents() 方法
public array getComponents(boolean $loadedOnly=true)
$loadedOnlybooleanwhether 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}arraythe application components (indexed by their IDs)
源码: framework/base/CModule.php#422 (显示) publicfunctiongetComponents($loadedOnly=true)
{
if($loadedOnly)
return$this->_components;
else
returnarray_merge($this->_componentConfig,$this->_components);
}

返回应用组件。

getId() 方法
public string getId()
{return}stringthe module ID.
源码: framework/base/CModule.php#128 (显示) publicfunctiongetId()
{
return$this->_id;
}

返回模块ID。

getModule() 方法
public CModule getModule(string $id)
$idstringapplication module ID (case-sensitive)
{return}CModulethe module instance, null if the module is disabled or does not exist.
源码: framework/base/CModule.php#270 (显示) publicfunctiongetModule($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'])
{
Yii::trace("Loading"$id"module",'system.base.CModule');
$class=$config['class'];
unset($config['class'],$config['enabled']);
if($this===Yii::app())
$module=Yii::createComponent($class,$id,null,$config);
else
$module=Yii::createComponent($class,$this->getId().'/'.$id,$this,$config);
return$this->_modules[$id]=$module;
}
}
}

检索指定的应用模块。 The module has to be declared in modules. A new instance will be created when calling this method with the given ID for the first time.

getModulePath() 方法
public string getModulePath()
{return}stringthe directory that contains the application modules. Defaults to the 'modules' subdirectory of basePath.
源码: framework/base/CModule.php#200 (显示) publicfunctiongetModulePath()
{
if($this->_modulePath!==null)
return$this->_modulePath;
else
return$this->_modulePath=$this->getBasePath().DIRECTORY_SEPARATOR.'modules';
}

返回包含应用程序模块的目录。

getModules() 方法
public array getModules()
{return}arraythe configuration of the currently installed modules (module ID => configuration)
源码: framework/base/CModule.php#306 (显示) publicfunctiongetModules()
{
return$this->_moduleConfig;
}

返回应用程序当前已经安装的模块。

getParams() 方法
public CAttributeCollection getParams()
{return}CAttributeCollectionthe list of user-defined parameters
源码: framework/base/CModule.php#173 (显示) publicfunctiongetParams()
{
if($this->_params!==null)
return$this->_params;
else
{
$this->_params=newCAttributeCollection;
$this->_params->caseSensitive=true;
return$this->_params;
}
}

返回用户定义的参数。

getParentModule() 方法
public CModule getParentModule()
{return}CModulethe parent module. Null if this module does not have a parent.
源码: framework/base/CModule.php#258 (显示) publicfunctiongetParentModule()
{
return$this->_parentModule;
}

返回父模块。

hasComponent() 方法
public boolean hasComponent(string $id)
$idstringapplication component ID
{return}booleanwhether the named application component exists (including both loaded and disabled.)
源码: framework/base/CModule.php#364 (显示) publicfunctionhasComponent($id)
{
returnisset($this->_components[$id])||isset($this->_componentConfig[$id]);
}

检查是否存在指定组件。

hasModule() 方法 (可用自 v1.1.2)
public boolean hasModule(string $id)
$idstringthe module ID
{return}booleanwhether the specified module is installed.
源码: framework/base/CModule.php#297 (显示) publicfunctionhasModule($id)
{
returnisset($this->_moduleConfig[$id])||isset($this->_modules[$id]);
}

检查是否已经安装了指定模块。

init() 方法
protected void init()
源码: framework/base/CModule.php#514 (显示) protectedfunctioninit()
{
}

初始化模块。 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.

参见

  • preinit
preinit() 方法
protected void preinit()
源码: framework/base/CModule.php#503 (显示) protectedfunctionpreinit()
{
}

模块预初始化。 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.

参见

  • init
preloadComponents() 方法
protected void preloadComponents()
源码: framework/base/CModule.php#490 (显示) protectedfunctionpreloadComponents()
{
foreach($this->preloadas$id)
$this->getComponent($id);
}

加载静态应用组件。

setAliases() 方法
public void setAliases(array $mappings)
$mappingsarraylist of aliases to be defined. The array keys are root aliases, while the array values are paths or aliases corresponding to the root aliases. For example,
array( 'models'=>'application.models',              // an existing alias 'extensions'=>'application.extensions',      // an existing alias 'backend'=>dirname(__FILE__).'/../backend',  // a directory
)
源码: framework/base/CModule.php#243 (显示) publicfunctionsetAliases($mappings)
{
foreach($mappingsas$name=>$alias)
{
if(($path=Yii::getPathOfAlias($alias))!==false)
Yii::setPathOfAlias($name,$path);
else
Yii::setPathOfAlias($name,$alias);
}
}

定义根目录的别名。

setBasePath() 方法
public void setBasePath(string $path)
$pathstringthe root directory of the module.
源码: framework/base/CModule.php#162 (显示) publicfunctionsetBasePath($path)
{
if(($this->_basePath=realpath($path))===false||!is_dir($this->_basePath))
thrownewCException(Yii::t('yii','Basepath"{path}"isnotavaliddirectory.',
array('{path}'=>$path)));
}

设置块根目录。 This method can only be invoked at the beginning of the constructor.

setComponent() 方法
public void setComponent(string $id, IApplicationComponent $component)
$idstringcomponent ID
$componentIApplicationComponentthe component to be added to the module. If this parameter is null, it will unload the component from the module.
源码: framework/base/CModule.php#402 (显示) publicfunctionsetComponent($id,$component)
{
if($component===null)
unset($this->_components[$id]);
else
{
$this->_components[$id]=$component;
if(!$component->getIsInitialized())
$component->init();
}
}

添加一个组件到模块中 The component will be initialized by calling its init() method if it has not done so.

setComponents() 方法
public void setComponents(array $components, boolean $merge=true)
$componentsarrayapplication components(id=>component configuration or instances)
$mergebooleanwhether 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.
源码: framework/base/CModule.php#461 (显示) publicfunctionsetComponents($components,$merge=true)
{
foreach($componentsas$id=>$component)
{
if($componentinstanceofIApplicationComponent)
$this->setComponent($id,$component);
elseif(isset($this->_componentConfig[$id])&&$merge)
$this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
else
$this->_componentConfig[$id]=$component;
}
}

设置应用组件。

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:

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  ),
)

setId() 方法
public void setId(string $id)
$idstringthe module ID
源码: framework/base/CModule.php#137 (显示) publicfunctionsetId($id)
{
$this->_id=$id;
}

设置模块ID。

setImport() 方法
public void setImport(array $aliases)
$aliasesarraylist of aliases to be imported
源码: framework/base/CModule.php#224 (显示) publicfunctionsetImport($aliases)
{
foreach($aliasesas$alias)
Yii::import($alias);
}

设置在模块中使用的路径别名。

setModulePath() 方法
public void setModulePath(string $value)
$valuestringthe directory that contains the application modules.
源码: framework/base/CModule.php#213 (显示) publicfunctionsetModulePath($value)
{
if(($this->_modulePath=realpath($value))===false||!is_dir($this->_modulePath))
thrownewCException(Yii::t('yii','Themodulepath"{path}"isnotavaliddirectory.',
array('{path}'=>$value)));
}

设置包含应用程序模块的目录。

setModules() 方法
public void setModules(array $modules)
$modulesarraymodule configurations.
源码: framework/base/CModule.php#337 (显示) publicfunctionsetModules($modules)
{
foreach($modulesas$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;
}
}

配置模块的子模块。

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:

array(  'admin',                // a single module ID  'payment'=>array(       // ID-configuration pair      'server'=>'paymentserver.com',  ),
)


By default, the module class is determined using the expression ucfirst($moduleID).'Module'. And the class file is located under modules/$moduleID. 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.

setParams() 方法
public void setParams(array $value)
$valuearrayuser-defined parameters. This should be in name-value pairs.
源码: framework/base/CModule.php#189 (显示) publicfunctionsetParams($value)
{
$params=$this->getParams();
foreach($valueas$k=>$v)
$params->add($k,$v);
}

设置用户定义的参数。