Skip to content

Commit

Permalink
Improve docs and add reset() method.
Browse files Browse the repository at this point in the history
Add a reset() method, while I don't think it will be widely useful it
could be helpful in testing situations and is simple to support and
maintain.
  • Loading branch information
markstory committed Jul 27, 2013
1 parent 3bae222 commit f3ae5e6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
16 changes: 16 additions & 0 deletions lib/Cake/Test/TestCase/Controller/ComponentRegistryTest.php
Expand Up @@ -165,4 +165,20 @@ public function testGetController() {
$result = $this->Components->getController();
$this->assertInstanceOf('Cake\Controller\Controller', $result);
}

/**
* Test reset.
*
* @return void
*/
public function testReset() {
$instance = $this->Components->load('Paginator');
$this->assertSame(
$instance,
$this->Components->Paginator,
'Instance in registry should be the same as previously loaded'
);
$this->assertNull($this->Components->reset(), 'No return expected');
$this->assertNotSame($instance, $this->Components->load('Paginator'));
}
}
22 changes: 17 additions & 5 deletions lib/Cake/Utility/ObjectRegistry.php
Expand Up @@ -19,8 +19,9 @@
* Provides registry & factory functionality for object types. Used
* as a super class for various composition based re-use features in CakePHP.
*
* Each subclass needs to implement its own load() functionality. Replaces ObjectCollection
* in previous versions of CakePHP.
* Each subclass needs to implement the various abstract methods to complete
* the template method load(). This class replaces ObjectCollection
* from previous versions of CakePHP.
*
* @since CakePHP 3.0
* @see Cake\Controller\ComponentRegistry
Expand All @@ -40,10 +41,12 @@ abstract class ObjectRegistry {
* Loads/constructs a object instance.
*
* Will return the instance in the registry if it already exists.
* You can use `$settings['enabled'] = false` to disable events on an object when loading it.
* Not all registry subclasses support events.
* If a subclass provides event support, you can use `$settings['enabled'] = false`
* to exclude constructed objects from being registered for events.
*
* Using Cake\Controller\Controller::$components as an example. You can alias
* an object by setting the 'className' key, i.e.,
*
* You can alias an object by setting the 'className' key, i.e.,
* {{{
* public $components = [
* 'Email' => [
Expand Down Expand Up @@ -163,4 +166,13 @@ public function normalizeArray($objects) {
return $normal;
}

/**
* Clear loaded instances in the registry.
*
* @return void
*/
public function reset() {
$this->_loaded = [];
}

}

0 comments on commit f3ae5e6

Please sign in to comment.