Skip to content

Commit

Permalink
Make services as lazy (#66)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
WedgeSama authored Jan 15, 2025
1 parent 2fcd7d0 commit 61c2a4c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
8 changes: 1 addition & 7 deletions src/DependencyInjection/Factory/ProjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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']);
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/FirebaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/Resources/config/firebase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
<services>
<service id="Kreait\Firebase\Factory" class="%kreait.firebase.factory%" />

<service id="Kreait\Firebase\Symfony\Bundle\DependencyInjection\Factory\ProjectFactory" class="%kreait.firebase.bundle.project_factory%" public="false">
<argument type="service" id="Kreait\Firebase\Factory" />
</service>
<service id="Kreait\Firebase\Symfony\Bundle\DependencyInjection\Factory\ProjectFactory" class="%kreait.firebase.bundle.project_factory%" public="false" />
</services>

</container>
8 changes: 6 additions & 2 deletions tests/DependencyInjection/FirebaseExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand Down

0 comments on commit 61c2a4c

Please sign in to comment.