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

Commit

Permalink
#112 #113 removed excessive inlining, named actual logic that resolve…
Browse files Browse the repository at this point in the history
…s new aliases via old aliases
  • Loading branch information
Ocramius committed Jun 1, 2016
1 parent d2aafcc commit 094fb53
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,7 @@ public function configure(array $config)
}

if (isset($config['aliases'])) {
if ($this->configured) {
$intersect = $this->mergeIntoArray($this->aliases, $config['aliases']);
if ($intersect) {
$this->resolveAliases($this->aliases);
} else {
$this->resolveAliases($config['aliases'], true);
}
} else {
$this->aliases = $config['aliases'] + $this->aliases;
$this->resolveAliases($this->aliases);
}
$this->configureAliases($config['aliases']);
} elseif (! $this->configured && ! empty($this->aliases)) {
$this->resolveAliases($this->aliases);
}
Expand Down Expand Up @@ -384,6 +374,31 @@ public function configure(array $config)
return $this;
}

/**
* @param string[] $aliases
*
* @return void
*/
private function configureAliases(array $aliases)
{
if (! $this->configured) {
$this->aliases = $aliases + $this->aliases;

$this->resolveAliases($this->aliases);

return;
}

if ($this->mergeIntoArray($this->aliases, $aliases)) {
$this->resolveAliases($this->aliases);

return;
}

$this->resolveAliases($aliases);
$this->resolveNewAliasesWithPreviouslyResolvedAliases($aliases);
}

/**
* Merge source into destination.
* This method has good performance:
Expand Down Expand Up @@ -609,10 +624,11 @@ private function resolveInitializers(array $initializers)
/**
* Resolve aliases to their canonical service names.
*
* @param array $aliases
* @param boolean $onlyNewAliases
* @param string[] $aliases
*
* @returns void
*/
private function resolveAliases(array $aliases, $onlyNewAliases = false)
private function resolveAliases(array $aliases)
{
foreach ($aliases as $alias => $service) {
$visited = [];
Expand All @@ -629,14 +645,21 @@ private function resolveAliases(array $aliases, $onlyNewAliases = false)

$this->resolvedAliases[$alias] = $name;
}
}

// Check and replace new aliases as targets in exists aliases.

if ($onlyNewAliases) {
foreach ($this->resolvedAliases as $name => $target) {
if (isset($aliases[$target])) {
$this->resolvedAliases[$name] = $this->resolvedAliases[$target];
}
/**
* Rewrites the map of aliases by resolving the given $aliases with the existing resolved ones.
* This is mostly done for performance reasons.
*
* @param string[] $aliases
*
* @return void
*/
private function resolveNewAliasesWithPreviouslyResolvedAliases(array $aliases)
{
foreach ($this->resolvedAliases as $name => $target) {
if (isset($aliases[$target])) {
$this->resolvedAliases[$name] = $this->resolvedAliases[$target];
}
}
}
Expand Down

0 comments on commit 094fb53

Please sign in to comment.