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

Commit c6a6575

Browse files
committed
Merge pull request #37 from weierophinney/hotfix/aliased-service
Ensure aliases can reference explicit services
2 parents 5ab23c5 + 8e1a9cc commit c6a6575

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/ServiceManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ public function get($name)
166166
return $this->services[$requestedName];
167167
}
168168

169+
// Next, if the alias should be shared, and we have cached the resolved
170+
// service, use it.
171+
if ($requestedName !== $name
172+
&& (! isset($this->shared[$requestedName]) || $this->shared[$requestedName])
173+
&& isset($this->services[$name])
174+
) {
175+
$this->services[$requestedName] = $this->services[$name];
176+
return $this->services[$name];
177+
}
178+
169179
// At this point, we need to create the instance; we use the resolved
170180
// name for that.
171181
$object = $this->doCreate($name);

test/ServiceManagerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,23 @@ public function testSharedServicesReferencingAliasShouldBeHonored()
221221

222222
$this->assertNotSame($instance1, $instance2);
223223
}
224+
225+
public function testAliasToAnExplicitServiceShouldWork()
226+
{
227+
$config = [
228+
'aliases' => [
229+
'Invokable' => InvokableObject::class,
230+
],
231+
'services' => [
232+
InvokableObject::class => new InvokableObject(),
233+
],
234+
];
235+
236+
$serviceManager = new ServiceManager($config);
237+
238+
$service = $serviceManager->get(InvokableObject::class);
239+
$alias = $serviceManager->get('Invokable');
240+
241+
$this->assertSame($service, $alias);
242+
}
224243
}

0 commit comments

Comments
 (0)