Skip to content

Loading…

Use InputFilterPluginManager in InputFilter\Factory #4365

Merged
merged 5 commits into from

2 participants

@danizord

Sorry to send it at the last minute, but I think it is relevant to be released with 2.2. :smiley:

@weierophinney weierophinney commented on the diff
library/Zend/Form/FormElementManager.php
@@ -85,7 +86,13 @@ public function __construct(ConfigInterface $configuration = null)
public function injectFactory($element)
{
if ($element instanceof FormFactoryAwareInterface) {
- $element->getFormFactory()->setFormElementManager($this);
+ $factory = $element->getFormFactory();
+ $factory->setFormElementManager($this);
+
+ if ($this->serviceLocator instanceof ServiceLocatorInterface) {
+ $inputFilters = $this->serviceLocator->get('InputFilterManager');
@weierophinney Zend Framework member

You need to check with has() before pulling this; right now, form tests fail due to that. (I'll probably fix this on merge, though.)

@weierophinney Zend Framework member

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney [#4365] Fix failing tests
- Check if the composed service locator has the InputFilterManager prior
  to attempting to retrieve it.
cb77630
@weierophinney weierophinney merged commit f27d372 into zendframework:develop

1 check failed

Details default The Travis build failed
@weierophinney weierophinney was assigned
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney [#4365] Fix failing tests
- Check if the composed service locator has the InputFilterManager prior
  to attempting to retrieve it.
0231a79
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'feature/4365' into develop
Close #4365
c0e7132
@fimbulvetr fimbulvetr pushed a commit to fimbulvetr/zf2 that referenced this pull request
Dan Vande More fix for #4365 - if constructor received InputFilterManager via argume…
…nts, it called setInputFilterManager. setInputFilterManager assumed default filter/validator chains were initialized if serviceLocator was present, which is not the case, as defaultFilter/Validator are not set until later in the constructor.
64878a4
@weierophinney weierophinney added a commit to zendframework/zend-inputfilter that referenced this pull request
@weierophinney weierophinney Merge pull request zendframework/zf2#4365 from Danizord/hotfix/inputf…
…ilter-factory-use-plugin-manager

Use InputFilterPluginManager in InputFilter\Factory
806018a
@weierophinney weierophinney added a commit to zendframework/zend-inputfilter that referenced this pull request
@weierophinney weierophinney Merge branch 'feature/4365' into develop fc97a3c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
View
9 library/Zend/Form/FormElementManager.php
@@ -11,6 +11,7 @@
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
+use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\InitializableInterface;
/**
@@ -85,7 +86,13 @@ public function __construct(ConfigInterface $configuration = null)
public function injectFactory($element)
{
if ($element instanceof FormFactoryAwareInterface) {
- $element->getFormFactory()->setFormElementManager($this);
+ $factory = $element->getFormFactory();
+ $factory->setFormElementManager($this);
+
+ if ($this->serviceLocator instanceof ServiceLocatorInterface) {
+ $inputFilters = $this->serviceLocator->get('InputFilterManager');
@weierophinney Zend Framework member

You need to check with has() before pulling this; right now, form tests fail due to that. (I'll probably fix this on merge, though.)

@weierophinney Zend Framework member

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $factory->getInputFilterFactory()->setInputFilterManager($inputFilters);
+ }
}
}
View
80 library/Zend/InputFilter/Factory.php
@@ -17,11 +17,30 @@
class Factory
{
+ /**
+ * @var FilterChain
+ */
protected $defaultFilterChain;
+
+ /**
+ * @var ValidatorChain
+ */
protected $defaultValidatorChain;
- public function __construct()
+ /**
+ * @var InputFilterPluginManager
+ */
+ protected $inputFilterManager;
+
+ /**
+ * @param InputFilterPluginManager $inputFilterManager
+ */
+ public function __construct(InputFilterPluginManager $inputFilterManager = null)
{
+ if ($inputFilterManager) {
+ $this->setInputFilterManager($inputFilterManager);
+ }
+
$this->defaultFilterChain = new FilterChain();
$this->defaultValidatorChain = new ValidatorChain();
}
@@ -91,6 +110,29 @@ public function clearDefaultValidatorChain()
}
/**
+ * @param InputFilterPluginManager $inputFilterManager
+ * @return self
+ */
+ public function setInputFilterManager(InputFilterPluginManager $inputFilterManager)
+ {
+ $this->inputFilterManager = $inputFilterManager;
+
+ return $this;
+ }
+
+ /**
+ * @return InputFilterPluginManager
+ */
+ public function getInputFilterManager()
+ {
+ if (null === $this->inputFilterManager) {
+ $this->inputFilterManager = new InputFilterPluginManager;
+ }
+
+ return $this->inputFilterManager;
+ }
+
+ /**
* Factory for input objects
*
* @param array|Traversable $inputSpecification
@@ -112,8 +154,14 @@ public function createInput($inputSpecification)
}
$class = 'Zend\InputFilter\Input';
+
if (isset($inputSpecification['type'])) {
$class = $inputSpecification['type'];
+
+ if ($this->getInputFilterManager()->has($class)) {
+ return $this->createInputFilter($inputSpecification);
+ }
+
if (!class_exists($class)) {
throw new Exception\RuntimeException(sprintf(
'Input factory expects the "type" to be a valid class; received "%s"',
@@ -225,24 +273,14 @@ public function createInputFilter($inputFilterSpecification)
$inputFilterSpecification = ArrayUtils::iteratorToArray($inputFilterSpecification);
}
- $class = 'Zend\InputFilter\InputFilter';
- if (isset($inputFilterSpecification['type']) && is_string($inputFilterSpecification['type'])) {
- $class = $inputFilterSpecification['type'];
- if (!class_exists($class)) {
- throw new Exception\RuntimeException(sprintf(
- 'Input factory expects the "type" to be a valid class; received "%s"',
- $class
- ));
- }
+ $type = 'Zend\InputFilter\InputFilter';
+
+ if (isset($inputFilterSpecification['type'])) {
+ $type = $inputFilterSpecification['type'];
unset($inputFilterSpecification['type']);
}
- $inputFilter = new $class();
- if (!$inputFilter instanceof InputFilterInterface) {
- throw new Exception\RuntimeException(sprintf(
- 'InputFilter factory expects the "type" to be a class implementing %s; received "%s"',
- 'Zend\InputFilter\InputFilterInterface', $class));
- }
+ $inputFilter = $this->getInputFilterManager()->get($type);
if ($inputFilter instanceof CollectionInputFilter) {
if (isset($inputFilterSpecification['input_filter'])) {
@@ -270,6 +308,11 @@ public function createInputFilter($inputFilterSpecification)
return $inputFilter;
}
+ /**
+ * @param FilterChain $chain
+ * @param array|Traversable $filters
+ * @return void
+ */
protected function populateFilters(FilterChain $chain, $filters)
{
foreach ($filters as $filter) {
@@ -300,6 +343,11 @@ protected function populateFilters(FilterChain $chain, $filters)
}
}
+ /**
+ * @param ValidatorChain $chain
+ * @param array|Traversable $validators
+ * @return void
+ */
protected function populateValidators(ValidatorChain $chain, $validators)
{
foreach ($validators as $validator) {
View
31 library/Zend/InputFilter/InputFilterPluginManager.php
@@ -21,6 +21,16 @@
class InputFilterPluginManager extends AbstractPluginManager
{
/**
+ * Default set of plugins
+ *
+ * @var array
+ */
+ protected $invokableClasses = array(
+ 'inputfilter' => 'Zend\InputFilter\InputFilter',
+ 'collection' => 'Zend\InputFilter\CollectionInputFilter',
+ );
+
+ /**
* Whether or not to share by default
*
* @var bool
@@ -38,16 +48,21 @@ public function __construct(ConfigInterface $configuration = null)
}
/**
- * Populate the factory with filter chain and validator chain
+ * Inject this and populate the factory with filter chain and validator chain
*
- * @param $element
+ * @param $inputfilter
*/
- public function populateFactory($element)
+ public function populateFactory($inputfilter)
{
- if ($element instanceof InputFilter && $this->serviceLocator instanceof ServiceLocatorInterface) {
- $factory = $element->getFactory();
- $factory->getDefaultFilterChain()->setPluginManager($this->serviceLocator->get('FilterManager'));
- $factory->getDefaultValidatorChain()->setPluginManager($this->serviceLocator->get('ValidatorManager'));
+ 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'));
+ }
}
}
@@ -57,7 +72,7 @@ public function populateFactory($element)
public function validatePlugin($plugin)
{
if ($plugin instanceof InputFilterInterface) {
- // Hook to perform various initialization, when the element is not created through the factory
+ // Hook to perform various initialization, when the inputfilter is not created through the factory
if ($plugin instanceof InitializableInterface) {
$plugin->init();
}
Something went wrong with that request. Please try again.