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

Commit 1f1f7c9

Browse files
committed
Merge remote-tracking branch 'zend-servicemanager/develop' into feature/bench
2 parents e17be58 + c6a6575 commit 1f1f7c9

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/ServiceManager.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function __construct(array $config = [])
147147
public function withConfig(array $config)
148148
{
149149
$container = clone $this;
150-
$container->creationContext = $container;
150+
$container->creationContext = ($this === $this->creationContext) ? $container : $this->creationContext;
151151
$container->configure($config);
152152
return $container;
153153
}
@@ -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/AbstractPluginManagerTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,52 @@ function ($container, $name, $callback) {
164164
);
165165
$this->assertEquals('bar', $instance->foo);
166166
}
167+
168+
public function testWithConfigProperlySetsCreationContextToOriginalCreationContext()
169+
{
170+
$config = [
171+
'factories' => [
172+
InvokableObject::class => new InvokableFactory(),
173+
],
174+
];
175+
176+
$services = new ServiceManager();
177+
$plugins = new SimplePluginManager($services);
178+
179+
$revised = $plugins->withConfig(['factories' => [
180+
'foo' => function ($container, $name) {
181+
},
182+
]]);
183+
184+
$this->assertAttributeSame($services, 'creationContext', $revised);
185+
}
186+
187+
/**
188+
* Overrides test from CommonServiceLocatorBehaviorsTrait
189+
*
190+
* Behavior of creation context differs for plugin managers.
191+
*/
192+
public function testCanCreateNewLocatorWithMergedConfig()
193+
{
194+
$serviceManager = $this->createContainer([
195+
'factories' => [
196+
DateTime::class => InvokableFactory::class
197+
]
198+
]);
199+
200+
$newServiceManager = $serviceManager->withConfig([
201+
'factories' => [
202+
stdClass::class => InvokableFactory::class
203+
]
204+
]);
205+
206+
$this->assertTrue($serviceManager->has(DateTime::class));
207+
$this->assertFalse($serviceManager->has(stdClass::class));
208+
209+
$this->assertTrue($newServiceManager->has(DateTime::class));
210+
$this->assertTrue($newServiceManager->has(stdClass::class));
211+
212+
// Make sure the context has been updated for the new container
213+
$this->assertAttributeSame($this->creationContext, 'creationContext', $newServiceManager);
214+
}
167215
}

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)