@@ -172,7 +172,7 @@ protected static function build($type, $parameters = array())
172
172
return new $ type ;
173
173
}
174
174
175
- $ dependencies = static ::dependencies ($ constructor ->getParameters ());
175
+ $ dependencies = static ::dependencies ($ constructor ->getParameters (), $ parameters );
176
176
177
177
return $ reflector ->newInstanceArgs ($ dependencies );
178
178
}
@@ -181,28 +181,54 @@ protected static function build($type, $parameters = array())
181
181
* Resolve all of the dependencies from the ReflectionParameters.
182
182
*
183
183
* @param array $parameters
184
+ * @param array $arguments that might have been passed into our resolve
184
185
* @return array
185
186
*/
186
- protected static function dependencies ($ parameters )
187
+ protected static function dependencies ($ parameters, $ arguments )
187
188
{
188
189
$ dependencies = array ();
189
190
190
191
foreach ($ parameters as $ parameter )
191
192
{
192
193
$ dependency = $ parameter ->getClass ();
193
194
194
- // If the class is null, it means the dependency is a string or some other
195
- // primitive type, which we can not resolve since it is not a class and
196
- // we'll just bomb out with an error since we have nowhere to go.
197
- if (is_null ( $ dependency ) )
195
+ // If the person passed in some parameters to the class
196
+ // then we should probably use those instead of trying
197
+ // to resolve a new instance of the class
198
+ if (count ( $ arguments ) > 0 )
198
199
{
199
- throw new \Exception ("Unresolvable dependency resolving [ $ parameter]. " );
200
+ $ dependencies [] = array_shift ($ arguments );
201
+ }
202
+ else if (is_null ($ dependency ))
203
+ {
204
+ $ dependency [] = static ::resolveNonClass ($ parameter );
205
+ }
206
+ else
207
+ {
208
+ $ dependencies [] = static ::resolve ($ dependency ->name );
200
209
}
201
-
202
- $ dependencies [] = static ::resolve ($ dependency ->name );
203
210
}
204
211
205
212
return (array ) $ dependencies ;
206
213
}
207
214
215
+ /**
216
+ * Resolves optional parameters for our dependency injection
217
+ * pretty much took backport straight from L4's Illuminate\Container
218
+ *
219
+ * @param ReflectionParameter
220
+ * @return default value
221
+ */
222
+ protected static function resolveNonClass ($ parameter )
223
+ {
224
+ if ($ parameter ->isDefaultValueAvailable ())
225
+ {
226
+ return $ parameter ->getDefaultValue ();
227
+ }
228
+ else
229
+ {
230
+ throw new \Exception ("Unresolvable dependency resolving [ $ parameter]. " );
231
+ }
232
+ }
233
+
208
234
}
0 commit comments