Browse Source

Move unload() into ObjectRegistry.

Make unload() and reset behave similarily. Both remove objects and
detach events.
mark_story 12 years ago
parent
commit
8f3612eaae

+ 1 - 22
src/Controller/ComponentRegistry.php

@@ -30,18 +30,11 @@ 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.
  *
  * @param \Cake\Controller\Controller $Controller
@@ -112,18 +105,4 @@ class ComponentRegistry extends ObjectRegistry {
 		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();
-	}
-
 }

+ 33 - 3
src/Utility/ObjectRegistry.php

@@ -38,6 +38,13 @@ abstract class ObjectRegistry {
 	protected $_loaded = [];
 
 /**
+ * The event manager to bind components to.
+ *
+ * @var \Cake\Event\EventManager
+ */
+	protected $_eventManager;
+
+/**
  * Loads/constructs a object instance.
  *
  * Will return the instance in the registry if it already exists.
@@ -170,18 +177,22 @@ abstract class ObjectRegistry {
 /**
  * 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
  */
@@ -190,4 +201,23 @@ abstract class ObjectRegistry {
 		$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]);
+	}
+
 }

+ 0 - 14
src/View/HelperRegistry.php

@@ -151,18 +151,4 @@ class HelperRegistry extends ObjectRegistry {
 		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 - 0
tests/TestCase/Controller/ComponentRegistryTest.php

@@ -178,4 +178,19 @@ class ComponentRegistryTest extends TestCase {
 		$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 - 0
tests/TestCase/View/HelperRegistryTest.php

@@ -200,4 +200,22 @@ class HelperRegistryTest extends TestCase {
 
 		$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'));
+	}
 }