Skip to content

Commit e2fa220

Browse files
committed
Merge pull request yiisoft#1491 from creocoder/cmodule-set-components-enh-2
[READY] CModule::setComponents() proper fix 2
2 parents 81a7ed3 + 12ed272 commit e2fa220

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Version 1.1.13 work in progress
5454
- Enh: Allow CDataProvider to use custom pagination and sorter (creocoder)
5555
- Enh: Value of the CHtml::activeTextArea() can now be set through $htmlOptions['value'] (resurtm)
5656
- Enh: Allow to customize CHtml::error() container tag (creocoder)
57+
- Enh: CModule::setComponents() now can reconfigure already loaded components (creocoder)
5758
- Chg: MSSQL unit tests updated and actualized, added SQLSRV driver support (resurtm)
5859
- Chg: Added Oracle unit tests (resurtm)
5960
- Chg: Updated CHttpCacheFilter to use dates as specified by RFC 1123 (bramp)

framework/base/CModule.php

+42-6
Original file line numberDiff line numberDiff line change
@@ -459,14 +459,50 @@ public function getComponents($loadedOnly=true)
459459
*/
460460
public function setComponents($components,$merge=true)
461461
{
462-
foreach($components as $id=>$component)
462+
foreach($components as $id=>$config)
463463
{
464-
if($component instanceof IApplicationComponent)
465-
$this->setComponent($id,$component);
466-
else if(isset($this->_componentConfig[$id]) && $merge)
467-
$this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
464+
if($config instanceof IApplicationComponent)
465+
{
466+
$this->setComponent($id,$config);
467+
continue;
468+
}
469+
470+
if(isset($this->_components[$id]))
471+
{
472+
if(isset($config['class']))
473+
{
474+
if(get_class($this->_components[$id])!==$config['class'])
475+
{
476+
unset($this->_components[$id]);
477+
$this->_componentConfig[$id]=$config; //we should ignore merge here
478+
continue;
479+
}
480+
481+
$class=$config['class'];
482+
unset($config['class']);
483+
484+
foreach($config as $key=>$value)
485+
$this->_components[$id]->$key=$value;
486+
487+
$config['class']=$class;
488+
}
489+
else
490+
{
491+
foreach($config as $key=>$value)
492+
$this->_components[$id]->$key=$value;
493+
}
494+
}
495+
elseif(isset($this->_componentConfig[$id]['class'],$config['class'])
496+
&& $this->_componentConfig[$id]['class']!==$config['class'])
497+
{
498+
$this->_componentConfig[$id]=$config; //we should ignore merge here
499+
continue;
500+
}
501+
502+
if(isset($this->_componentConfig[$id]) && $merge)
503+
$this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$config);
468504
else
469-
$this->_componentConfig[$id]=$component;
505+
$this->_componentConfig[$id]=$config;
470506
}
471507
}
472508

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
class AnotherNewApplicationComponent extends CApplicationComponent {
3+
private $_text='new';
4+
5+
public function getText()
6+
{
7+
return $this->_text;
8+
}
9+
10+
public function setText($value)
11+
{
12+
return $this->_text=$value;
13+
}
14+
}

tests/framework/base/CModuleTest.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
require_once dirname(__FILE__) . '/NewModule.php';
33
require_once dirname(__FILE__) . '/NewApplicationComponent.php';
4+
require_once dirname(__FILE__) . '/AnotherNewApplicationComponent.php';
45

56
class CModuleTest extends CTestCase {
67
protected $parent;
@@ -83,10 +84,37 @@ public function testSetComponents() {
8384
public function testSetComponentsViaConfig() {
8485
$this->mod = new NewModule('foo',$this->parent,array(
8586
'components' => array(
86-
'bar' => array('class' => 'NewApplicationComponent')
87-
)
87+
'bar' => array('class' => 'NewApplicationComponent'),
88+
),
8889
));
8990
$this->assertEquals('hello world',$this->mod->bar->getText('hello world'));
91+
$this->mod->setComponents(array(
92+
'bar' => array('text' => 'test'),
93+
));
94+
$this->assertEquals('test',$this->mod->bar->getText());
95+
$this->mod->setComponent('bar',null);
96+
$this->assertEquals('test',$this->mod->bar->getText());
97+
$this->mod->setComponents(array(
98+
'bar' => array('class' => 'NewApplicationComponent'),
99+
));
100+
$this->assertEquals('test',$this->mod->bar->getText());
101+
$this->mod->setComponents(array(
102+
'bar' => array('class' => 'AnotherNewApplicationComponent'),
103+
));
104+
$this->assertEquals('new',$this->mod->bar->getText());
105+
$this->mod->setComponent('bar',null);
106+
$this->assertEquals('new',$this->mod->bar->getText());
107+
$this->mod->setComponent('bar',null);
108+
$this->mod->setComponents(array(
109+
'bar' => array(
110+
'class' => 'NewApplicationComponent',
111+
'text' => 'test',
112+
),
113+
));
114+
$this->mod->setComponents(array(
115+
'bar' => array('class' => 'AnotherNewApplicationComponent'),
116+
));
117+
$this->assertEquals('new',$this->mod->bar->getText());
90118
}
91119
public function testSetAliases() {
92120
$this->mod->setAliases(array('modules' => $this->d));

0 commit comments

Comments
 (0)