Skip to content

Commit 68815ac

Browse files
committed
MemoizingContainer - memoize getService
1 parent a0371fc commit 68815ac

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

src/DependencyInjection/MemoizingContainer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ final class MemoizingContainer implements Container
1111
/** @var array<string, mixed> */
1212
private array $servicesByType = [];
1313

14+
/** @var array<string, mixed> */
15+
private array $servicesByName = [];
16+
1417
public function __construct(
1518
#[AutowiredParameter(ref: '@PHPStan\DependencyInjection\Nette\NetteContainer')]
1619
private Container $originalContainer,
@@ -25,7 +28,14 @@ public function hasService(string $serviceName): bool
2528

2629
public function getService(string $serviceName)
2730
{
28-
return $this->originalContainer->getService($serviceName);
31+
if (array_key_exists($serviceName, $this->servicesByName)) {
32+
return $this->servicesByName[$serviceName];
33+
}
34+
35+
$service = $this->originalContainer->getService($serviceName);
36+
$this->servicesByName[$serviceName] = $service;
37+
38+
return $service;
2939
}
3040

3141
public function getByType(string $className)

0 commit comments

Comments
 (0)