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

Commit

Permalink
Merge branch 'hotfix/239-reflection-factory-default-values' into develop
Browse files Browse the repository at this point in the history
Forward port #243

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Jan 29, 2018
2 parents aea63ce + 9a2c497 commit 84c1dda
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ All notable changes to this project will be documented in this file, in reverse
- [#230](https://github.com/zendframework/zend-servicemanager/pull/230) fixes a
problem in detecting cyclic aliases, ensuring they are detected correctly.

## 3.3.2 - TBD

### Added

- Nothing.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- [#243](https://github.com/zendframework/zend-servicemanager/pull/243) provides
a fix to the `ReflectionBasedAbstractFactory` to resolve type-hinted arguments
with default values to their default values if no matching type is found in
the container.

## 3.3.1 - 2017-11-27

### Added
Expand Down
10 changes: 8 additions & 2 deletions src/AbstractFactory/ReflectionBasedAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
$type = $parameter->getClass()->getName();
$type = $this->aliases[$type] ?? $type;

if (! $container->has($type)) {
if ($container->has($type)) {
return $container->get($type);
}

if (! $parameter->isOptional()) {
throw new ServiceNotFoundException(sprintf(
'Unable to create service "%s"; unable to resolve parameter "%s" using type hint "%s"',
$requestedName,
Expand All @@ -231,6 +235,8 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
));
}

return $container->get($type);
// Type not available in container, but the value is optional and has a
// default defined.
return $parameter->getDefaultValue();
}
}
17 changes: 17 additions & 0 deletions test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ZendTest\ServiceManager\AbstractFactory;

use ArrayAccess;
use Interop\Container\ContainerInterface;
use PHPUnit\Framework\TestCase;
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
Expand Down Expand Up @@ -156,4 +157,20 @@ public function testFactoryWillUseDefaultValueWhenPresentForScalarArgument()
self::assertInstanceOf(TestAsset\ClassWithScalarDependencyDefiningDefaultValue::class, $instance);
self::assertEquals('bar', $instance->foo);
}

/**
* @see https://github.com/zendframework/zend-servicemanager/issues/239
*/
public function testFactoryWillUseDefaultValueForTypeHintedArgument()
{
$this->container->has('config')->willReturn(false);
$this->container->has(ArrayAccess::class)->willReturn(false);
$factory = new ReflectionBasedAbstractFactory();
$instance = $factory(
$this->container->reveal(),
TestAsset\ClassWithTypehintedDefaultValue::class
);
$this->assertInstanceOf(TestAsset\ClassWithTypehintedDefaultValue::class, $instance);
$this->assertNull($instance->value);
}
}
20 changes: 20 additions & 0 deletions test/AbstractFactory/TestAsset/ClassWithTypehintedDefaultValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @see https://github.com/zendframework/zend-2018 for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-2018/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\ServiceManager\AbstractFactory\TestAsset;

use ArrayAccess;

class ClassWithTypehintedDefaultValue
{
public $value;

public function __construct(ArrayAccess $value = null)
{
$this->value = null;
}
}

0 comments on commit 84c1dda

Please sign in to comment.