fixes #4858, self referencing components

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7133 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-06-06 16:18:14 +00:00
parent 52d72a63b5
commit 1c21457eef
2 changed files with 82 additions and 42 deletions

View file

@ -43,7 +43,7 @@ class Component extends Object {
* @var object
* @access private
*/
var $__loaded = array();
var $__loaded = array();
/**
* Settings for loaded components.
*
@ -155,7 +155,7 @@ class Component extends Object {
function _loadComponents(&$object, $parent = null) {
$components = $object->components;
$base = $this->__controllerVars['base'];
if (is_array($object->components)) {
$normal = Set::normalize($object->components);
foreach ($normal as $component => $config) {
@ -194,10 +194,10 @@ class Component extends Object {
return false;
}
}
if (isset($this->__loaded[$component])) {
$object->{$component} =& $this->__loaded[$component];
if (!empty($config) && isset($this->__settings[$component])) {
$this->__settings[$component] = array_merge($this->__settings[$component], $config);
} elseif (!empty($config)) {
@ -215,9 +215,9 @@ class Component extends Object {
$this->__settings[$component] = $config;
}
}
if (isset($object->{$component}->components) && is_array($object->{$component}->components)) {
$this->_loadComponents($object->{$component});
if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
$this->_loadComponents($object->{$component}, $component);
}
}
}

View file

@ -31,40 +31,40 @@ App::import('Core', array('Component', 'Controller'));
if (!class_exists('AppController')) {
/**
* AppController class
*
*
* @package cake
* @subpackage cake.tests.cases.libs.controller
*/
class AppController extends Controller {
/**
* name property
*
*
* @var string 'App'
* @access public
*/
var $name = 'App';
/**
* uses property
*
*
* @var array
* @access public
*/
var $uses = array();
/**
* helpers property
*
*
* @var array
* @access public
*/
var $helpers = array();
/**
* components property
*
*
* @var array
* @access public
*/
var $components = array('Orange' => array('colour' => 'blood orange'));
}
} else {
define('AppControllerExists', true);
@ -78,23 +78,23 @@ if (!class_exists('AppController')) {
class ParamTestComponent extends Object {
/**
* name property
*
*
* @var string 'ParamTest'
* @access public
*/
var $name = 'ParamTest';
/**
* components property
*
*
* @var array
* @access public
*/
var $components = array('Banana' => array('config' => 'value'));
/**
* initialize method
*
* @param mixed $controller
* @param mixed $settings
*
* @param mixed $controller
* @param mixed $settings
* @access public
* @return void
*/
@ -107,7 +107,7 @@ class ParamTestComponent extends Object {
}
}
}
}
/**
@ -119,14 +119,14 @@ class ParamTestComponent extends Object {
class ComponentTestController extends AppController {
/**
* name property
*
*
* @var string 'ComponentTest'
* @access public
*/
var $name = 'ComponentTest';
/**
* uses property
*
*
* @var array
* @access public
*/
@ -134,29 +134,29 @@ class ComponentTestController extends AppController {
}
/**
* AppleComponent class
*
*
* @package cake
* @subpackage cake.tests.cases.libs.controller
*/
class AppleComponent extends Object {
/**
* components property
*
*
* @var array
* @access public
*/
var $components = array('Orange');
/**
* testName property
*
*
* @var mixed null
* @access public
*/
var $testName = null;
/**
* startup method
*
* @param mixed $controller
*
* @param mixed $controller
* @access public
* @return void
*/
@ -167,22 +167,22 @@ class AppleComponent extends Object {
}
/**
* OrangeComponent class
*
*
* @package cake
* @subpackage cake.tests.cases.libs.controller
*/
class OrangeComponent extends Object {
/**
* components property
*
*
* @var array
* @access public
*/
var $components = array('Banana');
/**
* initialize method
*
* @param mixed $controller
*
* @param mixed $controller
* @access public
* @return void
*/
@ -193,30 +193,54 @@ class OrangeComponent extends Object {
}
/**
* BananaComponent class
*
*
* @package cake
* @subpackage cake.tests.cases.libs.controller
*/
class BananaComponent extends Object {
/**
* testField property
*
*
* @var string 'BananaField'
* @access public
*/
var $testField = 'BananaField';
}
/**
* MutuallyReferencingOneComponent class
*
* @package cake
* @subpackage cake.tests.cases.libs.controller
*/
class MutuallyReferencingOneComponent extends Object {
var $components = array('MutuallyReferencingTwo');
}
/**
* MutuallyReferencingTwoComponent class
*
* @package cake
* @subpackage cake.tests.cases.libs.controller
*/
class MutuallyReferencingTwoComponent extends Object {
var $components = array('MutuallyReferencingOne');
}
/**
* ComponentTest class
*
*
* @package cake
* @subpackage cake.tests.cases.libs.controller
*/
class ComponentTest extends CakeTestCase {
/**
* setUp method
*
*
* @access public
* @return void
*/
@ -225,7 +249,7 @@ class ComponentTest extends CakeTestCase {
}
/**
* testLoadComponents method
*
*
* @access public
* @return void
*/
@ -303,7 +327,7 @@ class ComponentTest extends CakeTestCase {
}
/**
* test a component being used more than once.
*
*
* @return void
*/
function testMultipleComponentInitialize() {
@ -321,16 +345,16 @@ class ComponentTest extends CakeTestCase {
* tests merging of component parameters and merging / construction of components.
*
* @return void
*/
*/
function testComponentsWithParams() {
$this->skipIf(defined('AppControllerExists'), 'Components with Params test will be skipped as it needs a non-existent AppController. As the an AppController class exists, this cannot be run.');
$Controller =& new ComponentTestController();
$Controller->components = array('ParamTest' => array('test' => 'value', 'flag'), 'Apple');
$Controller->constructClasses();
$Controller->Component->initialize($Controller);
$this->assertTrue(is_a($Controller->ParamTest, 'ParamTestComponent'));
$this->assertTrue(is_a($Controller->ParamTest->Banana, 'BananaComponent'));
$this->assertTrue(is_a($Controller->Orange, 'OrangeComponent'));
@ -344,9 +368,25 @@ class ComponentTest extends CakeTestCase {
$Controller->components = array('ParamTest' => array('test' => 'value'), 'Orange' => array('ripeness' => 'perfect'));
$Controller->constructClasses();
$Controller->Component->initialize($Controller);
$this->assertEqual($Controller->Orange->settings, array('colour' => 'blood orange', 'ripeness' => 'perfect'));
$this->assertEqual($Controller->ParamTest->test, 'value');
}
/**
* Test mutually referencing components.
*
*
*/
function testMutuallyReferencingComponents() {
$Controller =& new ComponentTestController();
$Controller->components = array('MutuallyReferencingOne');
$Controller->constructClasses();
$Controller->Component->initialize($Controller);
$this->assertTrue(is_a($Controller->MutuallyReferencingOne, 'MutuallyReferencingOneComponent'));
$this->assertTrue(is_a($Controller->MutuallyReferencingOne->MutuallyReferencingTwo, 'MutuallyReferencingTwoComponent'));
$this->assertTrue(is_a($Controller->MutuallyReferencingOne->MutuallyReferencingTwo->MutuallyReferencingOne, 'MutuallyReferencingOneComponent'));
}
}
?>
?>