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

Commit 2c29e8b

Browse files
committed
Merge branch 'hotfix/filters-service-config'
Close #56
2 parents c4bb581 + a0404b2 commit 2c29e8b

File tree

3 files changed

+127
-2
lines changed

3 files changed

+127
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#56](https://github.com/zendframework/zend-filter/pull/56) fixes how the
22+
`FilterPluginManagerFactory` factory initializes the plugin manager instance,
23+
ensuring it is injecting the relevant configuration from the `config` service
24+
and thus seeding it with configured translator loader services. This means
25+
that the `filters` configuration will now be honored in non-zend-mvc contexts.
2226

2327
## 2.7.1 - 2016-04-18
2428

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)