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

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function retrieveFromPeeringManagerFirst()
* @return ServiceManager
* @throws Exception\InvalidServiceNameException
*/
public function setInvokableClass($name, $invokableClass, $shared = true)
public function setInvokableClass($name, $invokableClass, $shared = null)
{
$cName = $this->canonicalizeName($name);

Expand All @@ -237,6 +237,10 @@ public function setInvokableClass($name, $invokableClass, $shared = true)
$this->unregisterService($cName);
}

if ($shared === null) {
$shared = $this->shareByDefault();
}

$this->invokableClasses[$cName] = $invokableClass;
$this->shared[$cName] = (bool) $shared;

Expand All @@ -253,7 +257,7 @@ public function setInvokableClass($name, $invokableClass, $shared = true)
* @throws Exception\InvalidArgumentException
* @throws Exception\InvalidServiceNameException
*/
public function setFactory($name, $factory, $shared = true)
public function setFactory($name, $factory, $shared = null)
{
$cName = $this->canonicalizeName($name);

Expand All @@ -273,6 +277,10 @@ public function setFactory($name, $factory, $shared = true)
$this->unregisterService($cName);
}

if ($shared === null) {
$shared = $this->shareByDefault();
}

$this->factories[$cName] = $factory;
$this->shared[$cName] = (bool) $shared;

Expand Down Expand Up @@ -352,7 +360,7 @@ public function addInitializer($initializer, $topOfStack = true)
* @return ServiceManager
* @throws Exception\InvalidServiceNameException
*/
public function setService($name, $service, $shared = true)
public function setService($name, $service, $shared = null)
{
$cName = $this->canonicalizeName($name);

Expand All @@ -367,6 +375,10 @@ public function setService($name, $service, $shared = true)
$this->unregisterService($cName);
}

if ($shared === null) {
$shared = $this->shareByDefault();
}

$this->instances[$cName] = $service;
$this->shared[$cName] = (bool) $shared;
return $this;
Expand Down Expand Up @@ -458,7 +470,10 @@ public function get($name, $usePeeringServiceManagers = true)
));
}

if ($this->shareByDefault() && (!isset($this->shared[$cName]) || $this->shared[$cName] === true)) {
if (
($this->shareByDefault() && !isset($this->shared[$cName]))
|| (isset($this->shared[$cName]) && $this->shared[$cName] === true)
) {
$this->instances[$cName] = $instance;
}

Expand Down
17 changes: 17 additions & 0 deletions test/ServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ public function testGetThrowsExceptionOnUnknownService()
$this->assertEquals('bar', $this->serviceManager->get('foo'));
}

/**
* @covers Zend\ServiceManager\ServiceManager::get
*/
public function testGetUsesIndivualSharedSettingWhenSetAndDeviatesFromShareByDefaultSetting()
{
$this->serviceManager->setAllowOverride(true);
$this->serviceManager->setShareByDefault(false);
$this->serviceManager->setInvokableClass('foo', 'ZendTest\ServiceManager\TestAsset\Foo');
$this->serviceManager->setShared('foo', true);
$this->assertSame($this->serviceManager->get('foo'), $this->serviceManager->get('foo'));

$this->serviceManager->setShareByDefault(true);
$this->serviceManager->setInvokableClass('foo', 'ZendTest\ServiceManager\TestAsset\Foo');
$this->serviceManager->setShared('foo', false);
$this->assertNotSame($this->serviceManager->get('foo'), $this->serviceManager->get('foo'));
}

/**
* @covers Zend\ServiceManager\ServiceManager::get
*/
Expand Down

0 comments on commit b3220c5

Please sign in to comment.