@@ -275,74 +275,106 @@ protected function configure(array $config)
275
275
// For abstract factories and initializers, we always directly
276
276
// instantiate them to avoid checks during service construction.
277
277
if (isset ($ config ['abstract_factories ' ])) {
278
- foreach ($ config ['abstract_factories ' ] as $ abstractFactory ) {
279
- if (is_string ($ abstractFactory ) && class_exists ($ abstractFactory )) {
280
- $ abstractFactory = new $ abstractFactory ();
281
- }
278
+ $ this ->resolveAbstractFactories ($ config ['abstract_factories ' ]);
279
+ }
282
280
283
- if ($ abstractFactory instanceof AbstractFactoryInterface) {
284
- $ this ->abstractFactories [] = $ abstractFactory ;
285
- continue ;
286
- }
281
+ if (isset ($ config ['initializers ' ])) {
282
+ $ this ->resolveInitializers ($ config ['initializers ' ]);
283
+ }
287
284
288
- // Error condition; let's find out why.
285
+ $ this ->resolveAliases ();
286
+ }
289
287
290
- // If we still have a string, we have a class name that does not resolve
291
- if (is_string ($ abstractFactory )) {
292
- throw new InvalidArgumentException (sprintf (
293
- 'An invalid abstract factory was registered; resolved to class "%s" '
294
- . 'which does not exist; please provide a valid class name resolving '
295
- . 'to an implementation of %s ' ,
288
+ /**
289
+ * Instantiate abstract factories for to avoid checks during service construction.
290
+ *
291
+ * @param string[]|AbstractFactoryInterface[] $abstractFactories
292
+ *
293
+ * @return void
294
+ */
295
+ private function resolveAbstractFactories (array $ abstractFactories )
296
+ {
297
+ foreach ($ abstractFactories as $ abstractFactory ) {
298
+ if (is_string ($ abstractFactory ) && class_exists ($ abstractFactory )) {
299
+ $ abstractFactory = new $ abstractFactory ();
300
+ }
301
+
302
+ if ($ abstractFactory instanceof AbstractFactoryInterface) {
303
+ $ this ->abstractFactories [] = $ abstractFactory ;
304
+ continue ;
305
+ }
306
+
307
+ // Error condition; let's find out why.
308
+
309
+ // If we still have a string, we have a class name that does not resolve
310
+ if (is_string ($ abstractFactory )) {
311
+ throw new InvalidArgumentException (
312
+ sprintf (
313
+ 'An invalid abstract factory was registered; resolved to class "%s" ' .
314
+ 'which does not exist; please provide a valid class name resolving ' .
315
+ 'to an implementation of %s ' ,
296
316
$ abstractFactory ,
297
317
AbstractFactoryInterface::class
298
- ));
299
- }
318
+ )
319
+ );
320
+ }
300
321
301
- // Otherwise, we have an invalid type.
302
- throw new InvalidArgumentException (sprintf (
303
- 'An invalid abstract factory was registered. Expected an instance of "%s", '
304
- . 'but "%s" was received ' ,
322
+ // Otherwise, we have an invalid type.
323
+ throw new InvalidArgumentException (
324
+ sprintf (
325
+ 'An invalid abstract factory was registered. Expected an instance of "%s", ' .
326
+ 'but "%s" was received ' ,
305
327
AbstractFactoryInterface::class,
306
328
(is_object ($ abstractFactory ) ? get_class ($ abstractFactory ) : gettype ($ abstractFactory ))
307
- ));
308
- }
329
+ )
330
+ );
309
331
}
332
+ }
310
333
311
- if (isset ($ config ['initializers ' ])) {
312
- foreach ($ config ['initializers ' ] as $ initializer ) {
313
- if (is_string ($ initializer ) && class_exists ($ initializer )) {
314
- $ initializer = new $ initializer ();
315
- }
334
+ /**
335
+ * Instantiate initializers for to avoid checks during service construction.
336
+ *
337
+ * @param string[]|callable[]|InitializerInterface[] $initializers
338
+ *
339
+ * @return void
340
+ */
341
+ private function resolveInitializers (array $ initializers )
342
+ {
343
+ foreach ($ initializers as $ initializer ) {
344
+ if (is_string ($ initializer ) && class_exists ($ initializer )) {
345
+ $ initializer = new $ initializer ();
346
+ }
316
347
317
- if (is_callable ($ initializer )) {
318
- $ this ->initializers [] = $ initializer ;
319
- continue ;
320
- }
348
+ if (is_callable ($ initializer )) {
349
+ $ this ->initializers [] = $ initializer ;
350
+ continue ;
351
+ }
321
352
322
- // Error condition; let's find out why.
353
+ // Error condition; let's find out why.
323
354
324
- if (is_string ($ initializer )) {
325
- throw new InvalidArgumentException (sprintf (
326
- 'An invalid initializer was registered; resolved to class or function "%s" '
327
- . 'which does not exist; please provide a valid function name or class '
328
- . 'name resolving to an implementation of %s ' ,
355
+ if (is_string ($ initializer )) {
356
+ throw new InvalidArgumentException (
357
+ sprintf (
358
+ 'An invalid initializer was registered; resolved to class or function "%s" ' .
359
+ 'which does not exist; please provide a valid function name or class ' .
360
+ 'name resolving to an implementation of %s ' ,
329
361
$ initializer ,
330
362
InitializerInterface::class
331
- ));
332
- }
363
+ )
364
+ );
365
+ }
333
366
334
- // Otherwise, we have an invalid type.
335
- throw new InvalidArgumentException (sprintf (
336
- 'An invalid initializer was registered. Expected a callable, or an instance of '
337
- . '(or string class name resolving to) "%s", '
338
- . 'but "%s" was received ' ,
367
+ // Otherwise, we have an invalid type.
368
+ throw new InvalidArgumentException (
369
+ sprintf (
370
+ 'An invalid initializer was registered. Expected a callable, or an instance of ' .
371
+ '(or string class name resolving to) "%s", ' .
372
+ 'but "%s" was received ' ,
339
373
InitializerInterface::class,
340
374
(is_object ($ initializer ) ? get_class ($ initializer ) : gettype ($ initializer ))
341
- ));
342
- }
375
+ )
376
+ );
343
377
}
344
-
345
- $ this ->resolveAliases ();
346
378
}
347
379
348
380
/**
0 commit comments