Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'hotfix/3260'

Close #3260
  • Loading branch information...
commit d571e217634c8f15d1eef4594c07be40f9722562 2 parents 1675abc + 21fd2cd
@weierophinney weierophinney authored
View
42 library/Zend/Di/Di.php
@@ -120,6 +120,30 @@ public function instanceManager()
}
/**
+ * @param $name
+ * @param array $params
+ * @param string $method
+ * @return array
+ */
+ protected function getCallParameters($name, array $params, $method = "__construct")
+ {
+ $im = $this->instanceManager;
+ $class = $im->hasAlias($name) ? $im->getClassFromAlias($name) : $name;
+ if ($this->definitions->hasClass($class)) {
+ $callParameters = array();
+ if ($this->definitions->hasMethod($class, $method)) {
+ foreach ($this->definitions->getMethodParameters($class, $method) as $param) {
+ if (isset($params[$param[0]])) {
+ $callParameters[$param[0]] = $params[$param[0]];
+ }
+ }
+ }
+ return $callParameters;
+ }
+ return $params;
+ }
+
+ /**
* Lazy-load a class
*
* Attempts to load the class (or service alias) provided. If it has been
@@ -136,21 +160,21 @@ public function get($name, array $params = array())
$im = $this->instanceManager;
- if ($params) {
- $fastHash = $im->hasSharedInstanceWithParameters($name, $params, true);
+ $callParameters = $this->getCallParameters($name, $params);
+ if ($callParameters) {
+ $fastHash = $im->hasSharedInstanceWithParameters($name, $callParameters, true);
if ($fastHash) {
array_pop($this->instanceContext);
-
return $im->getSharedInstanceWithParameters(null, array(), $fastHash);
}
} else {
- if ($im->hasSharedInstance($name, $params)) {
+ if ($im->hasSharedInstance($name, $callParameters)) {
array_pop($this->instanceContext);
-
- return $im->getSharedInstance($name, $params);
+ return $im->getSharedInstance($name, $callParameters);
}
}
- $config = $im->getConfig($name);
+
+ $config = $im->getConfig($name);
$instance = $this->newInstance($name, $params, $config['shared']);
array_pop($this->instanceContext);
@@ -226,8 +250,8 @@ public function newInstance($name, array $params = array(), $isShared = true)
}
if ($isShared) {
- if ($params) {
- $this->instanceManager->addSharedInstanceWithParameters($instance, $name, $params);
+ if ($callParameters = $this->getCallParameters($name, $params)) {
+ $this->instanceManager->addSharedInstanceWithParameters($instance, $name, $callParameters);
} else {
$this->instanceManager->addSharedInstance($instance, $name);
}
View
27 tests/ZendTest/Di/DiTest.php
@@ -78,8 +78,8 @@ public function testGetRetrievesDifferentInstanceOnSubsequentCallsIfSharingDisab
'instance' => array(
'ZendTest\Di\TestAsset\BasicClass' => array(
'shared' => false,
- ),
),
+ ),
));
$di = new Di(null, null, $config);
$obj1 = $di->get('ZendTest\Di\TestAsset\BasicClass');
@@ -89,6 +89,31 @@ public function testGetRetrievesDifferentInstanceOnSubsequentCallsIfSharingDisab
$this->assertNotSame($obj1, $obj2);
}
+ public function testGetRetrievesSameSharedInstanceOnUsingInConstructor()
+ {
+ $config = new Config(array(
+ 'instance' => array(
+ 'ZendTest\Di\TestAsset\BasicClass' => array(
+ 'shared' => true,
+ ),
+ ),
+ ));
+ $di = new Di(null, null, $config);
+ $obj1 = $di->get('ZendTest\Di\TestAsset\BasicClassWithParent', array('foo' => 0));
+ $obj2 = $di->get('ZendTest\Di\TestAsset\BasicClassWithParent', array('foo' => 1));
+ $obj3 = $di->get('ZendTest\Di\TestAsset\BasicClassWithParent', array('foo' => 2, 'non_exists' => 1));
+ $objParent1 = $di->get('ZendTest\Di\TestAsset\BasicClass');
+ $objParent2 = $di->get('ZendTest\Di\TestAsset\BasicClass', array('foo' => 1));
+
+ $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClassWithParent', $obj1);
+ $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClassWithParent', $obj2);
+ $this->assertInstanceOf('ZendTest\Di\TestAsset\BasicClassWithParent', $obj3);
+ $this->assertSame($obj1->parent, $obj2->parent);
+ $this->assertSame($obj2->parent, $obj3->parent);
+ $this->assertSame($obj3->parent, $objParent1);
+ $this->assertSame($obj3->parent, $objParent2);
+ }
+
public function testGetThrowsExceptionWhenUnknownClassIsUsed()
{
$di = new Di();
View
21 tests/ZendTest/Di/TestAsset/BasicClassWithParent.php
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Di
+ */
+
+namespace ZendTest\Di\TestAsset;
+
+class BasicClassWithParent
+{
+ public $parent;
+
+ public function __construct(BasicClass $parent, $foo)
+ {
+ $this->parent = $parent;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.