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

Commit 5630ef4

Browse files
committed
Merge branch 'hotfix/123'
Close #110 Fixes zendframework/zend-servicemanager#123
2 parents 1e37715 + 2bae3f0 commit 5630ef4

File tree

3 files changed

+62
-35
lines changed

3 files changed

+62
-35
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.7.2 - TBD
5+
## 2.7.2 - 2016-06-11
66

77
### Added
88

@@ -19,7 +19,10 @@ All notable changes to this project will be documented in this file, in reverse
1919

2020
### Fixed
2121

22-
- Nothing.
22+
- [#110](https://github.com/zendframework/zend-inputfilter/pull/110) fixes an
23+
issue with `InputFilterAbstractServiceFactory` whereby it was not working when
24+
the provided container is not a plugin manager, but rather the application
25+
container.
2326

2427
## 2.7.1 - 2016-04-18
2528

src/InputFilterAbstractServiceFactory.php

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Interop\Container\ContainerInterface;
1313
use Zend\Filter\FilterPluginManager;
1414
use Zend\ServiceManager\AbstractFactoryInterface;
15+
use Zend\ServiceManager\AbstractPluginManager;
1516
use Zend\ServiceManager\ServiceLocatorInterface;
1617
use Zend\Validator\ValidatorPluginManager;
1718

@@ -63,48 +64,44 @@ public function canCreate(ContainerInterface $services, $rName)
6364
/**
6465
* Determine if we can create a service with name (v2)
6566
*
66-
* @param ServiceLocatorInterface $serviceLocator
67+
* @param ServiceLocatorInterface $container
6768
* @param $name
6869
* @param $requestedName
6970
* @return bool
7071
*/
71-
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
72+
public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName)
7273
{
73-
// v2 => need to get parent service locator
74-
$services = $serviceLocator->getServiceLocator();
75-
76-
// No parent locator => cannot create service.
77-
if (! $services) {
78-
return false;
74+
// v2 => may need to get parent service locator
75+
if ($container instanceof AbstractPluginManager) {
76+
$container = $container->getServiceLocator() ?: $container;
7977
}
8078

81-
return $this->canCreate($services, $requestedName);
79+
return $this->canCreate($container, $requestedName);
8280
}
8381

8482
/**
85-
* @param ServiceLocatorInterface $inputFilters
83+
* Create the requested service (v2)
84+
*
85+
* @param ServiceLocatorInterface $container
8686
* @param string $cName
8787
* @param string $rName
8888
* @return InputFilterInterface
8989
*/
90-
public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
90+
public function createServiceWithName(ServiceLocatorInterface $container, $cName, $rName)
9191
{
92-
// v2 => need to get parent service locator
93-
$services = $inputFilters->getServiceLocator();
94-
95-
// No parent locator => cannot create service.
96-
if (! $services) {
97-
return false;
92+
// v2 => may need to get parent service locator
93+
if ($container instanceof AbstractPluginManager) {
94+
$container = $container->getServiceLocator() ?: $container;
9895
}
9996

100-
return $this($services, $rName);
97+
return $this($container, $rName);
10198
}
10299

103100
/**
104-
* @param ServiceLocatorInterface $services
101+
* @param ContainerInterface $container
105102
* @return Factory
106103
*/
107-
protected function getInputFilterFactory(ServiceLocatorInterface $services)
104+
protected function getInputFilterFactory(ContainerInterface $container)
108105
{
109106
if ($this->factory instanceof Factory) {
110107
return $this->factory;
@@ -113,39 +110,39 @@ protected function getInputFilterFactory(ServiceLocatorInterface $services)
113110
$this->factory = new Factory();
114111
$this->factory
115112
->getDefaultFilterChain()
116-
->setPluginManager($this->getFilterPluginManager($services));
113+
->setPluginManager($this->getFilterPluginManager($container));
117114
$this->factory
118115
->getDefaultValidatorChain()
119-
->setPluginManager($this->getValidatorPluginManager($services));
116+
->setPluginManager($this->getValidatorPluginManager($container));
120117

121-
$this->factory->setInputFilterManager($services->get('InputFilterManager'));
118+
$this->factory->setInputFilterManager($container->get('InputFilterManager'));
122119

123120
return $this->factory;
124121
}
125122

126123
/**
127-
* @param ServiceLocatorInterface $services
124+
* @param ContainerInterface $container
128125
* @return FilterPluginManager
129126
*/
130-
protected function getFilterPluginManager(ServiceLocatorInterface $services)
127+
protected function getFilterPluginManager(ContainerInterface $container)
131128
{
132-
if ($services->has('FilterManager')) {
133-
return $services->get('FilterManager');
129+
if ($container->has('FilterManager')) {
130+
return $container->get('FilterManager');
134131
}
135132

136-
return new FilterPluginManager($services);
133+
return new FilterPluginManager($container);
137134
}
138135

139136
/**
140-
* @param ServiceLocatorInterface $services
137+
* @param ContainerInterface $container
141138
* @return ValidatorPluginManager
142139
*/
143-
protected function getValidatorPluginManager(ServiceLocatorInterface $services)
140+
protected function getValidatorPluginManager(ContainerInterface $container)
144141
{
145-
if ($services->has('ValidatorManager')) {
146-
return $services->get('ValidatorManager');
142+
if ($container->has('ValidatorManager')) {
143+
return $container->get('ValidatorManager');
147144
}
148145

149-
return new ValidatorPluginManager($services);
146+
return new ValidatorPluginManager($container);
150147
}
151148
}

test/InputFilterAbstractServiceFactoryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,31 @@ public function testInjectsInputFilterManagerFromServiceManager()
274274
$this->assertInstanceOf('Zend\InputFilter\InputFilterPluginManager', $inputFilterManager);
275275
$this->assertInstanceOf('ZendTest\InputFilter\TestAsset\Foo', $inputFilterManager->get('foo'));
276276
}
277+
278+
/**
279+
* @group zendframework/zend-servicemanager#123
280+
*/
281+
public function testAllowsPassingNonPluginManagerContainerToFactoryWithServiceManagerV2()
282+
{
283+
$this->services->setService('config', [
284+
'input_filter_specs' => [
285+
'filter' => [],
286+
],
287+
]);
288+
if (method_exists($this->services, 'configure')) {
289+
// v3
290+
$canCreate = 'canCreate';
291+
$create = '__invoke';
292+
$args = [$this->services, 'filter'];
293+
} else {
294+
// v2
295+
$canCreate = 'canCreateServiceWithName';
296+
$create = 'createServiceWithName';
297+
$args = [$this->services, 'filter', 'filter'];
298+
}
299+
300+
$this->assertTrue(call_user_func_array([$this->factory, $canCreate], $args));
301+
$inputFilter = call_user_func_array([$this->factory, $create], $args);
302+
$this->assertInstanceOf(InputFilterInterface::class, $inputFilter);
303+
}
277304
}

0 commit comments

Comments
 (0)