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

Commit 84c1dda

Browse files
committed
Merge branch 'hotfix/239-reflection-factory-default-values' into develop
Forward port #243 Conflicts: CHANGELOG.md
2 parents aea63ce + 9a2c497 commit 84c1dda

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@ All notable changes to this project will be documented in this file, in reverse
3232
- [#230](https://github.com/zendframework/zend-servicemanager/pull/230) fixes a
3333
problem in detecting cyclic aliases, ensuring they are detected correctly.
3434

35+
## 3.3.2 - TBD
36+
37+
### Added
38+
39+
- Nothing.
40+
41+
### Changed
42+
43+
- Nothing.
44+
45+
### Deprecated
46+
47+
- Nothing.
48+
49+
### Removed
50+
51+
- Nothing.
52+
53+
### Fixed
54+
55+
- [#243](https://github.com/zendframework/zend-servicemanager/pull/243) provides
56+
a fix to the `ReflectionBasedAbstractFactory` to resolve type-hinted arguments
57+
with default values to their default values if no matching type is found in
58+
the container.
59+
3560
## 3.3.1 - 2017-11-27
3661

3762
### Added

src/AbstractFactory/ReflectionBasedAbstractFactory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
222222
$type = $parameter->getClass()->getName();
223223
$type = $this->aliases[$type] ?? $type;
224224

225-
if (! $container->has($type)) {
225+
if ($container->has($type)) {
226+
return $container->get($type);
227+
}
228+
229+
if (! $parameter->isOptional()) {
226230
throw new ServiceNotFoundException(sprintf(
227231
'Unable to create service "%s"; unable to resolve parameter "%s" using type hint "%s"',
228232
$requestedName,
@@ -231,6 +235,8 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
231235
));
232236
}
233237

234-
return $container->get($type);
238+
// Type not available in container, but the value is optional and has a
239+
// default defined.
240+
return $parameter->getDefaultValue();
235241
}
236242
}

test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace ZendTest\ServiceManager\AbstractFactory;
99

10+
use ArrayAccess;
1011
use Interop\Container\ContainerInterface;
1112
use PHPUnit\Framework\TestCase;
1213
use Zend\ServiceManager\AbstractFactory\ReflectionBasedAbstractFactory;
@@ -156,4 +157,20 @@ public function testFactoryWillUseDefaultValueWhenPresentForScalarArgument()
156157
self::assertInstanceOf(TestAsset\ClassWithScalarDependencyDefiningDefaultValue::class, $instance);
157158
self::assertEquals('bar', $instance->foo);
158159
}
160+
161+
/**
162+
* @see https://github.com/zendframework/zend-servicemanager/issues/239
163+
*/
164+
public function testFactoryWillUseDefaultValueForTypeHintedArgument()
165+
{
166+
$this->container->has('config')->willReturn(false);
167+
$this->container->has(ArrayAccess::class)->willReturn(false);
168+
$factory = new ReflectionBasedAbstractFactory();
169+
$instance = $factory(
170+
$this->container->reveal(),
171+
TestAsset\ClassWithTypehintedDefaultValue::class
172+
);
173+
$this->assertInstanceOf(TestAsset\ClassWithTypehintedDefaultValue::class, $instance);
174+
$this->assertNull($instance->value);
175+
}
159176
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-2018 for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-2018/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace ZendTest\ServiceManager\AbstractFactory\TestAsset;
9+
10+
use ArrayAccess;
11+
12+
class ClassWithTypehintedDefaultValue
13+
{
14+
public $value;
15+
16+
public function __construct(ArrayAccess $value = null)
17+
{
18+
$this->value = null;
19+
}
20+
}

0 commit comments

Comments
 (0)