Yii Framework v1.1.10 绫诲弬鑰

CWidgetFactory

system.web
缁ф壙 class CWidgetFactory » CApplicationComponent » CComponent
瀹炵幇 IApplicationComponent, IWidgetFactory
婧愯嚜 1.1
鐗堟湰 $Id: CWidgetFactory.php 3421 2011-10-20 21:23:38Z alexander.makarow $
婧愮爜 framework/web/CWidgetFactory.php
CWidgetFactory creates new widgets to be used in views.

CWidgetFactory is used as the default "widgetFactory" application component.

When calling CBaseController::createWidget, CBaseController::widget or CBaseController::beginWidget, if the "widgetFactory" component is installed, it will be used to create the requested widget. To install the "widgetFactory" component, we should have the following application configuration:
return array(
    'components'=>array(
        'widgetFactory'=>array(
            'class'=>'CWidgetFactory',
        ),
    ),
)


CWidgetFactory implements the "skin" feature, which allows a new widget to be created and initialized with a set of predefined property values (called skin).

When CWidgetFactory is used to create a new widget, it will first instantiate the widget instance. It then checks if there is a skin available for this widget according to the widget class name and the widget CWidget::skin property. If a skin is found, it will be merged with the initial properties passed via createWidget. Then the merged initial properties will be used to initialize the newly created widget instance.

As aforementioned, a skin is a set of initial property values for a widget. It is thus represented as an associative array of name-value pairs. Skins are stored in PHP scripts like other configurations. Each script file stores the skins for a particular widget type and is named as the widget class name (e.g. CLinkPager.php). Each widget type may have one or several skins, identified by the skin name set via CWidget::skin property. If the CWidget::skin property is not set for a given widget, it means the default skin would be used. The following shows the possible skins for the CLinkPager widget:
return array(
    'default'=>array(
        'nextPageLabel'=>'>>',
        'prevPageLabel'=>'<<',
    ),
    'short'=>array(
        'header'=>'',
        'maxButtonCount'=>5,
    ),
);
In the above, there are two skins. The first one is the default skin which is indexed by the string "default". Note that CWidget::skin defaults to "default". Therefore, this is the skin that will be applied if we do not explicitly specify the CWidget::skin property. The second one is named as the "short" skin which will be used only when we set CWidget::skin to be "short".

By default, CWidgetFactory looks for the skin of a widget under the "skins" directory of the current application's CWebApplication::viewPath (e.g. protected/views/skins). If a theme is being used, it will look for the skin under the "skins" directory of the theme's CTheme::viewPath (as well as the aforementioned skin directory). In case the specified skin is not found, a widget will still be created normally without causing any error.

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
behaviors array 杩欎釜搴旂敤缁勪欢闄勫姞鐨勮涓恒 杩欐琛屼负灏嗗湪搴旂敤缁勪欢璋冪敤init鏃堕檮鍔犲湪搴旂敤缁勪欢涓娿 璇峰弬鐓CModel::behaviors濡備綍鎸囧畾姝ゅ睘鎬у笺 CApplicationComponent
enableSkin boolean whether to enable widget skinning. CWidgetFactory
isInitialized boolean 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 CApplicationComponent
skinPath string the directory containing all the skin files. CWidgetFactory
skinnableWidgets array list of widget class names that can be skinned. CWidgetFactory
widgets array widget initial property values. CWidgetFactory

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
createWidget() Creates a new widget based on the given class name and initial properties. CWidgetFactory
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getIsInitialized() 妫鏌ュ簲鐢ㄧ粍浠舵槸鍚﹀凡缁忓垵濮嬪寲銆 CApplicationComponent
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() Initializes the application component. CWidgetFactory
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
getSkin() Returns the skin for the specified widget class and skin name. CWidgetFactory

灞炴ц缁

enableSkin 灞炴 锛堝彲鐢ㄨ嚜 v1.1.3锛
public boolean $enableSkin;

whether to enable widget skinning. Defaults to false.

鍙傝

skinPath 灞炴
public string $skinPath;

the directory containing all the skin files. Defaults to null, meaning using the "skins" directory under the current application's CWebApplication::viewPath.

skinnableWidgets 灞炴 锛堝彲鐢ㄨ嚜 v1.1.3锛
public array $skinnableWidgets;

list of widget class names that can be skinned. Because skinning widgets has performance impact, you may want to specify this property to limit skinning only to specific widgets. Any widgets that are not in this list will not be skinned. Defaults to null, meaning all widgets can be skinned.

widgets 灞炴 锛堝彲鐢ㄨ嚜 v1.1.3锛
public array $widgets;

widget initial property values. Each array key-value pair represents the initial property values for a single widget class, with the array key being the widget class name, and array value being the initial property value array. For example,

array(
    'CLinkPager'=>array(
        'maxButtonCount'=>5,
        'cssFile'=>false,
    ),
    'CJuiDatePicker'=>array(
        'language'=>'ru',
    ),
)


Note that the initial values specified here may be overridden by the values given in CBaseController::createWidget calls. They may also be overridden by widget skins, if enableSkin is true.

鏂规硶璇︾粏

createWidget() 鏂规硶
public CWidget createWidget(CBaseController $owner, string $className, array $properties=array ( ))
$owner CBaseController the owner of the new widget
$className string the class name of the widget. This can also be a path alias (e.g. system.web.widgets.COutputCache)
$properties array the initial property values (name=>value) of the widget.
{return} CWidget the newly created widget whose properties have been initialized with the given values.
婧愮爜锛 framework/web/CWidgetFactory.php#145 (鏄剧ず)
public function createWidget($owner,$className,$properties=array())
{
    
$className=Yii::import($className,true);
    
$widget=new $className($owner);

    if(isset(
$this->widgets[$className]))
        
$properties=$properties===array() ? $this->widgets[$className] : CMap::mergeArray($this->widgets[$className],$properties);
    if(
$this->enableSkin)
    {
        if(
$this->skinnableWidgets===null || in_array($className,$this->skinnableWidgets))
        {
            
$skinName=isset($properties['skin']) ? $properties['skin'] : 'default';
            if(
$skinName!==false && ($skin=$this->getSkin($className,$skinName))!==array())
                
$properties=$properties===array() ? $skin CMap::mergeArray($skin,$properties);
        }
    }
    foreach(
$properties as $name=>$value)
        
$widget->$name=$value;
    return 
$widget;
}

Creates a new widget based on the given class name and initial properties.

getSkin() 鏂规硶
protected array getSkin(string $className, string $skinName)
$className string the widget class name
$skinName string the widget skin name
{return} array the skin (name=>value) for the widget
婧愮爜锛 framework/web/CWidgetFactory.php#172 (鏄剧ず)
protected function getSkin($className,$skinName)
{
    if(!isset(
$this->_skins[$className][$skinName]))
    {
        
$skinFile=$this->skinPath.DIRECTORY_SEPARATOR.$className.'.php';
        if(
is_file($skinFile))
            
$this->_skins[$className]=require($skinFile);
        else
            
$this->_skins[$className]=array();

        if((
$theme=Yii::app()->getTheme())!==null)
        {
            
$skinFile=$theme->getSkinPath().DIRECTORY_SEPARATOR.$className.'.php';
            if(
is_file($skinFile))
            {
                
$skins=require($skinFile);
                foreach(
$skins as $name=>$skin)
                    
$this->_skins[$className][$name]=$skin;
            }
        }

        if(!isset(
$this->_skins[$className][$skinName]))
            
$this->_skins[$className][$skinName]=array();
    }
    return 
$this->_skins[$className][$skinName];
}

Returns the skin for the specified widget class and skin name.

init() 鏂规硶
public void init()
婧愮爜锛 framework/web/CWidgetFactory.php#130 (鏄剧ず)
public function init()
{
    
parent::init();

    if(
$this->enableSkin && $this->skinPath===null)
        
$this->skinPath=Yii::app()->getViewPath().DIRECTORY_SEPARATOR.'skins';
}

Initializes the application component. This method overrides the parent implementation by resolving the skin path.

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.