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

Commit 09dab83

Browse files
committed
Merge branch 'hotfix/hydrators-service-config' into develop
Forward port #59
2 parents 51a18f5 + 071ccfd commit 09dab83

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ All notable changes to this project will be documented in this file, in reverse
3636

3737
### Fixed
3838

39-
- Nothing.
39+
- [#59](https://github.com/zendframework/zend-hydrator/pull/59) fixes how the
40+
`HydratorPluginManagerFactory` factory initializes the plugin manager
41+
instance, ensuring it is injecting the relevant configuration from the
42+
`config` service and thus seeding it with configured hydrator services. This
43+
means that the `hydrators` configuration will now be honored in non-zend-mvc
44+
contexts.
4045

4146
## 2.2.1 - 2016-04-18
4247

src/HydratorPluginManagerFactory.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Zend\Hydrator;
99

1010
use Interop\Container\ContainerInterface;
11+
use Zend\ServiceManager\Config;
1112
use Zend\ServiceManager\FactoryInterface;
1213
use Zend\ServiceManager\ServiceLocatorInterface;
1314

@@ -27,7 +28,30 @@ class HydratorPluginManagerFactory implements FactoryInterface
2728
*/
2829
public function __invoke(ContainerInterface $container, $name, array $options = null)
2930
{
30-
return new HydratorPluginManager($container, $options ?: []);
31+
$pluginManager = new HydratorPluginManager($container, $options ?: []);
32+
33+
// If this is in a zend-mvc application, the ServiceListener will inject
34+
// merged configuration during bootstrap.
35+
if ($container->has('ServiceListener')) {
36+
return $pluginManager;
37+
}
38+
39+
// If we do not have a config service, nothing more to do
40+
if (! $container->has('config')) {
41+
return $pluginManager;
42+
}
43+
44+
$config = $container->get('config');
45+
46+
// If we do not have hydrators configuration, nothing more to do
47+
if (! isset($config['hydrators']) || ! is_array($config['hydrators'])) {
48+
return $pluginManager;
49+
}
50+
51+
// Wire service configuration for hydrators
52+
(new Config($config['hydrators']))->configureServiceManager($pluginManager);
53+
54+
return $pluginManager;
3155
}
3256

3357
/**

test/HydratorPluginManagerFactoryTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Zend\Hydrator\HydratorInterface;
1313
use Zend\Hydrator\HydratorPluginManager;
1414
use Zend\Hydrator\HydratorPluginManagerFactory;
15+
use Zend\Hydrator\Reflection;
1516
use Zend\ServiceManager\ServiceLocatorInterface;
1617

1718
class HydratorPluginManagerFactoryTest extends TestCase
@@ -70,4 +71,99 @@ public function testFactoryConfiguresPluginManagerUnderServiceManagerV2()
7071
$hydrators = $factory->createService($container->reveal());
7172
$this->assertSame($hydrator, $hydrators->get('test'));
7273
}
74+
75+
public function testConfiguresHydratorServicesWhenFound()
76+
{
77+
$hydrator = $this->prophesize(HydratorInterface::class)->reveal();
78+
$config = [
79+
'hydrators' => [
80+
'aliases' => [
81+
'test' => Reflection::class,
82+
],
83+
'factories' => [
84+
'test-too' => function ($container) use ($hydrator) {
85+
return $hydrator;
86+
},
87+
],
88+
],
89+
];
90+
91+
$container = $this->prophesize(ServiceLocatorInterface::class);
92+
$container->willImplement(ContainerInterface::class);
93+
94+
$container->has('ServiceListener')->willReturn(false);
95+
$container->has('config')->willReturn(true);
96+
$container->get('config')->willReturn($config);
97+
98+
$factory = new HydratorPluginManagerFactory();
99+
$hydrators = $factory($container->reveal(), 'HydratorManager');
100+
101+
$this->assertInstanceOf(HydratorPluginManager::class, $hydrators);
102+
$this->assertTrue($hydrators->has('test'));
103+
$this->assertInstanceOf(Reflection::class, $hydrators->get('test'));
104+
$this->assertTrue($hydrators->has('test-too'));
105+
$this->assertSame($hydrator, $hydrators->get('test-too'));
106+
}
107+
108+
public function testDoesNotConfigureHydratorServicesWhenServiceListenerPresent()
109+
{
110+
$hydrator = $this->prophesize(HydratorInterface::class)->reveal();
111+
$config = [
112+
'hydrators' => [
113+
'aliases' => [
114+
'test' => Reflection::class,
115+
],
116+
'factories' => [
117+
'test-too' => function ($container) use ($hydrator) {
118+
return $hydrator;
119+
},
120+
],
121+
],
122+
];
123+
124+
$container = $this->prophesize(ServiceLocatorInterface::class);
125+
$container->willImplement(ContainerInterface::class);
126+
127+
$container->has('ServiceListener')->willReturn(true);
128+
$container->has('config')->shouldNotBeCalled();
129+
$container->get('config')->shouldNotBeCalled();
130+
131+
$factory = new HydratorPluginManagerFactory();
132+
$hydrators = $factory($container->reveal(), 'HydratorManager');
133+
134+
$this->assertInstanceOf(HydratorPluginManager::class, $hydrators);
135+
$this->assertFalse($hydrators->has('test'));
136+
$this->assertFalse($hydrators->has('test-too'));
137+
}
138+
139+
public function testDoesNotConfigureHydratorServicesWhenConfigServiceNotPresent()
140+
{
141+
$container = $this->prophesize(ServiceLocatorInterface::class);
142+
$container->willImplement(ContainerInterface::class);
143+
144+
$container->has('ServiceListener')->willReturn(false);
145+
$container->has('config')->willReturn(false);
146+
$container->get('config')->shouldNotBeCalled();
147+
148+
$factory = new HydratorPluginManagerFactory();
149+
$hydrators = $factory($container->reveal(), 'HydratorManager');
150+
151+
$this->assertInstanceOf(HydratorPluginManager::class, $hydrators);
152+
}
153+
154+
public function testDoesNotConfigureHydratorServicesWhenConfigServiceDoesNotContainHydratorsConfig()
155+
{
156+
$container = $this->prophesize(ServiceLocatorInterface::class);
157+
$container->willImplement(ContainerInterface::class);
158+
159+
$container->has('ServiceListener')->willReturn(false);
160+
$container->has('config')->willReturn(true);
161+
$container->get('config')->willReturn(['foo' => 'bar']);
162+
163+
$factory = new HydratorPluginManagerFactory();
164+
$hydrators = $factory($container->reveal(), 'HydratorManager');
165+
166+
$this->assertInstanceOf(HydratorPluginManager::class, $hydrators);
167+
$this->assertFalse($hydrators->has('foo'));
168+
}
73169
}

0 commit comments

Comments
 (0)