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

Commit f170072

Browse files
committed
Use default value for scalars if present.
1 parent aae6c20 commit f170072

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

doc/book/reflection-abstract-factory.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ The factory operates with the following constraints/features:
3636
application "config" service (i.e., the merged configuration).
3737
- Parameters typehinted against array, but not named `$config`, will
3838
be injected with an empty array.
39-
- Scalar parameters will result in the factory raising an exception.
39+
- Scalar parameters will result in the factory raising an exception,
40+
unless a default value is present; if it is, that value will be used.
4041
- If a service cannot be found for a given typehint, the factory will
4142
raise an exception detailing this.
4243

src/AbstractFactory/ReflectionBasedAbstractFactory.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
* application "config" service (i.e., the merged configuration).
5050
* - Parameters type-hinted against array, but not named `$config` will
5151
* be injected with an empty array.
52-
* - Scalar parameters will result in an exception being thrown.
52+
* - Scalar parameters will result in an exception being thrown, unless
53+
* a default value is present; if the default is present, that will be used.
5354
* - If a service cannot be found for a given typehint, the factory will
5455
* raise an exception detailing this.
5556
* - Some services provided by Zend Framework components do not have
@@ -202,12 +203,16 @@ private function resolveParameter(ReflectionParameter $parameter, ContainerInter
202203
}
203204

204205
if (! $parameter->getClass()) {
205-
throw new ServiceNotFoundException(sprintf(
206-
'Unable to create service "%s"; unable to resolve parameter "%s" '
207-
. 'to a class, interface, or array type',
208-
$requestedName,
209-
$parameter->getName()
210-
));
206+
if (! $parameter->isDefaultValueAvailable()) {
207+
throw new ServiceNotFoundException(sprintf(
208+
'Unable to create service "%s"; unable to resolve parameter "%s" '
209+
. 'to a class, interface, or array type',
210+
$requestedName,
211+
$parameter->getName()
212+
));
213+
}
214+
215+
return $parameter->getDefaultValue();
211216
}
212217

213218
$type = $parameter->getClass()->getName();

test/AbstractFactory/ReflectionBasedAbstractFactoryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,13 @@ public function testFactoryCanSupplyAMixOfParameterTypes()
146146
$this->assertSame($sample, $instance->sample);
147147
$this->assertSame($validators, $instance->validators);
148148
}
149+
150+
public function testFactoryWillUseDefaultValueWhenPresentForScalarArgument()
151+
{
152+
$this->container->has('config')->willReturn(false);
153+
$factory = new ReflectionBasedAbstractFactory();
154+
$instance = $factory($this->container->reveal(), TestAsset\ClassWithScalarDependencyDefiningDefaultValue::class);
155+
$this->assertInstanceOf(TestAsset\ClassWithScalarDependencyDefiningDefaultValue::class, $instance);
156+
$this->assertEquals('bar', $instance->foo);
157+
}
149158
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
4+
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
5+
* @license http://framework.zend.com/license/new-bsd New BSD License
6+
*/
7+
8+
namespace ZendTest\ServiceManager\AbstractFactory\TestAsset;
9+
10+
class ClassWithScalarDependencyDefiningDefaultValue
11+
{
12+
public $foo;
13+
14+
/**
15+
* @param string $foo
16+
*/
17+
public function __construct($foo = 'bar')
18+
{
19+
$this->foo = $foo;
20+
}
21+
}

0 commit comments

Comments
 (0)