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

[WIP] Migrate to ZF3 components #86

Closed
wants to merge 13 commits into from
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
},
"require": {
"php": ">=5.5",
"zendframework/zend-filter": "~2.5",
"zendframework/zend-validator": "^2.5.3",
"zendframework/zend-stdlib": "~2.5"
"zendframework/zend-filter": "dev-develop as 2.6.0",
"zendframework/zend-validator": "dev-develop as 2.5.3",
"zendframework/zend-stdlib": "dev-develop as 2.8.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo this. What feature exists only in 2.8?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SM updates for that component exist on the develop branch.

As you have not been involved on the other component migrations, I respectfully request that you refrain from further review of this PR; @ezimuel and I will complete this review, as we've been involved with all other component updates to date, and know the changes required intimately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weierophinney After SMv3 release composer should pick the most recent stdlib version automatically without any explicit requirement. This is important because if SM has a problem with the stdlib version then should be fixes on SM composer and don't in each component, application, ...

},
"require-dev": {
"zendframework/zend-servicemanager": "~2.5",
"zendframework/zend-servicemanager": "^2.7.3 || ^3.0",
"fabpot/php-cs-fixer": "1.7.*",
"phpunit/PHPUnit": "^4.5"
},
Expand Down
14 changes: 3 additions & 11 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Traversable;
use Zend\Filter\FilterChain;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\ValidatorChain;
use Zend\Validator\ValidatorInterface;
Expand Down Expand Up @@ -117,15 +117,7 @@ public function clearDefaultValidatorChain()
public function setInputFilterManager(InputFilterPluginManager $inputFilterManager)
{
$this->inputFilterManager = $inputFilterManager;
$serviceLocator = $this->inputFilterManager->getServiceLocator();
if ($serviceLocator && $serviceLocator instanceof ServiceLocatorInterface) {
if ($serviceLocator->has('ValidatorManager')) {
$this->getDefaultValidatorChain()->setPluginManager($serviceLocator->get('ValidatorManager'));
}
if ($serviceLocator->has('FilterManager')) {
$this->getDefaultFilterChain()->setPluginManager($serviceLocator->get('FilterManager'));
}
}
$inputFilterManager->populateFactoryPluginManagers($this);
return $this;
}

Expand All @@ -135,7 +127,7 @@ public function setInputFilterManager(InputFilterPluginManager $inputFilterManag
public function getInputFilterManager()
{
if (null === $this->inputFilterManager) {
$this->inputFilterManager = new InputFilterPluginManager;
$this->inputFilterManager = new InputFilterPluginManager(new ServiceManager());
}

return $this->inputFilterManager;
Expand Down
47 changes: 35 additions & 12 deletions src/InputFilterAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

namespace Zend\InputFilter;

use Interop\Container\ContainerInterface;
use Zend\Filter\FilterPluginManager;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Validator\ValidatorPluginManager;

class InputFilterAbstractServiceFactory implements AbstractFactoryInterface
Expand All @@ -22,17 +24,15 @@ class InputFilterAbstractServiceFactory implements AbstractFactoryInterface
protected $factory;

/**
* @param ServiceLocatorInterface $inputFilters
*
* @param ContainerInterface $services
* @param string $cName
* @param string $rName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
public function canCreate(ContainerInterface $services, $rName)
{
$services = $inputFilters->getServiceLocator();
if (! $services instanceof ServiceLocatorInterface
|| ! $services->has('Config')
) {
if (! $services->has('Config')) {
return false;
}

Expand All @@ -47,14 +47,26 @@ public function canCreateServiceWithName(ServiceLocatorInterface $inputFilters,
}

/**
* @param ServiceLocatorInterface $inputFilters
* @param string $cName
* Determine if we can create a service with name (v2)
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return $this->canCreate($serviceLocator, $requestedName);
}

/**
* @param ContainerInterface $services
* @param string $rName
* @param array $options
* @return InputFilterInterface
*/
public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
public function __invoke(ContainerInterface $services, $rName, array $options = null)
{
$services = $inputFilters->getServiceLocator();
$allConfig = $services->get('Config');
$config = $allConfig['input_filter_specs'][$rName];

Expand All @@ -63,6 +75,17 @@ public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cN
return $factory->createInputFilter($config);
}

/**
* @param ServiceLocatorInterface $inputFilters
* @param string $cName
* @param string $rName
* @return InputFilterInterface
*/
public function createServiceWithName(ServiceLocatorInterface $inputFilters, $cName, $rName)
{
return $this($inputFilters, $rName);
}

/**
* @param ServiceLocatorInterface $services
* @return Factory
Expand Down Expand Up @@ -94,7 +117,7 @@ protected function getFilterPluginManager(ServiceLocatorInterface $services)
return $services->get('FilterManager');
}

return new FilterPluginManager();
return new FilterPluginManager(new ServiceManager());
}

/**
Expand All @@ -107,6 +130,6 @@ protected function getValidatorPluginManager(ServiceLocatorInterface $services)
return $services->get('ValidatorManager');
}

return new ValidatorPluginManager();
return new ValidatorPluginManager(new ServiceManager());
}
}
88 changes: 71 additions & 17 deletions src/InputFilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace Zend\InputFilter;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Stdlib\InitializableInterface;

/**
Expand All @@ -22,55 +22,89 @@
class InputFilterPluginManager extends AbstractPluginManager
{
/**
* Default set of plugins
* Default alias of plugins
*
* @var string[]
*/
protected $invokableClasses = [
protected $aliases = [
'inputfilter' => InputFilter::class,
'inputFilter' => InputFilter::class,
'InputFilter' => InputFilter::class,
'collection' => CollectionInputFilter::class,
'Collection' => CollectionInputFilter::class,
];

/**
* Default set of plugins
*
* @var string[]
*/
protected $factories = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a docblock or inheritdoc

InputFilter::class => InvokableFactory::class,
CollectionInputFilter::class => InvokableFactory::class,
];

/**
* Whether or not to share by default
*
* @var bool
*/
protected $shareByDefault = false;
protected $sharedByDefault = false;

/**
* @param ConfigInterface $configuration
* @param ContainerInterface $parentLocator
* @param array $config
*/
public function __construct(ConfigInterface $configuration = null)
public function __construct(ContainerInterface $parentLocator, array $config = [])
{
parent::__construct($configuration);

parent::__construct($parentLocator, $config);
$this->addInitializer([$this, 'populateFactory']);
}

/**
* Inject this and populate the factory with filter chain and validator chain
*
* @param $inputFilter
* @param mixed $first
* @param mixed $second
*/
public function populateFactory($inputFilter)
public function populateFactory($first, $second)
{
if ($first instanceof ContainerInterface) {
$container = $first;
$inputFilter = $second;
} else {
$container = $second;
$inputFilter = $first;
}
if ($inputFilter instanceof InputFilter) {
$factory = $inputFilter->getFactory();

$factory->setInputFilterManager($this);
}
}

if ($this->serviceLocator instanceof ServiceLocatorInterface) {
$factory->getDefaultFilterChain()->setPluginManager($this->serviceLocator->get('FilterManager'));
$factory->getDefaultValidatorChain()->setPluginManager($this->serviceLocator->get('ValidatorManager'));
}
public function populateFactoryPluginManagers(Factory $factory)
{
if (property_exists($this, 'creationContext')) {
// v3
$container = $this->creationContext;
} else {
// v2
$container = $this->serviceLocator;
}

if ($container && $container->has('FilterManager')) {
$factory->getDefaultFilterChain()->setPluginManager($container->get('FilterManager'));
}
if ($container && $container->has('ValidatorManager')) {
$factory->getDefaultValidatorChain()->setPluginManager($container->get('ValidatorManager'));
}
}

/**
* {@inheritDoc}
* {@inheritDoc} (v3)
*/
public function validatePlugin($plugin)
public function validate($plugin)
{
if ($plugin instanceof InputFilterInterface || $plugin instanceof InputInterface) {
// Hook to perform various initialization, when the inputFilter is not created through the factory
Expand All @@ -89,4 +123,24 @@ public function validatePlugin($plugin)
InputInterface::class
));
}

/**
* Validate the plugin (v2)
*
* Checks that the filter loaded is either a valid callback or an instance
* of FilterInterface.
*
* @param mixed $plugin
* @return void
* @throws Exception\RuntimeException if invalid
*/
public function validatePlugin($plugin)
{
$this->validate($plugin);
}

public function shareByDefault()
{
return $this->sharedByDefault;
}
}
Loading