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

Zend/Di, retriving same shared instance for different extra parameters #3260

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions library/Zend/Di/Di.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ public function instanceManager()
return $this->instanceManager; return $this->instanceManager;
} }


/**
* @param $name
* @param array $params
* @param string $method
* @return array
*/

public function _getCallParameters($name, array $params, $method = "__construct")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the "_" prefix on the method?

{
$im = $this->instanceManager;
$class = $im->hasAlias($name) ? $im->getClassFromAlias($name) : $name;
if($this->definitions->hasClass($class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS note, for here and throughout: you need a space between an operator and any opening paren: if (, foreach (, etc.

$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]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS note: all blocks must be in braces: foreach ($methodParameters as $param) {, if (isset($params[$param[0]])) {, etc.

}
return $callParameters;
}
return $params;
}

/** /**
* Lazy-load a class * Lazy-load a class
* *
Expand All @@ -136,18 +159,19 @@ public function get($name, array $params = array())


$im = $this->instanceManager; $im = $this->instanceManager;


if ($params) { $callParameters = $this->_getCallParameters($name, $params);
$fastHash = $im->hasSharedInstanceWithParameters($name, $params, true); if($callParameters) {
$fastHash = $im->hasSharedInstanceWithParameters($name, $callParameters, true);
if ($fastHash) { if ($fastHash) {
array_pop($this->instanceContext); array_pop($this->instanceContext);


return $im->getSharedInstanceWithParameters(null, array(), $fastHash); return $im->getSharedInstanceWithParameters(null, array(), $fastHash);
} }
} else { } else {
if ($im->hasSharedInstance($name, $params)) { if ($im->hasSharedInstance($name, $callParameters)) {
array_pop($this->instanceContext); array_pop($this->instanceContext);


return $im->getSharedInstance($name, $params); return $im->getSharedInstance($name, $callParameters);
} }
} }
$config = $im->getConfig($name); $config = $im->getConfig($name);
Expand Down Expand Up @@ -226,8 +250,8 @@ public function newInstance($name, array $params = array(), $isShared = true)
} }


if ($isShared) { if ($isShared) {
if ($params) { if ($callParameters = $this->_getCallParameters($name, $params)) {
$this->instanceManager->addSharedInstanceWithParameters($instance, $name, $params); $this->instanceManager->addSharedInstanceWithParameters($instance, $name, $callParameters);
} else { } else {
$this->instanceManager->addSharedInstance($instance, $name); $this->instanceManager->addSharedInstance($instance, $name);
} }
Expand Down
27 changes: 26 additions & 1 deletion tests/ZendTest/Di/DiTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public function testGetRetrievesDifferentInstanceOnSubsequentCallsIfSharingDisab
'instance' => array( 'instance' => array(
'ZendTest\Di\TestAsset\BasicClass' => array( 'ZendTest\Di\TestAsset\BasicClass' => array(
'shared' => false, 'shared' => false,
),
), ),
),
)); ));
$di = new Di(null, null, $config); $di = new Di(null, null, $config);
$obj1 = $di->get('ZendTest\Di\TestAsset\BasicClass'); $obj1 = $di->get('ZendTest\Di\TestAsset\BasicClass');
Expand All @@ -89,6 +89,31 @@ public function testGetRetrievesDifferentInstanceOnSubsequentCallsIfSharingDisab
$this->assertNotSame($obj1, $obj2); $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() public function testGetThrowsExceptionWhenUnknownClassIsUsed()
{ {
$di = new Di(); $di = new Di();
Expand Down
21 changes: 21 additions & 0 deletions tests/ZendTest/Di/TestAsset/BasicClassWithParent.php
Original file line number Original file line 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;
}
}