From 97dd7c76448184edf71ecda5a91c8904dd1c8046 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 14 Sep 2010 21:57:40 -0400 Subject: [PATCH] Adding support to ObjectCollection and its subclasses to trigger callbacks on all objects instead of just the enabled ones. Fixes issues where inner components would not get access to the controller as the initialize callback wasn't fired. This fixes some backwards compatibility issues. Tests updated. --- cake/libs/controller/controller.php | 2 +- cake/libs/object_collection.php | 8 ++++++- .../controller/component_collection.test.php | 21 +++++++++++++++++++ .../cases/libs/controller/controller.test.php | 2 +- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 0b2e55c35..07e0b8f59 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -533,7 +533,7 @@ class Controller extends Object { * @return void */ public function startupProcess() { - $this->Components->trigger('initialize', array(&$this)); + $this->Components->trigger('initialize', array(&$this), array('triggerDisabled' => true)); $this->beforeFilter(); $this->Components->trigger('startup', array(&$this)); } diff --git a/cake/libs/object_collection.php b/cake/libs/object_collection.php index 01f1efeda..cca7ec58f 100644 --- a/cake/libs/object_collection.php +++ b/cake/libs/object_collection.php @@ -57,6 +57,8 @@ abstract class ObjectCollection { * - `break` Set to true to enabled breaking. Defaults to `false`. * - `collectReturn` Set to true to collect the return of each object into an array. * This array of return values will be returned from the trigger() call. Defaults to `false`. + * - `triggerDisabled` Will trigger the callback on all objects in the collection even the non-enabled + * objects. Defaults to false. * * @param string $callback Method to fire on all the objects. Its assumed all the objects implement * the method you are calling. @@ -69,10 +71,14 @@ abstract class ObjectCollection { return true; } $options = array_merge( - array('break' => false, 'breakOn' => false, 'collectReturn' => false), + array('break' => false, 'breakOn' => false, 'collectReturn' => false, 'triggerDisabled' => false), $options ); $collected = array(); + $list = $this->_enabled; + if ($options['triggerDisabled'] === true) { + $list = array_keys($this->_loaded); + } foreach ($this->_enabled as $name) { $result = call_user_func_array(array(&$this->_loaded[$name], $callback), $params); if ($options['collectReturn'] === true) { diff --git a/cake/tests/cases/libs/controller/component_collection.test.php b/cake/tests/cases/libs/controller/component_collection.test.php index a44e33e63..ccaacbc99 100644 --- a/cake/tests/cases/libs/controller/component_collection.test.php +++ b/cake/tests/cases/libs/controller/component_collection.test.php @@ -149,6 +149,27 @@ class ComponentCollectionTest extends CakeTestCase { $this->assertTrue($this->Components->trigger('startup', array(&$controller))); } +/** + * test that the initalize callback is triggered on all components even those that are disabled. + * + * @return void + */ + function testTriggerOnDisabledObjects() { + $controller = 'Not a controller'; + + $this->_makeMockClasses(); + $this->Components->load('TriggerMockCookie', array(), false); + $this->Components->load('TriggerMockSecurity'); + + $this->Components->TriggerMockCookie->expects($this->once())->method('initalize') + ->with($controller); + $this->Components->TriggerMockSecurity->expects($this->once())->method('initalize') + ->with($controller); + + $result = $this->Components->trigger('initialize', array(&$controller), array('triggerDisabled' => true)); + $this->assertTrue($result); + } + /** * test trigger and disabled helpers. * diff --git a/cake/tests/cases/libs/controller/controller.test.php b/cake/tests/cases/libs/controller/controller.test.php index 5ad875016..20eb72642 100644 --- a/cake/tests/cases/libs/controller/controller.test.php +++ b/cake/tests/cases/libs/controller/controller.test.php @@ -1478,7 +1478,7 @@ class ControllerTest extends CakeTestCase { $Controller->expects($this->once())->method('beforeFilter'); $Controller->Components->expects($this->at(0))->method('trigger') - ->with('initialize', array(&$Controller)); + ->with('initialize', array(&$Controller), array('triggerDisabled' => true)); $Controller->Components->expects($this->at(1))->method('trigger') ->with('startup', array(&$Controller));