From 0ef76eb69ada803a03c5f1b0d0a1d5570b80874c Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 14 Sep 2010 22:52:51 -0400 Subject: [PATCH] Adding a reference of the controller to the component collection, so components can easily access the controller in their constructor. --- cake/libs/controller/component_collection.php | 17 +++++++++++++++++ .../controller/component_collection.test.php | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/cake/libs/controller/component_collection.php b/cake/libs/controller/component_collection.php index a4aea463b..2f8e6b286 100644 --- a/cake/libs/controller/component_collection.php +++ b/cake/libs/controller/component_collection.php @@ -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. * diff --git a/cake/tests/cases/libs/controller/component_collection.test.php b/cake/tests/cases/libs/controller/component_collection.test.php index 6d4eabb22..cb0e83b83 100644 --- a/cake/tests/cases/libs/controller/component_collection.test.php +++ b/cake/tests/cases/libs/controller/component_collection.test.php @@ -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); + } } \ No newline at end of file