From 3edf5f1528b767229e594687d93d98185fb1871e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 13 Feb 2018 09:11:34 +0100 Subject: [PATCH] [DI] Top micro benchmarks --- .../DependencyInjection/Container.php | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index c5ada9ee20bb..2afd4518496e 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -217,21 +217,18 @@ public function has($id) */ public function get($id, $invalidBehavior = /* self::EXCEPTION_ON_INVALID_REFERENCE */ 1) { - if (isset($this->aliases[$id])) { - $id = $this->aliases[$id]; - } - - // Re-use shared service instance if it exists. - if (isset($this->services[$id])) { - return $this->services[$id]; - } - if ('service_container' === $id) { - return $this; - } - if (isset($this->factories[$id])) { - return $this->factories[$id](); - } + return $this->services[$id] + ?? $this->services[$id = $this->aliases[$id] ?? $id] + ?? ('service_container' === $id ? $this : ($this->factories[$id] ?? array($this, 'make'))($id, $invalidBehavior)); + } + /** + * Creates a service. + * + * As a separate method to allow "get()" to use the really fast `??` operator. + */ + private function make(string $id, int $invalidBehavior) + { if (isset($this->loading[$id])) { throw new ServiceCircularReferenceException($id, array_keys($this->loading)); }