Permalink
Browse files

Hotfix: Allow items from Abstract Factories to have setShared() called

Some items that are created from Abstract Factories, we may want to
re-create each time - rather than having a shared object.  This allows
checks whether the requested object is able to be created via an
Abstract Factory.  The rest of the code is already in place to allow
this not to be added to $sm->instances;
  • Loading branch information...
1 parent a0681e2 commit 461b0ea71c7412d927c40640f8c0be3da6448c95 Martin Meredith committed Oct 5, 2012
@@ -385,7 +385,11 @@ public function setShared($name, $isShared)
{
$cName = $this->canonicalizeName($name);
- if (!isset($this->invokableClasses[$cName]) && !isset($this->factories[$cName])) {
+ if (
+ !isset($this->invokableClasses[$cName])
+ && !isset($this->factories[$cName])
+ && !$this->canCreateFromAbstractFactory($cName, $name)
+ ) {
throw new Exception\ServiceNotFoundException(sprintf(
'%s: A service by the name "%s" was not found and could not be marked as shared',
__METHOD__,
@@ -160,6 +160,16 @@ public function testSetShared()
$ret = $this->serviceManager->setShared('foo', true);
$this->assertSame($this->serviceManager, $ret);
}
+
+ /**
+ * @covers Zend\ServiceManager\ServiceManager::setShared
+ */
+ public function testSetSharedAbstractFactory()
+ {
+ $this->serviceManager->addAbstractFactory('ZendTest\ServiceManager\TestAsset\FooAbstractFactory');
+ $ret = $this->serviceManager->setShared('foo', false);
+ $this->assertSame($this->serviceManager, $ret);
+ }
/**
* @covers Zend\ServiceManager\ServiceManager::setShared

5 comments on commit 461b0ea

Contributor

gauthier replied Jan 15, 2013

thanks for the addition, I was about to propose it because I'm stuck on 2.0.2 version :) It was worth checking if somebody did it before...

Anyway, I'm wondering the benefit of checking whether the service exist or not when setting its shared status...

if a lot of AbstractFactories are registered, it could lead to some non-neglectable overhead (depending on canCreateServiceWithName() complexity).

What about a simplified setShared() like this :

public function setShared($name, $isShared)
{
$this->shared[$this->canonicalizeName($name)] = (bool) $isShared;
return $this;
}

I'm pretty not sure that being informed that a service is not defined when setting its shared status is that useful for the developer... they'll be informed when trying to get the service, and it seems good enough to me.

Gauthier

Contributor

Mezzle replied Jan 15, 2013

This has already been merged?

Member

Maks3w replied Jan 15, 2013

This commit is already merged

Member

Maks3w replied Jan 15, 2013

@gauthier I suggest you open a discussion on the Mailing List because this thread is out of the normal places of discussion. http://framework.zend.com/archives/subscribe/

Contributor

gauthier replied Jan 15, 2013

Please sign in to comment.