Skip to content

Commit

Permalink
Clear bound events when calling reset().
Browse files Browse the repository at this point in the history
Events will retain instances that should have been destroyed. Clear
bound events when emptying a registry.
  • Loading branch information
markstory committed Jul 27, 2013
1 parent f3ae5e6 commit 999aef9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
14 changes: 14 additions & 0 deletions lib/Cake/Controller/ComponentRegistry.php
Expand Up @@ -111,4 +111,18 @@ protected function _create($class, $settings) {
return $instance;
}

/**
* Destroys all objects in the registry.
*
* Removes all attached listeners and destroys all stored instances.
*
* @return void
*/
public function reset() {
foreach ($this->_loaded as $component) {
$this->_eventManager->detach($component);
}
parent::reset();
}

}
12 changes: 9 additions & 3 deletions lib/Cake/Test/TestCase/Controller/ComponentRegistryTest.php
Expand Up @@ -172,13 +172,19 @@ public function testGetController() {
* @return void
*/
public function testReset() {
$instance = $this->Components->load('Paginator');
$eventManager = $this->Components->getController()->getEventManager();
$instance = $this->Components->load('Auth');
$this->assertSame(
$instance,
$this->Components->Paginator,
$this->Components->Auth,
'Instance in registry should be the same as previously loaded'
);
$this->assertCount(1, $eventManager->listeners('Controller.startup'));

$this->assertNull($this->Components->reset(), 'No return expected');
$this->assertNotSame($instance, $this->Components->load('Paginator'));
$this->assertCount(0, $eventManager->listeners('Controller.startup'));

$this->assertNotSame($instance, $this->Components->load('Auth'));
}

}
19 changes: 19 additions & 0 deletions lib/Cake/Test/TestCase/View/HelperRegistryTest.php
Expand Up @@ -188,4 +188,23 @@ public function testLoadPluginHelper() {
App::build();
}

/**
* Test reset.
*
* @return void
*/
public function testReset() {
$instance = $this->Helpers->load('Paginator');
$this->assertSame(
$instance,
$this->Helpers->Paginator,
'Instance in registry should be the same as previously loaded'
);
$this->assertCount(1, $this->Events->listeners('View.beforeRender'));

$this->assertNull($this->Helpers->reset(), 'No return expected');
$this->assertCount(0, $this->Events->listeners('View.beforeRender'));

$this->assertNotSame($instance, $this->Helpers->load('Paginator'));
}
}
14 changes: 14 additions & 0 deletions lib/Cake/View/HelperRegistry.php
Expand Up @@ -150,4 +150,18 @@ protected function _create($class, $settings) {
return $instance;
}

/**
* Destroys all objects in the registry.
*
* Removes all attached listeners and destroys all stored instances.
*
* @return void
*/
public function reset() {
foreach ($this->_loaded as $helper) {
$this->_eventManager->detach($helper);
}
parent::reset();
}

}

0 comments on commit 999aef9

Please sign in to comment.