Skip to content

Commit 3eaf84b

Browse files
committed
Improve service caching
1 parent 00974c5 commit 3eaf84b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/ServiceProviderContainer.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,19 @@ public function get(string $id): mixed
8787

8888
try {
8989
$service = call_user_func($factory[$id], $this->wrapperContainer);
90-
$this->applyServiceExtensions($id, $service);
90+
$class = get_class($service);
91+
$this->applyServiceExtensions($id, $class, $service);
9192
} catch (ContainerExceptionInterface $containerException) {
9293
throw ContainerException::forInvalidService($id, $containerException);
9394
}
9495

95-
return $this->cache[$id] = $service;
96+
$this->cache[$id] = $service;
97+
98+
if ($id !== $class && !isset($this->cache[$class])) {
99+
$this->cache[$class] = $service;
100+
}
101+
102+
return $service;
96103
}
97104

98105
/**
@@ -118,9 +125,8 @@ public function get(string $id): mixed
118125
*
119126
* @throws ContainerException If any extension fails during invocation.
120127
*/
121-
private function applyServiceExtensions(string $id, mixed $service): void
128+
private function applyServiceExtensions(string $id, string $class, mixed $service): void
122129
{
123-
$class = get_class($service);
124130
$extensions = $this->serviceProvider->getExtensions();
125131

126132
if (\array_key_exists($id, $extensions) && \is_callable($extensions[$id])) {

0 commit comments

Comments
 (0)