beberlei / yadif

Yet Another (PHP) Dependency Injection Framework
Other
37 stars 1 forks source link

Investigate how to support Adapter pattern #9

Open beberlei opened 14 years ago

beberlei commented 14 years ago

There are often use cases where you want to have several different instances for the same interface and decide which collaborator is used from the interface based on the target object.

One idea:

Add "names" or second level descriptions to a component name and have one being registered as default:

$yadif->addComponent('Foo', array('class' => 'FooImpl'));
$yadif->addComponent('Foo', array('class' => 'BarImpl', 'annotateWith' => 'Super'));

Then when calling getComponent you can specifiy an order of annotations and the first one found matches falling back to the default one:

$foo = $yadif->getComponent('Foo', array('Super'));