diff --git a/Dice.php b/Dice.php index c27afe6..d9360cc 100644 --- a/Dice.php +++ b/Dice.php @@ -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); diff --git a/tests/ChainTest.php b/tests/ChainTest.php index b448b8e..43b00f7 100644 --- a/tests/ChainTest.php +++ b/tests/ChainTest.php @@ -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' => [ @@ -88,7 +105,9 @@ public function get() { } class FactoryDependency { - + public function getBar() { + return 'bar'; + } } class RequiresFactoryDependecy {