Skip to content

Commit

Permalink
Move unload() into ObjectRegistry.
Browse files Browse the repository at this point in the history
Make unload() and reset behave similarily. Both remove objects and
detach events.
  • Loading branch information
markstory committed Mar 26, 2014
1 parent 84823ff commit 8f3612e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 39 deletions.
23 changes: 1 addition & 22 deletions src/Controller/ComponentRegistry.php
Expand Up @@ -30,17 +30,10 @@ class ComponentRegistry extends ObjectRegistry {
/**
* The controller that this collection was initialized with.
*
* @var Controller
* @var \Cake\Controller\Controller
*/
protected $_Controller = null;

/**
* The event manager to bind components to.
*
* @var \Cake\Event\EventManager
*/
protected $_eventManager = null;

/**
* Constructor.
*
Expand Down Expand Up @@ -112,18 +105,4 @@ protected function _create($class, $alias, $config) {
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();
}

}
36 changes: 33 additions & 3 deletions src/Utility/ObjectRegistry.php
Expand Up @@ -37,6 +37,13 @@ abstract class ObjectRegistry {
*/
protected $_loaded = [];

/**
* The event manager to bind components to.
*
* @var \Cake\Event\EventManager
*/
protected $_eventManager;

/**
* Loads/constructs a object instance.
*
Expand Down Expand Up @@ -170,18 +177,22 @@ public function normalizeArray($objects) {
/**
* Clear loaded instances in the registry.
*
* If the registry subclass has an event manager, the objects will be detached from events as well.
*
* @return void
*/
public function reset() {
$this->_loaded = [];
foreach (array_keys($this->_loaded) as $name) {
$this->unload($name);
}
}

/**
* set an object directly into the registry by name
*
* This is primarily to aide testing
* This is primarily to aid testing
*
* @param string $objectName
* @param string $objectName The name of the object to set in the registry.
* @param object $object instance to store in the registry
* @return void
*/
Expand All @@ -190,4 +201,23 @@ public function set($objectName, $object) {
$this->_loaded[$name] = $object;
}

/**
* Remove an object from the registry.
*
* If this registry has an event manager, the object will be detached from any events as well.
*
* @param string $objectName The name of the object to remove from the registry.
* @return void
*/
public function unload($objectName) {
if (empty($this->_loaded[$objectName])) {
return;
}
$object = $this->_loaded[$objectName];
if (isset($this->_eventManager)) {
$this->_eventManager->detach($object);
}
unset($this->_loaded[$objectName]);
}

}
14 changes: 0 additions & 14 deletions src/View/HelperRegistry.php
Expand Up @@ -151,18 +151,4 @@ protected function _create($class, $alias, $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();
}

}
15 changes: 15 additions & 0 deletions tests/TestCase/Controller/ComponentRegistryTest.php
Expand Up @@ -178,4 +178,19 @@ public function testReset() {
$this->assertNotSame($instance, $this->Components->load('Auth'));
}

/**
* Test unloading.
*
* @return void
*/
public function testUnload() {
$eventManager = $this->Components->getController()->getEventManager();

$result = $this->Components->load('Auth');
$this->Components->unload('Auth');

$this->assertFalse(isset($this->Components->Auth), 'Should be gone');
$this->assertCount(0, $eventManager->listeners('Controller.startup'));
}

}
18 changes: 18 additions & 0 deletions tests/TestCase/View/HelperRegistryTest.php
Expand Up @@ -200,4 +200,22 @@ public function testReset() {

$this->assertNotSame($instance, $this->Helpers->load('Paginator'));
}

/**
* Test unloading.
*
* @return void
*/
public function testUnload() {
$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->unload('Paginator'), 'No return expected');
$this->assertCount(0, $this->Events->listeners('View.beforeRender'));
}
}

0 comments on commit 8f3612e

Please sign in to comment.