commit
5dd3ec6f1e
|
@ -46,4 +46,13 @@ ## Resolving Objects
|
||||||
|
|
||||||
$mailer = IoC::resolve('mailer');
|
$mailer = IoC::resolve('mailer');
|
||||||
|
|
||||||
> **Note:** You may also [register controllers in the container](/docs/controllers#dependency-injection).
|
> **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');
|
|
@ -31,6 +31,19 @@ public static function register($name, $resolver = null, $singleton = false)
|
||||||
static::$registry[$name] = compact('resolver', 'singleton');
|
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.
|
* Determine if an object has been registered in the container.
|
||||||
*
|
*
|
||||||
|
@ -141,6 +154,7 @@ public static function resolve($type, $parameters = array())
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
protected static function build($type, $parameters = array())
|
protected static function build($type, $parameters = array())
|
||||||
{
|
{
|
||||||
|
@ -193,7 +207,7 @@ protected static function dependencies($parameters, $arguments)
|
||||||
$dependency = $parameter->getClass();
|
$dependency = $parameter->getClass();
|
||||||
|
|
||||||
// If the person passed in some parameters to the class
|
// If the person passed in some parameters to the class
|
||||||
// then we should probably use those instead of trying
|
// then we should probably use those instead of trying
|
||||||
// to resolve a new instance of the class
|
// to resolve a new instance of the class
|
||||||
if (count($arguments) > 0)
|
if (count($arguments) > 0)
|
||||||
{
|
{
|
||||||
|
@ -205,7 +219,7 @@ protected static function dependencies($parameters, $arguments)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$dependencies[] = static::resolve($dependency->name);
|
$dependencies[] = static::resolve($dependency->name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,6 +232,7 @@ protected static function dependencies($parameters, $arguments)
|
||||||
*
|
*
|
||||||
* @param ReflectionParameter
|
* @param ReflectionParameter
|
||||||
* @return default value
|
* @return default value
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
protected static function resolveNonClass($parameter)
|
protected static function resolveNonClass($parameter)
|
||||||
{
|
{
|
||||||
|
@ -229,6 +244,6 @@ protected static function resolveNonClass($parameter)
|
||||||
{
|
{
|
||||||
throw new \Exception("Unresolvable dependency resolving [$parameter].");
|
throw new \Exception("Unresolvable dependency resolving [$parameter].");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -28,6 +28,7 @@ public function __construct(TestClassOneForIoC $class_one)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use \Laravel\IoC as IoC;
|
||||||
|
|
||||||
class IoCTest extends PHPUnit_Framework_TestCase {
|
class IoCTest extends PHPUnit_Framework_TestCase {
|
||||||
|
|
||||||
|
@ -150,4 +151,17 @@ public function testClassTwoResolvesClassOneWithArgument()
|
||||||
$this->assertEquals(42, $class_two->class_one->test_variable);
|
$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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue