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

Commit

Permalink
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Definition/CompilerDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ protected function processParams(&$def, Reflection\ClassReflection $rClass, Refl
$def['parameters'][$methodName][$fqName][] = $actualParamName;
$def['parameters'][$methodName][$fqName][] = ($p->getClass() !== null) ? $p->getClass()->getName() : null;
$def['parameters'][$methodName][$fqName][] = !($optional =$p->isOptional());
$def['parameters'][$methodName][$fqName][] = $optional ? $p->getDefaultValue() : null;
$def['parameters'][$methodName][$fqName][] = $optional && $p->isDefaultValueAvailable() ? $p->getDefaultValue() : null;
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/Definition/RuntimeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ protected function processParams(&$def, Reflection\ClassReflection $rClass, Refl
// set the class name, if it exists
$def['parameters'][$methodName][$fqName][] = $actualParamName;
$def['parameters'][$methodName][$fqName][] = ($p->getClass() !== null) ? $p->getClass()->getName() : null;
$def['parameters'][$methodName][$fqName][] = !($optional =$p->isOptional());
$def['parameters'][$methodName][$fqName][] = !($optional = $p->isOptional() && $p->isDefaultValueAvailable());
$def['parameters'][$methodName][$fqName][] = $optional ? $p->getDefaultValue() : null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DefinitionList.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function hasMethod($class, $method)
if ($definition->hasMethods($class) === false && $definition instanceof Definition\PartialMarker) {
continue;
} else {
return $definition->hasMethods($class);
return $definition->hasMethod($class, $method);
}
}
}
Expand Down
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
2 changes: 1 addition & 1 deletion src/ServiceLocator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function setNamespace($namespace)
/**
* Construct, configure, and return a PHP class file code generation object
*
* Creates a Zend\CodeGenerator\Php\PhpFile object that has
* Creates a Zend\Code\Generator\FileGenerator object that has
* created the specified class and service locator methods.
*
* @param null|string $filename
Expand Down
34 changes: 34 additions & 0 deletions test/Definition/RuntimeDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,38 @@ public function testIncludesDefaultMethodParameters()
)
);
}

public function testExceptionDefaultValue()
{
$definition = new RuntimeDefinition();

$definition->forceLoadClass('RecursiveIteratorIterator');

$this->assertSame(
array(
'RecursiveIteratorIterator::__construct:0' => array(
'iterator',
'Traversable',
true,
null,
),
'RecursiveIteratorIterator::__construct:1' => Array (
'mode',
null,
true,
null,
),
'RecursiveIteratorIterator::__construct:2' => Array (
'flags',
null,
true,
null,
),
),
$definition->getMethodParameters(
'RecursiveIteratorIterator',
'__construct'
)
);
}
}
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
6 changes: 3 additions & 3 deletions test/ServiceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testRetrievingAServiceMultipleTimesReturnsSameInstance()

public function testRegisteringCallbacksReturnsReturnValueWhenServiceRequested()
{
$this->services->set('foo', function() {
$this->services->set('foo', function () {
$object = new \stdClass();
$object->foo = 'FOO';
return $object;
Expand All @@ -68,7 +68,7 @@ public function testRegisteringCallbacksReturnsReturnValueWhenServiceRequested()

public function testReturnValueOfCallbackIsCachedBetweenRequestsToService()
{
$this->services->set('foo', function() {
$this->services->set('foo', function () {
$object = new \stdClass();
$object->foo = 'FOO';
return $object;
Expand All @@ -81,7 +81,7 @@ public function testReturnValueOfCallbackIsCachedBetweenRequestsToService()

public function testParametersArePassedToCallbacks()
{
$this->services->set('foo', function() {
$this->services->set('foo', function () {
$object = new \stdClass();
$object->params = func_get_args();
return $object;
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;
}
}
15 changes: 15 additions & 0 deletions test/TestAsset/CompilerClasses/F.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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\CompilerClasses;

class F extends \Exception
{
}

0 comments on commit 24f43fa

Please sign in to comment.