Skip to content

Commit

Permalink
$container->inject now returns the value the function produces.
Browse files Browse the repository at this point in the history
  • Loading branch information
vegardlarsen committed Jun 16, 2015
1 parent 3082b33 commit 89efc42
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/IoC/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,18 @@ public function resolve($classOrInterfaceName)
/**
* Inject properties into an object or invoke a function with parameters injected.
*
* @param $object object|callable The object or function to have its properties injected
* @param $object object|callable The object or function to have its properties/arguments injected
* @return object Returns the object, or the result of the function
* @throws Exceptions\InjectorException
*/
public function inject($object) {

if (is_callable($object)) {
$this->functionInjector->run($object);
return $this->functionInjector->run($object);
}
else {
$this->propertyInjector->inject($object);
return $object;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Tests/IoC/IoContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,12 @@ public function testInjectForMethod() {
$container->register('\DC\Tests\IoC\Foo')->to('\DC\Tests\IoC\IFoo');

$ran = false;
$container->inject(function(IFoo $foo) use (&$ran) {
$result = $container->inject(function(IFoo $foo) use (&$ran) {
$this->assertInstanceOf('\DC\Tests\IoC\Foo', $foo);
$ran = true;
return true;
});
$this->assertTrue($ran);
$this->assertTrue($result);
}
}

0 comments on commit 89efc42

Please sign in to comment.