99
1010namespace Zend \InputFilter ;
1111
12+ use Interop \Container \ContainerInterface ;
1213use Zend \ServiceManager \AbstractPluginManager ;
13- use Zend \ServiceManager \ConfigInterface ;
14- use Zend \ServiceManager \ServiceLocatorInterface ;
14+ use Zend \ServiceManager \Exception \ InvalidServiceException ;
15+ use Zend \ServiceManager \Factory \ InvokableFactory ;
1516use Zend \Stdlib \InitializableInterface ;
1617
1718/**
2223class InputFilterPluginManager extends AbstractPluginManager
2324{
2425 /**
25- * Default set of plugins
26+ * Default alias of plugins
2627 *
2728 * @var string[]
2829 */
29- protected $ invokableClasses = [
30+ protected $ aliases = [
3031 'inputfilter ' => InputFilter::class,
32+ 'inputFilter ' => InputFilter::class,
33+ 'InputFilter ' => InputFilter::class,
3134 'collection ' => CollectionInputFilter::class,
35+ 'Collection ' => CollectionInputFilter::class,
3236 ];
3337
3438 /**
35- * Whether or not to share by default
39+ * Default set of plugins
40+ *
41+ * @var string[]
42+ */
43+ protected $ factories = [
44+ InputFilter::class => InvokableFactory::class,
45+ CollectionInputFilter::class => InvokableFactory::class,
46+ // v2 canonical FQCN
47+ 'zendinputfilterinputfilter ' => InvokableFactory::class,
48+ 'zendinputfiltercollectioninputfilter ' => InvokableFactory::class,
49+ ];
50+
51+ /**
52+ * Whether or not to share by default (v3)
53+ *
54+ * @var bool
55+ */
56+ protected $ sharedByDefault = false ;
57+
58+ /**
59+ * Whether or not to share by default (v2)
3660 *
3761 * @var bool
3862 */
3963 protected $ shareByDefault = false ;
4064
4165 /**
42- * @param ConfigInterface $configuration
66+ * @param null|\Zend\ServiceManager\ConfigInterface|ContainerInterface $configOrContainer
67+ * For zend-servicemanager v2, null or a ConfigInterface instance are
68+ * allowed; for v3, a ContainerInterface is expected.
69+ * @param array $v3config Optional configuration array (zend-servicemanager v3 only)
4370 */
44- public function __construct (ConfigInterface $ configuration = null )
71+ public function __construct ($ configOrContainer = null , array $ v3config = [] )
4572 {
46- parent ::__construct ($ configuration );
47-
48- $ this ->addInitializer ([$ this , 'populateFactory ' ]);
73+ $ this ->initializers [] = [$ this , 'populateFactory ' ];
74+ parent ::__construct ($ configOrContainer , $ v3config );
4975 }
5076
5177 /**
5278 * Inject this and populate the factory with filter chain and validator chain
5379 *
54- * @param $inputFilter
80+ * @param mixed $first
81+ * @param mixed $second
5582 */
56- public function populateFactory ($ inputFilter )
83+ public function populateFactory ($ first , $ second )
5784 {
85+ if ($ first instanceof ContainerInterface) {
86+ $ container = $ first ;
87+ $ inputFilter = $ second ;
88+ } else {
89+ $ container = $ second ;
90+ $ inputFilter = $ first ;
91+ }
5892 if ($ inputFilter instanceof InputFilter) {
5993 $ factory = $ inputFilter ->getFactory ();
6094
6195 $ factory ->setInputFilterManager ($ this );
96+ }
97+ }
6298
63- if ($ this ->serviceLocator instanceof ServiceLocatorInterface) {
64- $ factory ->getDefaultFilterChain ()->setPluginManager ($ this ->serviceLocator ->get ('FilterManager ' ));
65- $ factory ->getDefaultValidatorChain ()->setPluginManager ($ this ->serviceLocator ->get ('ValidatorManager ' ));
66- }
99+ /**
100+ * Populate the filter and validator managers for the default filter/validator chains.
101+ *
102+ * @param Factory $factory
103+ * @return void
104+ */
105+ public function populateFactoryPluginManagers (Factory $ factory )
106+ {
107+ $ container = property_exists ($ this , 'creationContext ' )
108+ ? $ this ->creationContext // v3
109+ : $ this ->serviceLocator ; // v2
110+
111+ if ($ container && $ container ->has ('FilterManager ' )) {
112+ $ factory ->getDefaultFilterChain ()->setPluginManager ($ container ->get ('FilterManager ' ));
113+ }
114+
115+ if ($ container && $ container ->has ('ValidatorManager ' )) {
116+ $ factory ->getDefaultValidatorChain ()->setPluginManager ($ container ->get ('ValidatorManager ' ));
67117 }
68118 }
69119
70120 /**
71- * {@inheritDoc}
121+ * {@inheritDoc} (v3)
72122 */
73- public function validatePlugin ($ plugin )
123+ public function validate ($ plugin )
74124 {
75125 if ($ plugin instanceof InputFilterInterface || $ plugin instanceof InputInterface) {
76126 // Hook to perform various initialization, when the inputFilter is not created through the factory
@@ -82,11 +132,30 @@ public function validatePlugin($plugin)
82132 return ;
83133 }
84134
85- throw new Exception \ RuntimeException (sprintf (
135+ throw new InvalidServiceException (sprintf (
86136 'Plugin of type %s is invalid; must implement %s or %s ' ,
87137 (is_object ($ plugin ) ? get_class ($ plugin ) : gettype ($ plugin )),
88138 InputFilterInterface::class,
89139 InputInterface::class
90140 ));
91141 }
142+
143+ /**
144+ * Validate the plugin (v2)
145+ *
146+ * Checks that the filter loaded is either a valid callback or an instance
147+ * of FilterInterface.
148+ *
149+ * @param mixed $plugin
150+ * @return void
151+ * @throws Exception\RuntimeException if invalid
152+ */
153+ public function validatePlugin ($ plugin )
154+ {
155+ try {
156+ $ this ->validate ($ plugin );
157+ } catch (InvalidServiceException $ e ) {
158+ throw new Exception \RuntimeException ($ e ->getMessage (), $ e ->getCode (), $ e );
159+ }
160+ }
92161}
0 commit comments