-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,10 @@ | |
|
||
namespace Zend\InputFilter; | ||
|
||
use Interop\Container\ContainerInterface; | ||
use Zend\ServiceManager\AbstractPluginManager; | ||
use Zend\ServiceManager\ConfigInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use Zend\ServiceManager\Exception\InvalidServiceException; | ||
use Zend\ServiceManager\Factory\InvokableFactory; | ||
use Zend\Stdlib\InitializableInterface; | ||
|
||
/** | ||
|
@@ -22,55 +23,100 @@ | |
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, | ||
]; | ||
|
||
/** | ||
* Whether or not to share by default | ||
* Default set of plugins | ||
* | ||
* @var string[] | ||
*/ | ||
protected $factories = [ | ||
InputFilter::class => InvokableFactory::class, | ||
CollectionInputFilter::class => InvokableFactory::class, | ||
// v2 canonical FQCN | ||
'zendinputfilterinputfilter' => InvokableFactory::class, | ||
'zendinputfiltercollectioninputfilter' => InvokableFactory::class, | ||
]; | ||
|
||
/** | ||
* Whether or not to share by default (v3) | ||
* | ||
* @var bool | ||
*/ | ||
protected $sharedByDefault = false; | ||
|
||
/** | ||
* Whether or not to share by default (v2) | ||
* | ||
* @var bool | ||
*/ | ||
protected $shareByDefault = 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) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
weierophinney
Member
|
||
{ | ||
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 | ||
|
@@ -82,11 +128,35 @@ public function validatePlugin($plugin) | |
return; | ||
} | ||
|
||
throw new Exception\RuntimeException(sprintf( | ||
throw new InvalidServiceException(sprintf( | ||
'Plugin of type %s is invalid; must implement %s or %s', | ||
(is_object($plugin) ? get_class($plugin) : gettype($plugin)), | ||
InputFilterInterface::class, | ||
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) | ||
{ | ||
try { | ||
$this->validate($plugin); | ||
} catch (InvalidServiceException $e) { | ||
throw new Exception\RuntimeException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
public function shareByDefault() | ||
{ | ||
return $this->sharedByDefault; | ||
} | ||
} |
@gianarb this signature update is a BC break here. Also,
$container
is never used in this method, so I'm not clear about the rationale of this change. Could you please elaborate on this?Can't we make
$second
optional and default to null?/cc @weierophinney