11
11
12
12
use Interop \Container \ContainerInterface ;
13
13
use Zend \ServiceManager \AbstractPluginManager ;
14
+ use Zend \ServiceManager \Exception \InvalidServiceException ;
14
15
use Zend \Stdlib \ArrayUtils ;
15
16
16
17
/**
@@ -33,32 +34,94 @@ class RoutePluginManager extends AbstractPluginManager
33
34
protected $ instanceOf = RouteInterface::class;
34
35
35
36
/**
36
- * Do not share instances.
37
+ * Do not share instances. (v3)
37
38
*
38
39
* @var bool
39
40
*/
40
41
protected $ shareByDefault = false ;
41
42
43
+ /**
44
+ * Do not share instances. (v2)
45
+ *
46
+ * @var bool
47
+ */
48
+ protected $ sharedByDefault = false ;
49
+
42
50
/**
43
51
* Constructor
44
52
*
45
53
* Ensure that the instance is seeded with the RouteInvokableFactory as an
46
54
* abstract factory.
47
55
*
48
- * @param ContainerInterface $container
49
- * @param array $config
56
+ * @param ContainerInterface|\Zend\ServiceManager\ConfigInterface $configOrContainerInstance
57
+ * @param array $v3config
58
+ */
59
+ public function __construct ($ configOrContainerInstance , array $ v3config = [])
60
+ {
61
+ $ this ->addAbstractFactory (RouteInvokableFactory::class);
62
+ parent ::__construct ($ configOrContainerInstance , $ v3config );
63
+ }
64
+
65
+ /**
66
+ * Validate a route plugin. (v2)
67
+ *
68
+ * @param object $plugin
69
+ * @throws InvalidServiceException
70
+ */
71
+ public function validate ($ plugin )
72
+ {
73
+ if (! $ plugin instanceof $ this ->instanceOf ) {
74
+ throw new InvalidServiceException (sprintf (
75
+ 'Plugin of type %s is invalid; must implement %s ' ,
76
+ (is_object ($ plugin ) ? get_class ($ plugin ) : gettype ($ plugin )),
77
+ RouteInterface::class
78
+ ));
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Validate a route plugin. (v2)
84
+ *
85
+ * @param object $plugin
86
+ * @throws Exception\RuntimeException
50
87
*/
51
- public function __construct ( ContainerInterface $ container , array $ config = [] )
88
+ public function validatePlugin ( $ plugin )
52
89
{
53
- $ config = ArrayUtils::merge (['abstract_factories ' => [
54
- RouteInvokableFactory::class,
55
- ]], $ config );
90
+ try {
91
+ $ this ->validate ($ plugin );
92
+ } catch (InvalidServiceException $ e ) {
93
+ throw new Exception \RuntimeException (
94
+ $ e ->getMessage (),
95
+ $ e ->getCode (),
96
+ $ e
97
+ );
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Register an invokable class. (v2)
103
+ *
104
+ * Create invokable factories + optional aliases for an invokable class.
105
+ *
106
+ * @param string $name
107
+ * @param string $class
108
+ * @return self
109
+ */
110
+ public function setInvokableClass ($ name , $ class )
111
+ {
112
+ foreach ($ this ->createAliasesForInvokables ([$ name => $ class ]) as $ name => $ class ) {
113
+ $ this ->setAlias ($ name , $ class );
114
+ }
115
+
116
+ foreach ($ this ->createFactoriesForInvokables ([$ name => $ class ]) as $ name => $ factory ) {
117
+ $ this ->setFactory ($ name , $ factory );
118
+ }
56
119
57
- parent :: __construct ( $ container , $ config ) ;
120
+ return $ this ;
58
121
}
59
122
60
123
/**
61
- * Pre-process configuration.
124
+ * Pre-process configuration. (v3)
62
125
*
63
126
* Checks for invokables, and, if found, maps them to the
64
127
* component-specific RouteInvokableFactory; removes the invokables entry
0 commit comments