Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 3 commits into from

2 participants

@skolodyazhnyy

I have created an fix for issue #3192, about a problem which I got. Also, I made a simple test for my situation and add it to tests/ZendTests/Di/DiTest.php.

@weierophinney weierophinney commented on the diff
library/Zend/Di/Di.php
@@ -120,6 +120,29 @@ public function instanceManager()
}
/**
+ * @param $name
+ * @param array $params
+ * @param string $method
+ * @return array
+ */
+
+ public function _getCallParameters($name, array $params, $method = "__construct")
@weierophinney Owner

Why the "_" prefix on the method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney commented on the diff
library/Zend/Di/Di.php
@@ -120,6 +120,29 @@ public function instanceManager()
}
/**
+ * @param $name
+ * @param array $params
+ * @param string $method
+ * @return array
+ */
+
+ public function _getCallParameters($name, array $params, $method = "__construct")
+ {
+ $im = $this->instanceManager;
+ $class = $im->hasAlias($name) ? $im->getClassFromAlias($name) : $name;
+ if($this->definitions->hasClass($class)) {
@weierophinney Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney commented on the diff
library/Zend/Di/Di.php
((4 lines not shown))
+ * @param $name
+ * @param array $params
+ * @param string $method
+ * @return array
+ */
+
+ public 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]];
@weierophinney Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney was assigned
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#3260] CS fixes
- Removed "_" prefix from getCallParameters()
- whitespace between operators and parens
- added braces to logic blocks
21fd2cd
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3260' into develop
Forward port #3260
d5c76dc
@weierophinney

Merged, and corrected all CS issues at the same time.

@skolodyazhnyy

Thanks, Matthew

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#3260] CS fixes
- Removed "_" prefix from getCallParameters()
- whitespace between operators and parens
- added braces to logic blocks
86d83f0
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3260'
Close #3260
6dcc96f
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3260' into develop
Forward port #3260
d47d5f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
36 library/Zend/Di/Di.php
@@ -120,6 +120,29 @@ public function instanceManager()
}
/**
+ * @param $name
+ * @param array $params
+ * @param string $method
+ * @return array
+ */
+
+ public function _getCallParameters($name, array $params, $method = "__construct")
@weierophinney Owner

Why the "_" prefix on the method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ $im = $this->instanceManager;
+ $class = $im->hasAlias($name) ? $im->getClassFromAlias($name) : $name;
+ if($this->definitions->hasClass($class)) {
@weierophinney Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $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]];
@weierophinney Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }
+ return $callParameters;
+ }
+ return $params;
+ }
+
+ /**
* Lazy-load a class
*
* Attempts to load the class (or service alias) provided. If it has been
@@ -136,18 +159,19 @@ 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);
@@ -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;
+ }
+}
Something went wrong with that request. Please try again.