Skip to content

Commit

Permalink
Fix issues with multiple chain calls (#168)
Browse files Browse the repository at this point in the history
* Pass share to call methods

* Fix issue with multiple chain calls
  • Loading branch information
solleer authored and TRPB committed May 1, 2019
1 parent 4667ce9 commit e631f11
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions Dice.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private function getClosure(string $name, array $rule) {
if (isset($call[2])) {
if ($call[2] === self::CHAIN_CALL) {
if (!empty($rule['shared'])) $this->instances[$name] = $return;
if (is_object($return)) $class = new \ReflectionClass(get_class($return));
$object = $return;
}
else if (is_callable($call[2])) call_user_func($call[2], $return);
Expand Down
21 changes: 20 additions & 1 deletion tests/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ public function testChainCall() {

}

public function testMultipleChainCall() {
$dice = $this->dice->addRules([
'$someClass' => [
'instanceOf' => 'Factory',
'call' => [
['get', [], \Dice\Dice::CHAIN_CALL],
['getBar', [], \Dice\Dice::CHAIN_CALL]
]
]
]);

$obj = $dice->create('$someClass');

$this->assertEquals('bar', $obj);

}

public function testChainCallShared() {
$dice = $this->dice->addRules([
'$someClass' => [
Expand Down Expand Up @@ -88,7 +105,9 @@ public function get() {
}

class FactoryDependency {

public function getBar() {
return 'bar';
}
}

class RequiresFactoryDependecy {
Expand Down

0 comments on commit e631f11

Please sign in to comment.