Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 4270b28

Browse files
malininkOcramius
authored andcommitted
improve actions with aliases
1 parent be8ae43 commit 4270b28

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

src/ServiceManager.php

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,6 @@ public function configure(array $config)
324324
? array_merge($config['aliases'], $aliases)
325325
: $aliases;
326326
}
327-
328327
$config['factories'] = (isset($config['factories']))
329328
? array_merge($config['factories'], $factories)
330329
: $factories;
@@ -343,10 +342,18 @@ public function configure(array $config)
343342
}
344343

345344
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)) {
350357
$this->resolveAliases($this->aliases);
351358
}
352359

@@ -376,6 +383,35 @@ public function configure(array $config)
376383
return $this;
377384
}
378385

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+
379415
/**
380416
* Add an alias.
381417
*
@@ -570,9 +606,12 @@ private function resolveInitializers(array $initializers)
570606
}
571607

572608
/**
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
574613
*/
575-
private function resolveAliases(array $aliases)
614+
private function resolveAliases(array $aliases, $onlyNewAliases = false)
576615
{
577616
foreach ($aliases as $alias => $service) {
578617
$visited = [];
@@ -589,6 +628,16 @@ private function resolveAliases(array $aliases)
589628

590629
$this->resolvedAliases[$alias] = $name;
591630
}
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+
}
592641
}
593642

594643
/**

0 commit comments

Comments
 (0)