This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Zend/Di, retriving same shared instance for different extra parameters #3260
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -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") | |||
{ | |||
$im = $this->instanceManager; | |||
$class = $im->hasAlias($name) ? $im->getClassFromAlias($name) : $name; | |||
if($this->definitions->hasClass($class)) { | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
|||
$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]]; | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CS note: all blocks must be in braces: |
|||
} | |||
return $callParameters; | |||
} | |||
return $params; | |||
} | |||
|
|||
/** | /** | ||
* Lazy-load a class | * Lazy-load a class | ||
* | * | ||
|
@@ -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); | ||
|
@@ -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); | ||
} | } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |||
} | |||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?