-
Notifications
You must be signed in to change notification settings - Fork 89
Repeated AbstractFactory may occur in ServiceManager. #106
Conversation
Needs a test case before being applied. |
Added a unit test. |
ping @Ocramius |
I just realized that this change is incorrect. Following code is perfectly legit, for example: $abstractFactory1 = new MyConfiguredAbstractFactory($config1);
$abstractFactory2 = new MyConfiguredAbstractFactory($config2);
$sm = new ServiceManager([
'abstract_factories' => [
$abstractFactory1,
$abstractFactory2,
],
]); This scenario seems legit to me, and is actually even used within stuff like DoctrineModule, now that I look closer. I think the key used to store the factory internally should be based on A test for this scenario is also needed, IMO... |
$serviceManager->addAbstractFactory(CallOnlyOnceAbstractFactory::class); | ||
$serviceManager->has(stdClass::class); | ||
|
||
$this->assertEquals(1, CallOnlyOnceAbstractFactory::getCallTimes()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be 2
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A new test with the same instance of CallOnlyOnceAbstractFactory
should be provided. In that scenario, only one factory should remain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And even there, strong reasoning should be given as to why a duplicate abstract factory is actually a problem...
Ocramius is right. ServiceManager:
Unit test:
|
Ok, now that the patch is correct (and sorry if it feels like I'm dragging you around, I really get to check this stuff when it pops up in my inbox): what kind of problem does #104 cause? Is it just a performance issue? Assuming idempotent factories (big assumptions, so I might be wrong here), I don't see a problem with having two factories referencing the same instance inside the |
I thought for a while. May be I am too sensitive to performance. At that time I wrote two ZF modules (Module A & Module B). They all have the same configuration. 'service_manager' => array(
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstractServiceFactory',
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'...',
),
), I just want to get another service, but call every Now this is another solution. class Module
{
public function init(ModuleManager $manager)
{
$manager->getEventManager()->attach(ModuleEvent::EVENT_MERGE_CONFIG, function (ModuleEvent $event) {
/** @var \Zend\ModuleManager\Listener\ConfigListener $configListener */
$configListener =$event->getParam('configListener');
$configs = $configListener->getMergedConfig(false);
$configs['service_manager']['abstract_factories'] =
array_unique($configs['service_manager']['abstract_factories']);
$configListener->setMergedConfig($configs);
});
}
} 😄 😄 |
It can be problem when |
Or may be it is |
Assigning to 3.2.0 release, as the feature is technically new behavior. |
Repeated AbstractFactory may occur in ServiceManager. Conflicts: src/ServiceManager.php
Merged to develop for release with 3.2.0 |
See #104