Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/3260' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
42 changes: 33 additions & 9 deletions src/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ public function instanceManager()
return $this->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
*
Expand All @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down
27 changes: 26 additions & 1 deletion test/DiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions test/TestAsset/BasicClassWithParent.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit a365923

Please sign in to comment.