Skip to content
Permalink
Browse files

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

[READY] CModule::setComponents() proper fix 2
  • Loading branch information...
qiangxue committed Sep 29, 2012
2 parents 81a7ed3 + 12ed272 commit e2fa2201ec86f8e197d560deba0b3d23340ef2a7
@@ -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)
@@ -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;
}
}
@@ -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;
}
}
@@ -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));

0 comments on commit e2fa220

Please sign in to comment.
You can’t perform that action at this time.