From 61c2a4cd874e0513cac18aea0aca59b68173cb16 Mon Sep 17 00:00:00 2001 From: Benjamin Georgeault Date: Thu, 16 Jan 2025 00:24:25 +0100 Subject: [PATCH] Make services as lazy (#66) Too many compute done when services are instantiated (e.g. injected). Better to compute only if the service is really used. Plus some optim: - `ProjectFactory` service do not need `Factory` factory injection (no argument in its constructor). - `ProjectFactory::createFactory` if we really want a new instance, use `new` instead of `clone`. --- src/DependencyInjection/Factory/ProjectFactory.php | 8 +------- src/DependencyInjection/FirebaseExtension.php | 1 + src/Resources/config/firebase.xml | 4 +--- tests/DependencyInjection/FirebaseExtensionTest.php | 8 ++++++-- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/DependencyInjection/Factory/ProjectFactory.php b/src/DependencyInjection/Factory/ProjectFactory.php index 41616fd..b665eb7 100644 --- a/src/DependencyInjection/Factory/ProjectFactory.php +++ b/src/DependencyInjection/Factory/ProjectFactory.php @@ -13,17 +13,11 @@ class ProjectFactory { - private Factory $firebaseFactory; private ?CacheItemPoolInterface $verifierCache = null; private ?CacheItemPoolInterface $authTokenCache = null; private ?LoggerInterface $httpRequestLogger = null; private ?LoggerInterface $httpRequestDebugLogger = null; - public function __construct() - { - $this->firebaseFactory = new Factory(); - } - /** * @param CacheInterface|CacheItemPoolInterface $verifierCache */ @@ -65,7 +59,7 @@ public function createAuth(array $config = []): Firebase\Contract\Auth public function createFactory(array $config = []): Factory { - $factory = clone $this->firebaseFactory; // Ensure a new instance + $factory = new Factory(); if ($config['credentials'] ?? null) { $factory = $factory->withServiceAccount($config['credentials']); diff --git a/src/DependencyInjection/FirebaseExtension.php b/src/DependencyInjection/FirebaseExtension.php index ed47d78..ce51449 100644 --- a/src/DependencyInjection/FirebaseExtension.php +++ b/src/DependencyInjection/FirebaseExtension.php @@ -85,6 +85,7 @@ private function registerService(string $name, string $postfix, array $config, s $container->register($projectServiceId, $contract) ->setFactory([$factory, $method]) + ->setLazy(true) ->addArgument($config) ->setPublic($isPublic); diff --git a/src/Resources/config/firebase.xml b/src/Resources/config/firebase.xml index 15d4487..97d6897 100644 --- a/src/Resources/config/firebase.xml +++ b/src/Resources/config/firebase.xml @@ -11,9 +11,7 @@ - - - + diff --git a/tests/DependencyInjection/FirebaseExtensionTest.php b/tests/DependencyInjection/FirebaseExtensionTest.php index a6d4549..9d3944b 100644 --- a/tests/DependencyInjection/FirebaseExtensionTest.php +++ b/tests/DependencyInjection/FirebaseExtensionTest.php @@ -179,7 +179,9 @@ public function an_invalid_verifier_cache_can_not_be_used(): void $container->set($cacheServiceId, $invalidCache); $this->expectException(TypeError::class); - $container->get(Firebase\Contract\Auth::class); + /** @var Firebase\Contract\Auth $service */ + $service = $container->get(Firebase\Contract\Auth::class); + $service->createAnonymousUser(); } /** @@ -199,7 +201,9 @@ public function a_non_existing_verifier_cache_can_not_be_used(): void ]); $this->expectException(ServiceNotFoundException::class); - $container->get(Firebase\Contract\Auth::class); + /** @var Firebase\Contract\Auth $service */ + $service = $container->get(Firebase\Contract\Auth::class); + $service->createAnonymousUser(); } /**