diff --git a/src/Services/InfoProviderSystem/ProviderRegistry.php b/src/Services/InfoProviderSystem/ProviderRegistry.php index 46f2484b..acfbdc1e 100644 --- a/src/Services/InfoProviderSystem/ProviderRegistry.php +++ b/src/Services/InfoProviderSystem/ProviderRegistry.php @@ -46,12 +46,28 @@ final class ProviderRegistry */ private array $providers_disabled = []; + /** + * @var bool Whether the registry has been initialized + */ + private bool $initialized = false; + /** * @param iterable $providers */ - public function __construct(iterable $providers) + public function __construct(private readonly iterable $providers) { - foreach ($providers as $provider) { + //We do not initialize the structures here, because we do not want to do unnecessary work + //We do this lazy on the first call to getProviders() + } + + /** + * Initializes the registry, we do this lazy to avoid unnecessary work, on construction, which is always called + * even if the registry is not used + * @return void + */ + private function initStructures(): void + { + foreach ($this->providers as $provider) { $key = $provider->getProviderKey(); if (isset($this->providers_by_name[$key])) { @@ -65,6 +81,8 @@ final class ProviderRegistry $this->providers_disabled[$key] = $provider; } } + + $this->initialized = true; } /** @@ -73,6 +91,10 @@ final class ProviderRegistry */ public function getProviders(): array { + if (!$this->initialized) { + $this->initStructures(); + } + return $this->providers_by_name; } @@ -84,6 +106,10 @@ final class ProviderRegistry */ public function getProviderByKey(string $key): InfoProviderInterface { + if (!$this->initialized) { + $this->initStructures(); + } + return $this->providers_by_name[$key] ?? throw new \InvalidArgumentException("Provider with key $key not found"); } @@ -93,6 +119,10 @@ final class ProviderRegistry */ public function getActiveProviders(): array { + if (!$this->initialized) { + $this->initStructures(); + } + return $this->providers_active; } @@ -102,6 +132,10 @@ final class ProviderRegistry */ public function getDisabledProviders(): array { + if (!$this->initialized) { + $this->initStructures(); + } + return $this->providers_disabled; } } \ No newline at end of file diff --git a/tests/Services/InfoProviderSystem/ProviderRegistryTest.php b/tests/Services/InfoProviderSystem/ProviderRegistryTest.php index f25d89e4..5e6bec28 100644 --- a/tests/Services/InfoProviderSystem/ProviderRegistryTest.php +++ b/tests/Services/InfoProviderSystem/ProviderRegistryTest.php @@ -104,5 +104,7 @@ class ProviderRegistryTest extends TestCase $this->getMockProvider('test2'), $this->getMockProvider('test1'), ]); + + $registry->getProviders(); } }