Commit 5dd3ec6f authored by Taylor Otwell's avatar Taylor Otwell

Merge pull request #1845 from stevenklar/master

Add unregister IoC
parents e86532e0 4086d130
......@@ -47,3 +47,12 @@ Now that we have SwiftMailer registered in the container, we can resolve it usin
$mailer = IoC::resolve('mailer');
> **Note:** You may also [register controllers in the container](/docs/controllers#dependency-injection).
<a name="unregister"></a>
## Unregister an existing instance
For test purposes sometimes you need to unregister some container.
#### Unregister example mail class:
IoC::unregister('mailer');
\ No newline at end of file
......@@ -31,6 +31,19 @@ class IoC {
static::$registry[$name] = compact('resolver', 'singleton');
}
/**
* Unregister an object
*
* @param string $name
*/
public static function unregister($name)
{
if (array_key_exists($name, static::$registry)) {
unset(static::$registry[$name]);
unset(static::$singletons[$name]);
}
}
/**
* Determine if an object has been registered in the container.
*
......@@ -141,6 +154,7 @@ class IoC {
* @param string $type
* @param array $parameters
* @return mixed
* @throws \Exception
*/
protected static function build($type, $parameters = array())
{
......@@ -218,6 +232,7 @@ class IoC {
*
* @param ReflectionParameter
* @return default value
* @throws \Exception
*/
protected static function resolveNonClass($parameter)
{
......
......@@ -28,6 +28,7 @@ class TestClassTwoForIoC
}
}
use \Laravel\IoC as IoC;
class IoCTest extends PHPUnit_Framework_TestCase {
......@@ -150,4 +151,17 @@ class IoCTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(42, $class_two->class_one->test_variable);
}
public function testCanUnregisterRegistered()
{
$testClass = 'test';
IoC::register($testClass, function() {});
$this->assertTrue(IoC::registered($testClass));
IoC::unregister($testClass);
$this->assertFalse(IoC::registered($testClass));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment