[BUG] Service Manager Not Shared Duplicate new Instance with multiple Abstract Factories

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[BUG] Service Manager Not Shared Duplicate new Instance with multiple Abstract Factories

richard
This post has NOT been accepted by the mailing list yet.
Example test:

class notSharedByDefault extends ServiceManager
{
    protected $shareByDefault = false;
}

public function testDuplicateNewInstanceMultipleAbstractFactories()
{
    $this->serviceManager = new notSharedByDefault();
    $this->serviceManager->addAbstractFactory('ZendTest\ServiceManager\TestAsset\BarAbstractFactory');
    $this->serviceManager->addAbstractFactory('ZendTest\ServiceManager\TestAsset\FooAbstractFactory');
    $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\Bar', $this->serviceManager->get('bar'));
    $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\Bar', $this->serviceManager->get('bar'));
}

The first instance of Bar is created. The second instance of Bar cannot be created. The FooAbstractFactory adds a $this->pendingAbstractFactoryRequests which then prevents canCreateFromAbstractFactory returning true where it should.

This appears to be due to lines 892/893 in Zend\ServiceManager\ServiceManager:
$this->pendingAbstractFactoryRequests[get_class($abstractFactory)] = $requestedName;
if ($abstractFactory->canCreateServiceWithName($this, $canonicalName, $requestedName)) {

This also appears to be fixable by reversing the order of lines 892/893 :
if ($abstractFactory->canCreateServiceWithName($this, $canonicalName, $requestedName)) {
    $this->pendingAbstractFactoryRequests[get_class($abstractFactory)] = $requestedName;