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

Commit 1173515

Browse files
committed
Pre-resolve all aliases
Resolve aliases during instantiation, and remove need for function call in each of `get()`, `has()`, and `build()` as a result. No measureable impact on test execution time and resource usage.
1 parent 8be70ef commit 1173515

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/ServiceManager.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class ServiceManager implements ServiceLocatorInterface
8585
*/
8686
private $lazyServicesDelegator;
8787

88+
/**
89+
* @var string[]
90+
*/
91+
private $resolvedAliases = [];
92+
8893
/**
8994
* A list of already loaded services (this act as a local cache)
9095
*
@@ -152,7 +157,7 @@ public function withConfig(array $config)
152157
*/
153158
public function get($name)
154159
{
155-
$name = $this->resolveAlias($name);
160+
$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
156161

157162
// We start by checking if the service is cached (this is the fastest method).
158163
if (isset($this->services[$name])) {
@@ -175,15 +180,16 @@ public function get($name)
175180
public function build($name, array $options = null)
176181
{
177182
// We never cache when using "build"
178-
return $this->doCreate($this->resolveAlias($name), $options);
183+
$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
184+
return $this->doCreate($name, $options);
179185
}
180186

181187
/**
182188
* {@inheritDoc}
183189
*/
184190
public function has($name, $checkAbstractFactories = false)
185191
{
186-
$name = $this->resolveAlias($name);
192+
$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
187193
$found = isset($this->services[$name]) || isset($this->factories[$name]);
188194

189195
if ($found || !$checkAbstractFactories) {
@@ -296,6 +302,8 @@ protected function configure(array $config)
296302
));
297303
}
298304
}
305+
306+
$this->resolveAliases();
299307
}
300308

301309
/**
@@ -316,6 +324,16 @@ private function resolveAlias($alias)
316324
return $name;
317325
}
318326

327+
/**
328+
* Resolve all aliases to their canonical service names.
329+
*/
330+
private function resolveAliases()
331+
{
332+
foreach ($this->aliases as $alias => $service) {
333+
$this->resolvedAliases[$alias] = $this->resolveAlias($alias);
334+
}
335+
}
336+
319337
/**
320338
* Get a factory for the given service name
321339
*

0 commit comments

Comments
 (0)