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

Commit c62abc9

Browse files
committed
Merge branch 'hotfix/input_filters-service-config' into develop
Forward port #137
2 parents 8cfd88e + 83bbc37 commit c62abc9

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-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+
- [#137](https://github.com/zendframework/zend-inputfilter/pull/137) fixes how the
40+
`InputFilterPluginManagerFactory` 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 input filter services.
43+
This means that the `input_filters` configuration will now be honored in
44+
non-zend-mvc contexts.
4045

4146
## 2.7.3 - 2016-08-18
4247

src/InputFilterPluginManagerFactory.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Zend\InputFilter;
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 InputFilterPluginManagerFactory implements FactoryInterface
2728
*/
2829
public function __invoke(ContainerInterface $container, $name, array $options = null)
2930
{
30-
return new InputFilterPluginManager($container, $options ?: []);
31+
$pluginManager = new InputFilterPluginManager($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 input_filters configuration, nothing more to do
47+
if (! isset($config['input_filters']) || ! is_array($config['input_filters'])) {
48+
return $pluginManager;
49+
}
50+
51+
// Wire service configuration for input_filters
52+
(new Config($config['input_filters']))->configureServiceManager($pluginManager);
53+
54+
return $pluginManager;
3155
}
3256

3357
/**

test/InputFilterPluginManagerFactoryTest.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,99 @@ public function testFactoryConfiguresPluginManagerUnderServiceManagerV2($pluginT
8181
$filters = $factory->createService($container->reveal());
8282
$this->assertSame($plugin, $filters->get('test'));
8383
}
84+
85+
public function testConfiguresInputFilterServicesWhenFound()
86+
{
87+
$inputFilter = $this->prophesize(InputFilterInterface::class)->reveal();
88+
$config = [
89+
'input_filters' => [
90+
'aliases' => [
91+
'test' => 'test-too',
92+
],
93+
'factories' => [
94+
'test-too' => function ($container) use ($inputFilter) {
95+
return $inputFilter;
96+
},
97+
],
98+
],
99+
];
100+
101+
$container = $this->prophesize(ServiceLocatorInterface::class);
102+
$container->willImplement(ContainerInterface::class);
103+
104+
$container->has('ServiceListener')->willReturn(false);
105+
$container->has('config')->willReturn(true);
106+
$container->get('config')->willReturn($config);
107+
108+
$factory = new InputFilterPluginManagerFactory();
109+
$inputFilters = $factory($container->reveal(), 'InputFilterManager');
110+
111+
$this->assertInstanceOf(InputFilterPluginManager::class, $inputFilters);
112+
$this->assertTrue($inputFilters->has('test'));
113+
$this->assertSame($inputFilter, $inputFilters->get('test'));
114+
$this->assertTrue($inputFilters->has('test-too'));
115+
$this->assertSame($inputFilter, $inputFilters->get('test-too'));
116+
}
117+
118+
public function testDoesNotConfigureInputFilterServicesWhenServiceListenerPresent()
119+
{
120+
$inputFilter = $this->prophesize(InputFilterInterface::class)->reveal();
121+
$config = [
122+
'input_filters' => [
123+
'aliases' => [
124+
'test' => 'test-too',
125+
],
126+
'factories' => [
127+
'test-too' => function ($container) use ($inputFilter) {
128+
return $inputFilter;
129+
},
130+
],
131+
],
132+
];
133+
134+
$container = $this->prophesize(ServiceLocatorInterface::class);
135+
$container->willImplement(ContainerInterface::class);
136+
137+
$container->has('ServiceListener')->willReturn(true);
138+
$container->has('config')->shouldNotBeCalled();
139+
$container->get('config')->shouldNotBeCalled();
140+
141+
$factory = new InputFilterPluginManagerFactory();
142+
$inputFilters = $factory($container->reveal(), 'InputFilterManager');
143+
144+
$this->assertInstanceOf(InputFilterPluginManager::class, $inputFilters);
145+
$this->assertFalse($inputFilters->has('test'));
146+
$this->assertFalse($inputFilters->has('test-too'));
147+
}
148+
149+
public function testDoesNotConfigureInputFilterServicesWhenConfigServiceNotPresent()
150+
{
151+
$container = $this->prophesize(ServiceLocatorInterface::class);
152+
$container->willImplement(ContainerInterface::class);
153+
154+
$container->has('ServiceListener')->willReturn(false);
155+
$container->has('config')->willReturn(false);
156+
$container->get('config')->shouldNotBeCalled();
157+
158+
$factory = new InputFilterPluginManagerFactory();
159+
$inputFilters = $factory($container->reveal(), 'InputFilterManager');
160+
161+
$this->assertInstanceOf(InputFilterPluginManager::class, $inputFilters);
162+
}
163+
164+
public function testDoesNotConfigureInputFilterServicesWhenConfigServiceDoesNotContainInputFiltersConfig()
165+
{
166+
$container = $this->prophesize(ServiceLocatorInterface::class);
167+
$container->willImplement(ContainerInterface::class);
168+
169+
$container->has('ServiceListener')->willReturn(false);
170+
$container->has('config')->willReturn(true);
171+
$container->get('config')->willReturn(['foo' => 'bar']);
172+
173+
$factory = new InputFilterPluginManagerFactory();
174+
$inputFilters = $factory($container->reveal(), 'InputFilterManager');
175+
176+
$this->assertInstanceOf(InputFilterPluginManager::class, $inputFilters);
177+
$this->assertFalse($inputFilters->has('foo'));
178+
}
84179
}

0 commit comments

Comments
 (0)