Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1491 from creocoder/cmodule-set-components-enh-2

[READY] CModule::setComponents() proper fix 2
  • Loading branch information...
commit e2fa2201ec86f8e197d560deba0b3d23340ef2a7 2 parents 81a7ed3 + 12ed272
@qiangxue qiangxue authored
View
1  CHANGELOG
@@ -54,6 +54,7 @@ Version 1.1.13 work in progress
- Enh: Allow CDataProvider to use custom pagination and sorter (creocoder)
- Enh: Value of the CHtml::activeTextArea() can now be set through $htmlOptions['value'] (resurtm)
- Enh: Allow to customize CHtml::error() container tag (creocoder)
+- Enh: CModule::setComponents() now can reconfigure already loaded components (creocoder)
- Chg: MSSQL unit tests updated and actualized, added SQLSRV driver support (resurtm)
- Chg: Added Oracle unit tests (resurtm)
- Chg: Updated CHttpCacheFilter to use dates as specified by RFC 1123 (bramp)
View
48 framework/base/CModule.php
@@ -459,14 +459,50 @@ public function getComponents($loadedOnly=true)
*/
public function setComponents($components,$merge=true)
{
- foreach($components as $id=>$component)
+ foreach($components as $id=>$config)
{
- if($component instanceof IApplicationComponent)
- $this->setComponent($id,$component);
- else if(isset($this->_componentConfig[$id]) && $merge)
- $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
+ if($config instanceof IApplicationComponent)
+ {
+ $this->setComponent($id,$config);
+ continue;
+ }
+
+ if(isset($this->_components[$id]))
+ {
+ if(isset($config['class']))
+ {
+ if(get_class($this->_components[$id])!==$config['class'])
+ {
+ unset($this->_components[$id]);
+ $this->_componentConfig[$id]=$config; //we should ignore merge here
+ continue;
+ }
+
+ $class=$config['class'];
+ unset($config['class']);
+
+ foreach($config as $key=>$value)
+ $this->_components[$id]->$key=$value;
+
+ $config['class']=$class;
+ }
+ else
+ {
+ foreach($config as $key=>$value)
+ $this->_components[$id]->$key=$value;
+ }
+ }
+ elseif(isset($this->_componentConfig[$id]['class'],$config['class'])
+ && $this->_componentConfig[$id]['class']!==$config['class'])
+ {
+ $this->_componentConfig[$id]=$config; //we should ignore merge here
+ continue;
+ }
+
+ if(isset($this->_componentConfig[$id]) && $merge)
+ $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$config);
else
- $this->_componentConfig[$id]=$component;
+ $this->_componentConfig[$id]=$config;
}
}
View
14 tests/framework/base/AnotherNewApplicationComponent.php
@@ -0,0 +1,14 @@
+<?php
+class AnotherNewApplicationComponent extends CApplicationComponent {
+ private $_text='new';
+
+ public function getText()
+ {
+ return $this->_text;
+ }
+
+ public function setText($value)
+ {
+ return $this->_text=$value;
+ }
+}
View
32 tests/framework/base/CModuleTest.php
@@ -1,6 +1,7 @@
<?php
require_once dirname(__FILE__) . '/NewModule.php';
require_once dirname(__FILE__) . '/NewApplicationComponent.php';
+require_once dirname(__FILE__) . '/AnotherNewApplicationComponent.php';
class CModuleTest extends CTestCase {
protected $parent;
@@ -83,10 +84,37 @@ public function testSetComponents() {
public function testSetComponentsViaConfig() {
$this->mod = new NewModule('foo',$this->parent,array(
'components' => array(
- 'bar' => array('class' => 'NewApplicationComponent')
- )
+ 'bar' => array('class' => 'NewApplicationComponent'),
+ ),
));
$this->assertEquals('hello world',$this->mod->bar->getText('hello world'));
+ $this->mod->setComponents(array(
+ 'bar' => array('text' => 'test'),
+ ));
+ $this->assertEquals('test',$this->mod->bar->getText());
+ $this->mod->setComponent('bar',null);
+ $this->assertEquals('test',$this->mod->bar->getText());
+ $this->mod->setComponents(array(
+ 'bar' => array('class' => 'NewApplicationComponent'),
+ ));
+ $this->assertEquals('test',$this->mod->bar->getText());
+ $this->mod->setComponents(array(
+ 'bar' => array('class' => 'AnotherNewApplicationComponent'),
+ ));
+ $this->assertEquals('new',$this->mod->bar->getText());
+ $this->mod->setComponent('bar',null);
+ $this->assertEquals('new',$this->mod->bar->getText());
+ $this->mod->setComponent('bar',null);
+ $this->mod->setComponents(array(
+ 'bar' => array(
+ 'class' => 'NewApplicationComponent',
+ 'text' => 'test',
+ ),
+ ));
+ $this->mod->setComponents(array(
+ 'bar' => array('class' => 'AnotherNewApplicationComponent'),
+ ));
+ $this->assertEquals('new',$this->mod->bar->getText());
}
public function testSetAliases() {
$this->mod->setAliases(array('modules' => $this->d));
Please sign in to comment.
Something went wrong with that request. Please try again.