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

Commit 7fd57c9

Browse files
committed
Ensure filters config is honored in non-zend-mvc contexts
Per https://discourse.zendframework.com/t/validatormanager-not-calling-custom-validator-factory/109/5?u=matthew the `filters` config key is not honored currently unless the application is within a zend-mvc context. This is due to the fact that `Zend\Filter\Module` wires configuration for the `Zend\ModuleManager\Listener\ServiceListener` in order to push merged service configuration into the plugin during bootstrap; no similar logic is available when not in a zend-mvc context, however. This patch fixes that situation by modifying the `FilterPluginManagerFactory` to do the following: - If a `ServiceListener` service exists, it returns the plugin manager immediately (old behavior). - Otherwise, it checks for the `config` service, and, if found, a `filters` key with an array value. When found, it feeds that value to a `Zend\ServiceManager\Config` instance and uses that to configure the plugin manager before returning it.
1 parent c4bb581 commit 7fd57c9

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

src/FilterPluginManagerFactory.php

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

3357
/**

test/FilterPluginManagerFactoryTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
use Interop\Container\ContainerInterface;
1111
use PHPUnit_Framework_TestCase as TestCase;
12+
use Zend\Filter\Boolean;
13+
use Zend\Filter\FilterInterface;
1214
use Zend\Filter\FilterPluginManager;
1315
use Zend\Filter\FilterPluginManagerFactory;
1416
use Zend\ServiceManager\ServiceLocatorInterface;
@@ -73,4 +75,99 @@ public function testFactoryConfiguresPluginManagerUnderServiceManagerV2()
7375
$filters = $factory->createService($container->reveal());
7476
$this->assertSame($filter, $filters->get('test'));
7577
}
78+
79+
public function testConfiguresFilterServicesWhenFound()
80+
{
81+
$filter = $this->prophesize(FilterInterface::class)->reveal();
82+
$config = [
83+
'filters' => [
84+
'aliases' => [
85+
'test' => Boolean::class,
86+
],
87+
'factories' => [
88+
'test-too' => function ($container) use ($filter) {
89+
return $filter;
90+
},
91+
],
92+
],
93+
];
94+
95+
$container = $this->prophesize(ServiceLocatorInterface::class);
96+
$container->willImplement(ContainerInterface::class);
97+
98+
$container->has('ServiceListener')->willReturn(false);
99+
$container->has('config')->willReturn(true);
100+
$container->get('config')->willReturn($config);
101+
102+
$factory = new FilterPluginManagerFactory();
103+
$filters = $factory($container->reveal(), 'FilterManager');
104+
105+
$this->assertInstanceOf(FilterPluginManager::class, $filters);
106+
$this->assertTrue($filters->has('test'));
107+
$this->assertInstanceOf(Boolean::class, $filters->get('test'));
108+
$this->assertTrue($filters->has('test-too'));
109+
$this->assertSame($filter, $filters->get('test-too'));
110+
}
111+
112+
public function testDoesNotConfigureFilterServicesWhenServiceListenerPresent()
113+
{
114+
$filter = $this->prophesize(FilterInterface::class)->reveal();
115+
$config = [
116+
'filters' => [
117+
'aliases' => [
118+
'test' => Boolean::class,
119+
],
120+
'factories' => [
121+
'test-too' => function ($container) use ($filter) {
122+
return $filter;
123+
},
124+
],
125+
],
126+
];
127+
128+
$container = $this->prophesize(ServiceLocatorInterface::class);
129+
$container->willImplement(ContainerInterface::class);
130+
131+
$container->has('ServiceListener')->willReturn(true);
132+
$container->has('config')->shouldNotBeCalled();
133+
$container->get('config')->shouldNotBeCalled();
134+
135+
$factory = new FilterPluginManagerFactory();
136+
$filters = $factory($container->reveal(), 'FilterManager');
137+
138+
$this->assertInstanceOf(FilterPluginManager::class, $filters);
139+
$this->assertFalse($filters->has('test'));
140+
$this->assertFalse($filters->has('test-too'));
141+
}
142+
143+
public function testDoesNotConfigureFilterServicesWhenConfigServiceNotPresent()
144+
{
145+
$container = $this->prophesize(ServiceLocatorInterface::class);
146+
$container->willImplement(ContainerInterface::class);
147+
148+
$container->has('ServiceListener')->willReturn(false);
149+
$container->has('config')->willReturn(false);
150+
$container->get('config')->shouldNotBeCalled();
151+
152+
$factory = new FilterPluginManagerFactory();
153+
$filters = $factory($container->reveal(), 'FilterManager');
154+
155+
$this->assertInstanceOf(FilterPluginManager::class, $filters);
156+
}
157+
158+
public function testDoesNotConfigureFilterServicesWhenConfigServiceDoesNotContainFiltersConfig()
159+
{
160+
$container = $this->prophesize(ServiceLocatorInterface::class);
161+
$container->willImplement(ContainerInterface::class);
162+
163+
$container->has('ServiceListener')->willReturn(false);
164+
$container->has('config')->willReturn(true);
165+
$container->get('config')->willReturn(['foo' => 'bar']);
166+
167+
$factory = new FilterPluginManagerFactory();
168+
$filters = $factory($container->reveal(), 'FilterManager');
169+
170+
$this->assertInstanceOf(FilterPluginManager::class, $filters);
171+
$this->assertFalse($filters->has('foo'));
172+
}
76173
}

0 commit comments

Comments
 (0)