diff --git a/src/Di.php b/src/Di.php index a476391d..b2404b27 100644 --- a/src/Di.php +++ b/src/Di.php @@ -163,13 +163,14 @@ public function get($name, array $params = array()) array_pop($this->instanceContext); return $im->getSharedInstanceWithParameters(null, array(), $fastHash); } - } else { - if ($im->hasSharedInstance($name, $callParameters)) { - array_pop($this->instanceContext); - return $im->getSharedInstance($name, $callParameters); - } } + if ($im->hasSharedInstance($name, $callParameters)) { + array_pop($this->instanceContext); + return $im->getSharedInstance($name, $callParameters); + } + + $config = $im->getConfig($name); $instance = $this->newInstance($name, $params, $config['shared']); array_pop($this->instanceContext); diff --git a/test/DiTest.php b/test/DiTest.php index 64255bc0..cf019f5a 100644 --- a/test/DiTest.php +++ b/test/DiTest.php @@ -779,4 +779,33 @@ public function testDiWillInjectDefaultParameters() $this->assertSame('defaultConstruct', $optionalParams->b); $this->assertSame(array(), $optionalParams->c); } + + /** + * @group SharedInstance + */ + public function testGetWithParamsWillUseSharedInstance() + { + $di = new Di; + + $sharedInstanceClass = 'ZendTest\Di\TestAsset\ConstructorInjection\A'; + $retrievedInstanceClass = 'ZendTest\Di\TestAsset\ConstructorInjection\C'; + + // Provide definitions for $retrievedInstanceClass, but not for $sharedInstanceClass. + $arrayDefinition = array($retrievedInstanceClass => array ( + 'supertypes' => array ( ), + 'instantiator' => '__construct', + 'methods' => array ('__construct' => true), + 'parameters' => array ( '__construct' => array ( + "$retrievedInstanceClass::__construct:0" => array ('a', $sharedInstanceClass, true, NULL), + "$retrievedInstanceClass::__construct:1" => array ('params', NULL, false, array()), + )), + )); + + // This also disables scanning of class A. + $di->setDefinitionList(new DefinitionList(new Definition\ArrayDefinition($arrayDefinition))); + + $di->instanceManager()->addSharedInstance(new $sharedInstanceClass, $sharedInstanceClass); + $returnedC = $di->get($retrievedInstanceClass, array('params' => array('test'))); + $this->assertInstanceOf($retrievedInstanceClass, $returnedC); + } } diff --git a/test/TestAsset/ConstructorInjection/C.php b/test/TestAsset/ConstructorInjection/C.php new file mode 100644 index 00000000..751efa1b --- /dev/null +++ b/test/TestAsset/ConstructorInjection/C.php @@ -0,0 +1,22 @@ +a = $a; + $this->params = $params; + } +}