Skip to content

Commit 7cc4097

Browse files
[DI] Top micro benchmarks
1 parent e615852 commit 7cc4097

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

Container.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,18 @@ public function has($id)
217217
*/
218218
public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERENCE */ 1)
219219
{
220-
if (isset($this->aliases[$id])) {
221-
$id = $this->aliases[$id];
222-
}
223-
224-
// Re-use shared service instance if it exists.
225-
if (isset($this->services[$id])) {
226-
return $this->services[$id];
227-
}
228-
if ('service_container' === $id) {
229-
return $this;
230-
}
231-
if (isset($this->factories[$id])) {
232-
return $this->factories[$id]();
233-
}
220+
return $this->services[$id]
221+
?? $this->services[$id = $this->aliases[$id] ?? $id]
222+
?? ('service_container' === $id ? $this : ($this->factories[$id] ?? array($this, 'make'))($id, $invalidBehavior));
223+
}
234224

225+
/**
226+
* Creates a service.
227+
*
228+
* As a separate method to allow "get()" to use the really fast `??` operator.
229+
*/
230+
private function make(string $id, int $invalidBehavior)
231+
{
235232
if (isset($this->loading[$id])) {
236233
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
237234
}

0 commit comments

Comments
 (0)