Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 30 additions & 33 deletions src/InputFilterAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Interop\Container\ContainerInterface;
use Zend\Filter\FilterPluginManager;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Validator\ValidatorPluginManager;

Expand Down Expand Up @@ -63,48 +64,44 @@ public function canCreate(ContainerInterface $services, $rName)
/**
* Determine if we can create a service with name (v2)
*
* @param ServiceLocatorInterface $serviceLocator
* @param ServiceLocatorInterface $container
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName)
{
// v2 => need to get parent service locator
$services = $serviceLocator->getServiceLocator();

// No parent locator => cannot create service.
if (! $services) {
return false;
// v2 => may need to get parent service locator
if ($container instanceof AbstractPluginManager) {
$container = $container->getServiceLocator() ?: $container;
}

return $this->canCreate($services, $requestedName);
return $this->canCreate($container, $requestedName);
}

/**
* @param ServiceLocatorInterface $inputFilters
* Create the requested service (v2)
*
* @param ServiceLocatorInterface $container
* @param string $cName
* @param string $rName
* @return InputFilterInterface
*/
public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
public function createServiceWithName(ServiceLocatorInterface $container, $cName, $rName)
{
// v2 => need to get parent service locator
$services = $inputFilters->getServiceLocator();

// No parent locator => cannot create service.
if (! $services) {
return false;
// v2 => may need to get parent service locator
if ($container instanceof AbstractPluginManager) {
$container = $container->getServiceLocator() ?: $container;
}

return $this($services, $rName);
return $this($container, $rName);
}

/**
* @param ServiceLocatorInterface $services
* @param ContainerInterface $container
* @return Factory
*/
protected function getInputFilterFactory(ServiceLocatorInterface $services)
protected function getInputFilterFactory(ContainerInterface $container)
{
if ($this->factory instanceof Factory) {
return $this->factory;
Expand All @@ -113,39 +110,39 @@ protected function getInputFilterFactory(ServiceLocatorInterface $services)
$this->factory = new Factory();
$this->factory
->getDefaultFilterChain()
->setPluginManager($this->getFilterPluginManager($services));
->setPluginManager($this->getFilterPluginManager($container));
$this->factory
->getDefaultValidatorChain()
->setPluginManager($this->getValidatorPluginManager($services));
->setPluginManager($this->getValidatorPluginManager($container));

$this->factory->setInputFilterManager($services->get('InputFilterManager'));
$this->factory->setInputFilterManager($container->get('InputFilterManager'));

return $this->factory;
}

/**
* @param ServiceLocatorInterface $services
* @param ContainerInterface $container
* @return FilterPluginManager
*/
protected function getFilterPluginManager(ServiceLocatorInterface $services)
protected function getFilterPluginManager(ContainerInterface $container)
{
if ($services->has('FilterManager')) {
return $services->get('FilterManager');
if ($container->has('FilterManager')) {
return $container->get('FilterManager');
}

return new FilterPluginManager($services);
return new FilterPluginManager($container);
}

/**
* @param ServiceLocatorInterface $services
* @param ContainerInterface $container
* @return ValidatorPluginManager
*/
protected function getValidatorPluginManager(ServiceLocatorInterface $services)
protected function getValidatorPluginManager(ContainerInterface $container)
{
if ($services->has('ValidatorManager')) {
return $services->get('ValidatorManager');
if ($container->has('ValidatorManager')) {
return $container->get('ValidatorManager');
}

return new ValidatorPluginManager($services);
return new ValidatorPluginManager($container);
}
}
27 changes: 27 additions & 0 deletions test/InputFilterAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,31 @@ public function testInjectsInputFilterManagerFromServiceManager()
$this->assertInstanceOf('Zend\InputFilter\InputFilterPluginManager', $inputFilterManager);
$this->assertInstanceOf('ZendTest\InputFilter\TestAsset\Foo', $inputFilterManager->get('foo'));
}

/**
* @group 123
*/
public function testAllowsPassingNonPluginManagerContainerToFactoryWithServiceManagerV2()
{
$this->services->setService('config', [
'input_filter_specs' => [
'filter' => [],
],
]);
if (method_exists($this->services, 'configure')) {
// v3
$canCreate = 'canCreate';
$create = '__invoke';
$args = [$this->services, 'filter'];
} else {
// v2
$canCreate = 'canCreateServiceWithName';
$create = 'createServiceWithName';
$args = [$this->services, 'filter', 'filter'];
}

$this->assertTrue(call_user_func_array([$this->factory, $canCreate], $args));
$inputFilter = call_user_func_array([$this->factory, $create], $args);
$this->assertInstanceOf(InputFilterInterface::class, $inputFilter);
}
}