mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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.
This commit is contained in:
parent
2906927a16
commit
97dd7c7644
4 changed files with 30 additions and 3 deletions
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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));
|
||||
|
|
Loading…
Reference in a new issue