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

Commit 8c49590

Browse files
author
fhein
committed
Finalized mapAliasToTarget and mapAliasesToTargets.
1 parent 37e7268 commit 8c49590

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

src/ServiceManager.php

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,10 @@ public function configure(array $config)
377377
}
378378

379379
if (isset($config['aliases'])) {
380-
// @todo: Shouldn't that be $config['aliases'] + $this->aliases (or array_merge?)
381-
// Will have to examine.
382-
$aliases = $config['aliases'];
380+
$this->aliases = $config['aliases'] + $this->aliases;
381+
$this->mapAliasesToTargets();
383382
} elseif (! $this->configured && ! empty($this->aliases)) {
384-
$aliases = $this->aliases;
385-
}
386-
if (! empty($aliases)) {
387-
$this->mapAliasesToTargets($aliases);
383+
$this->mapAliasesToTargets();
388384
}
389385

390386
if (isset($config['shared_by_default'])) {
@@ -915,7 +911,7 @@ private function mapAliasToTarget($alias, $target)
915911

916912
// finally we have to check if existing incomplete alias definitions
917913
// exist which can get resolved by the new alias
918-
if (in_array($alias, $this->aliases, true)) {
914+
if (in_array($alias, $this->aliases)) {
919915
$r = array_intersect($this->aliases, [ $alias ]);
920916
// found some, resolve them
921917
foreach ($r as $name => $service) {
@@ -930,32 +926,32 @@ private function mapAliasToTarget($alias, $target)
930926
* This as an adaptation of Tarjan's strongly connected components
931927
* algorithm. We detect cycles as well reduce the graph so that
932928
* each alias key gets associated with the resolved service.
929+
* This function maps $this->aliases in place.
930+
*
931+
* This algorithm is fast for mass updates through configure().
932+
* It is not appropriate if just a single alias is added.
933933
*
934-
* @param string[][] array of alias definitions
935934
*/
936-
private function mapAliasesToTargets($aliases)
935+
private function mapAliasesToTargets()
937936
{
938937
$tagged = [];
939-
foreach ($aliases as $alias => $target) {
940-
if (! isset($tagged[$alias])) {
941-
$tCursor = $aliases[$alias];
942-
$aCursor = $alias;
943-
$stack = [];
944-
while (isset($aliases[$tCursor])) {
945-
$stack[] = $aCursor;
946-
$aCursor = $tCursor;
947-
if (isset($tagged[$aCursor])) {
948-
throw CyclicAliasException::fromCyclicAlias($alias, $aliases);
949-
}
950-
$tagged[$aCursor] = true;
951-
$tCursor = $aliases[$tCursor];
952-
}
953-
foreach ($stack as $alias) {
954-
$aliases[$alias] = $tCursor;
938+
foreach ($this->aliases as $alias => $target) {
939+
if (isset($tagged[$alias])) {
940+
continue;
941+
}
942+
$tCursor = $this->aliases[$alias];
943+
$aCursor = $alias;
944+
$stack = [];
945+
while (isset($this->aliases[$tCursor])) {
946+
$tagged[$aCursor] = true;
947+
$this->aliases[$aCursor] = $this->aliases[$tCursor];
948+
$aCursor = $tCursor;
949+
$tCursor = $this->aliases[$tCursor];
950+
if ($aCursor === $tCursor) {
951+
throw CyclicAliasException::fromCyclicAlias($alias, $this->aliases);
955952
}
956953
}
957954
}
958-
$this->aliases = $aliases;
959955
}
960956

961957
/**

0 commit comments

Comments
 (0)