Adding a reference of the controller to the component collection, so components can easily access the controller in their constructor.

This commit is contained in:
mark_story 2010-09-14 22:52:51 -04:00
parent 7a14d3a8f2
commit 0ef76eb69a
2 changed files with 31 additions and 0 deletions

View file

@ -20,6 +20,13 @@ App::import('Core', 'ObjectCollection');
class ComponentCollection extends ObjectCollection {
/**
* The controller that this collection was initialized with.
*
* @var Controller
*/
protected $_Controller = null;
/**
* Initializes all the Components for a controller.
* Attaches a reference of each component to the Controller.
@ -31,12 +38,22 @@ class ComponentCollection extends ObjectCollection {
if (empty($Controller->components)) {
return;
}
$this->_Controller = $Controller;
$components = ComponentCollection::normalizeObjectArray($Controller->components);
foreach ($components as $name => $properties) {
$Controller->{$name} = $this->load($properties['class'], $properties['settings']);
}
}
/**
* Get the controller associated with the collection.
*
* @return Controller.
*/
public function getController() {
return $this->_Controller;
}
/**
* Loads/constructs a component. Will return the instance in the registry if it already exists.
*

View file

@ -274,4 +274,18 @@ class ComponentCollectionTest extends CakeTestCase {
);
$this->assertEquals($expected, $result);
}
/**
* test getting the controller out of the collection
*
* @return void
*/
function testGetController() {
$controller = $this->getMock('Controller');
$controller->components = array('Security');
$this->Components->init($controller);
$result = $this->Components->getController();
$this->assertSame($controller, $result);
}
}