Skip to content

Commit e511cf1

Browse files
authored
Merge pull request #15 from dereuromark/master-fix
Fix reusability for looped loadService() call.
2 parents b554cf6 + 35cd87a commit e511cf1

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/Service/ServiceAwareTrait.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ trait ServiceAwareTrait
5050
*/
5151
public function loadService($service, array $constructorArgs = [], $assignProperty = true)
5252
{
53-
$serviceInstance = $this->getServiceLocator()->load($service, $constructorArgs);
54-
55-
if (!$assignProperty) {
56-
return $serviceInstance;
57-
}
58-
5953
list(, $name) = pluginSplit($service);
6054

6155
if (strpos($name, '/') !== false) {
6256
$name = substr($name, strrpos($name, '/') + 1);
6357
}
6458

65-
if (isset($this->{$name})) {
66-
trigger_error(__CLASS__ . '::$%s is already in use.', E_USER_WARNING);
59+
if ($assignProperty && isset($this->{$name})) {
60+
return $this->{$name};
61+
}
62+
63+
$serviceInstance = $this->getServiceLocator()->load($service, $constructorArgs);
64+
65+
if (!$assignProperty) {
66+
return $serviceInstance;
6767
}
6868

6969
$this->{$name} = $serviceInstance;

tests/TestCase/Service/ServiceLocatorTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public function testLocate()
3636
$this->assertInstanceOf(TestService::class, $service);
3737
}
3838

39+
/**
40+
* testLocate multiple
41+
*
42+
* @return void
43+
*/
44+
public function testLocateMultiple()
45+
{
46+
$locator = new ServiceLocator();
47+
$service = $locator->load('Test');
48+
$service = $locator->load('Test');
49+
50+
$this->assertInstanceOf(TestService::class, $service);
51+
}
52+
3953
/**
4054
* testLocate
4155
*

0 commit comments

Comments
 (0)