Skip to content

Commit

Permalink
Adding support to ObjectCollection and its subclasses to trigger call…
Browse files Browse the repository at this point in the history
…backs 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.
  • Loading branch information
markstory committed Sep 15, 2010
1 parent 2906927 commit 97dd7c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cake/libs/controller/controller.php
Expand Up @@ -533,7 +533,7 @@ public function getResponse() {
* @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));
}
Expand Down
8 changes: 7 additions & 1 deletion cake/libs/object_collection.php
Expand Up @@ -57,6 +57,8 @@ abstract public function load($name, $options = array(), $enable = true);
* - `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.
Expand All @@ -69,10 +71,14 @@ public function trigger($callback, $params = array(), $options = array()) {
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) {
Expand Down
21 changes: 21 additions & 0 deletions cake/tests/cases/libs/controller/component_collection.test.php
Expand Up @@ -149,6 +149,27 @@ function testTrigger() {
$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.
*
Expand Down
2 changes: 1 addition & 1 deletion cake/tests/cases/libs/controller/controller.test.php
Expand Up @@ -1478,7 +1478,7 @@ function testStartupProcess() {

$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));
Expand Down

0 comments on commit 97dd7c7

Please sign in to comment.