New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[READY] CModule::setComponents() proper fix 2 #1491

Merged
merged 6 commits into from Sep 29, 2012
View
@@ -53,6 +53,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
@@ -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));
ProTip! Use n and p to navigate between commits in a pull request.