Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 117e876

Browse files
committed
Merge pull request #33 from weierophinney/hotfix/shared-aliasing
Cache by requested service name
2 parents 3f4e6a6 + f18beb6 commit 117e876

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

src/ServiceManager.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,34 @@ public function withConfig(array $config)
157157
*/
158158
public function get($name)
159159
{
160+
$requestedName = $name;
160161
$name = isset($this->resolvedAliases[$name]) ? $this->resolvedAliases[$name] : $name;
161162

162-
// We start by checking if the service is cached (this is the fastest method).
163-
if (isset($this->services[$name])) {
164-
return $this->services[$name];
163+
// We start by checking if we have cached the requested service (this
164+
// is the fastest method).
165+
if (isset($this->services[$requestedName])) {
166+
return $this->services[$requestedName];
165167
}
166168

169+
// At this point, we need to create the instance; we use the resolved
170+
// name for that.
167171
$object = $this->doCreate($name);
168172

169-
if (($this->sharedByDefault && !isset($this->shared[$name]))
170-
|| (isset($this->shared[$name]) && $this->shared[$name])) {
173+
// Cache it for later, if it is supposed to be shared.
174+
if (($this->sharedByDefault && ! isset($this->shared[$name]))
175+
|| (isset($this->shared[$name]) && $this->shared[$name])
176+
) {
171177
$this->services[$name] = $object;
172178
}
173179

180+
// Also do so for aliases; this allows sharing based on service name used.
181+
if ($requestedName !== $name
182+
&& (($this->sharedByDefault && ! isset($this->shared[$requestedName]))
183+
|| (isset($this->shared[$requestedName]) && $this->shared[$requestedName]))
184+
) {
185+
$this->services[$requestedName] = $object;
186+
}
187+
174188
return $object;
175189
}
176190

test/ServiceManagerTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,46 @@ public function testMapsNonSymmetricInvokablesAsAliasPlusInvokableFactory()
179179
InvokableObject::class => InvokableFactory::class,
180180
], 'factories', $serviceManager, 'Factory not found for non-symmetric invokable target');
181181
}
182+
183+
/**
184+
* @depends testMapsNonSymmetricInvokablesAsAliasPlusInvokableFactory
185+
*/
186+
public function testSharedServicesReferencingInvokableAliasShouldBeHonored()
187+
{
188+
$config = [
189+
'invokables' => [
190+
'Invokable' => InvokableObject::class,
191+
],
192+
'shared' => [
193+
'Invokable' => false,
194+
],
195+
];
196+
197+
$serviceManager = new ServiceManager($config);
198+
$instance1 = $serviceManager->get('Invokable');
199+
$instance2 = $serviceManager->get('Invokable');
200+
201+
$this->assertNotSame($instance1, $instance2);
202+
}
203+
204+
public function testSharedServicesReferencingAliasShouldBeHonored()
205+
{
206+
$config = [
207+
'aliases' => [
208+
'Invokable' => InvokableObject::class,
209+
],
210+
'factories' => [
211+
InvokableObject::class => InvokableFactory::class,
212+
],
213+
'shared' => [
214+
'Invokable' => false,
215+
],
216+
];
217+
218+
$serviceManager = new ServiceManager($config);
219+
$instance1 = $serviceManager->get('Invokable');
220+
$instance2 = $serviceManager->get('Invokable');
221+
222+
$this->assertNotSame($instance1, $instance2);
223+
}
182224
}

0 commit comments

Comments
 (0)