@@ -324,7 +324,6 @@ public function configure(array $config)
324
324
? array_merge ($ config ['aliases ' ], $ aliases )
325
325
: $ aliases ;
326
326
}
327
-
328
327
$ config ['factories ' ] = (isset ($ config ['factories ' ]))
329
328
? array_merge ($ config ['factories ' ], $ factories )
330
329
: $ factories ;
@@ -343,10 +342,18 @@ public function configure(array $config)
343
342
}
344
343
345
344
if (isset ($ config ['aliases ' ])) {
346
- $ this ->aliases = $ config ['aliases ' ] + $ this ->aliases ;
347
- }
348
-
349
- if (! empty ($ this ->aliases )) {
345
+ if ($ this ->configured ) {
346
+ $ intersect = $ this ->mergeIntoArray ($ this ->aliases , $ config ['aliases ' ]);
347
+ if ($ intersect ) {
348
+ $ this ->resolveAliases ($ this ->aliases );
349
+ } else {
350
+ $ this ->resolveAliases ($ config ['aliases ' ], true );
351
+ }
352
+ } else {
353
+ $ this ->aliases = $ config ['aliases ' ] + $ this ->aliases ;
354
+ $ this ->resolveAliases ($ this ->aliases );
355
+ }
356
+ } elseif (! $ this ->configured && ! empty ($ this ->aliases )) {
350
357
$ this ->resolveAliases ($ this ->aliases );
351
358
}
352
359
@@ -376,6 +383,35 @@ public function configure(array $config)
376
383
return $ this ;
377
384
}
378
385
386
+ /**
387
+ * Merge source into destination.
388
+ * This method has good performance:
389
+ * - at empty destination
390
+ * - at adding few elements into array
391
+ * - at checking arrays keys intersection
392
+ *
393
+ * Provide only present intersect
394
+ *
395
+ * @param array $destination
396
+ * @param array $source
397
+ * @return boolean
398
+ */
399
+ private function mergeIntoArray (array &$ destination , array &$ source )
400
+ {
401
+ if (empty ($ destination )) {
402
+ $ destination = $ source ;
403
+ return false ;
404
+ }
405
+ $ intersect = false ;
406
+ foreach ($ source as $ name => $ target ) {
407
+ if (isset ($ destination [$ name ])) {
408
+ $ intersect = true ;
409
+ }
410
+ $ destination [$ name ] = $ target ;
411
+ }
412
+ return $ intersect ;
413
+ }
414
+
379
415
/**
380
416
* Add an alias.
381
417
*
@@ -570,9 +606,12 @@ private function resolveInitializers(array $initializers)
570
606
}
571
607
572
608
/**
573
- * Resolve all aliases to their canonical service names.
609
+ * Resolve aliases to their canonical service names.
610
+ *
611
+ * @param array $aliases
612
+ * @param boolean $onlyNewAliases
574
613
*/
575
- private function resolveAliases (array $ aliases )
614
+ private function resolveAliases (array $ aliases, $ onlyNewAliases = false )
576
615
{
577
616
foreach ($ aliases as $ alias => $ service ) {
578
617
$ visited = [];
@@ -589,6 +628,16 @@ private function resolveAliases(array $aliases)
589
628
590
629
$ this ->resolvedAliases [$ alias ] = $ name ;
591
630
}
631
+
632
+ // Check and replace new aliases as targets in exists aliases.
633
+
634
+ if ($ onlyNewAliases ) {
635
+ foreach ($ this ->resolvedAliases as $ name => $ target ) {
636
+ if (isset ($ aliases [$ target ])) {
637
+ $ this ->resolvedAliases [$ name ] = $ this ->resolvedAliases [$ target ];
638
+ }
639
+ }
640
+ }
592
641
}
593
642
594
643
/**
0 commit comments