update loading of components, fixes #3899, tests added

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6368 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-01-12 17:35:12 +00:00
parent ce1b042843
commit e989625316
4 changed files with 110 additions and 12 deletions

View file

@ -74,11 +74,6 @@ class Component extends Object {
foreach (array_keys($loaded) as $component) {
$tempComponent =& $loaded[$component];
if (isset($tempComponent->components) && is_array($tempComponent->components)) {
foreach ($tempComponent->components as $subComponent) {
$this->controller->{$component}->{$subComponent} =& $loaded[$subComponent];
}
}
if (is_callable(array($tempComponent, 'initialize'))) {
$tempComponent->initialize($controller);
}
@ -93,7 +88,7 @@ class Component extends Object {
* @return array Components loaded
* @access protected
*/
function &_loadComponents(&$loaded, $components) {
function &_loadComponents(&$loaded, $components, $parent = null) {
foreach ($components as $component) {
$parts = preg_split('/\/|\./', $component);
@ -131,17 +126,25 @@ class Component extends Object {
$base = null;
if ($componentCn == 'SessionComponent') {
$this->controller->{$component} =& new $componentCn($this->controller->base);
} else {
} elseif ($parent === null) {
$this->controller->{$component} =& new $componentCn();
$loaded[$component] =& $this->controller->{$component};
} elseif ($parent !== null) {
$this->controller->{$parent}->{$component} =& new $componentCn();
$loaded[$component] =& $this->controller->{$parent}->{$component};
}
$loaded[$component] =& $this->controller->{$component};
if (isset($this->controller->{$component}->components) && is_array($this->controller->{$component}->components)) {
$loaded =& $this->_loadComponents($loaded, $this->controller->{$component}->components);
$loaded =& $this->_loadComponents($loaded, $this->controller->{$component}->components, $component);
}
}
if (isset($loaded[$parent])) {
$loaded[$parent]->{$component} =& $loaded[$component];
}
}
return $loaded;
}
}

View file

@ -26,17 +26,47 @@
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
uses('controller' . DS . 'component');
uses('controller' . DS . 'component', 'controller' . DS . 'app_controller');
/**
* Short description for class.
*
* @package cake.tests
* @subpackage cake.tests.cases.libs.controller
*/
class ComponentTestController extends AppController {
var $name = 'ComponentTestController';
var $uses = array();
}
class ComponentTest extends CakeTestCase {
function skip() {
$this->skipif (true, 'ComponentTest not implemented');
function setUp() {
Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
$this->Controller = new ComponentTestController();
}
function tearDown() {
unset($this->Controller);
}
function testLoadComponents() {
$this->Controller->components = array('RequestHandler');
$Component = new Component($this->Controller);
$loaded = array();
$result = $Component->init($this->Controller);
$this->assertTrue(is_object($this->Controller->RequestHandler));
$this->Controller->plugin = 'test_plugin';
$this->Controller->components = array('RequestHandler', 'TestPluginComponent');
$result = $Component->init($this->Controller);
$this->assertTrue(is_object($this->Controller->RequestHandler));
$this->assertTrue(is_object($this->Controller->TestPluginComponent));
$this->assertTrue(is_object($this->Controller->TestPluginComponent->TestPluginOtherComponent));
$this->assertFalse(isset($this->Controller->TestPluginOtherComponent));
}
}
?>

View file

@ -0,0 +1,33 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @subpackage cake.tests.test_app.plugins.test_plugin.views.helpers
* @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
class TestPluginComponentComponent extends Object {
var $components = array('TestPlugin.TestPluginOtherComponent');
}
?>

View file

@ -0,0 +1,32 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2008, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @subpackage cake.tests.test_app.plugins.test_plugin.views.helpers
* @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
class TestPluginOtherComponentComponent extends Object {
}
?>