9
9
10
10
use Interop \Container \ContainerInterface ;
11
11
use ReflectionClass ;
12
+ use ReflectionParameter ;
12
13
use Zend \Console \Adapter \AdapterInterface as ConsoleAdapterInterface ;
13
14
use Zend \Filter \FilterPluginManager ;
14
15
use Zend \Hydrator \HydratorPluginManager ;
@@ -113,40 +114,9 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
113
114
return new $ requestedName ();
114
115
}
115
116
116
- $ parameters = [];
117
- foreach ($ reflectionParameters as $ parameter ) {
118
- if ($ parameter ->isArray ()
119
- && $ parameter ->getName () === 'config '
120
- && $ container ->has ('config ' )
121
- ) {
122
- $ parameters [] = $ container ->get ('config ' );
123
- continue ;
124
- }
125
-
126
- if ($ parameter ->isArray ()) {
127
- $ parameters [] = [];
128
- continue ;
129
- }
130
-
131
- if (! $ parameter ->getClass ()) {
132
- $ parameters [] = null ;
133
- continue ;
134
- }
135
-
136
- $ type = $ parameter ->getClass ()->getName ();
137
- $ type = isset ($ this ->aliases [$ type ]) ? $ this ->aliases [$ type ] : $ type ;
138
-
139
- if (! $ container ->has ($ type )) {
140
- throw new ServiceNotFoundException (sprintf (
141
- 'Unable to create controller "%s"; unable to resolve parameter "%s" using type hint "%s" ' ,
142
- $ requestedName ,
143
- $ parameter ->getName (),
144
- $ type
145
- ));
146
- }
147
-
148
- $ parameters [] = $ container ->get ($ type );
149
- }
117
+ $ parameters = array_map (function (ReflectionParameter $ parameter ) use ($ container , $ requestedName ) {
118
+ return $ this ->resolveParameter ($ parameter , $ container , $ requestedName );
119
+ }, $ reflectionParameters );
150
120
151
121
return new $ requestedName (...$ parameters );
152
122
}
@@ -162,4 +132,46 @@ public function canCreate(ContainerInterface $container, $requestedName)
162
132
163
133
return in_array (DispatchableInterface::class, class_implements ($ requestedName ), true );
164
134
}
135
+
136
+ /**
137
+ * Resolve a parameter to a value.
138
+ *
139
+ * @param ReflectionClass $parameter
140
+ * @param ContainerInterface $container
141
+ * @param string $requestedName
142
+ * @return mixed
143
+ * @throws ServiceNotFoundException If type-hinted parameter cannot be
144
+ * resolved to a service in the container.
145
+ */
146
+ private function resolveParameter (ReflectionParameter $ parameter , ContainerInterface $ container , $ requestedName )
147
+ {
148
+ if ($ parameter ->isArray ()
149
+ && $ parameter ->getName () === 'config '
150
+ && $ container ->has ('config ' )
151
+ ) {
152
+ return $ container ->get ('config ' );
153
+ }
154
+
155
+ if ($ parameter ->isArray ()) {
156
+ return [];
157
+ }
158
+
159
+ if (! $ parameter ->getClass ()) {
160
+ return ;
161
+ }
162
+
163
+ $ type = $ parameter ->getClass ()->getName ();
164
+ $ type = isset ($ this ->aliases [$ type ]) ? $ this ->aliases [$ type ] : $ type ;
165
+
166
+ if (! $ container ->has ($ type )) {
167
+ throw new ServiceNotFoundException (sprintf (
168
+ 'Unable to create controller "%s"; unable to resolve parameter "%s" using type hint "%s" ' ,
169
+ $ requestedName ,
170
+ $ parameter ->getName (),
171
+ $ type
172
+ ));
173
+ }
174
+
175
+ return $ container ->get ($ type );
176
+ }
165
177
}
0 commit comments