-
-
Notifications
You must be signed in to change notification settings - Fork 324
Use wildcards in definitions #156
Description
Hi, we are currently integrating PHP-DI 4 in one of our project.
Because its a good practice to have interfaces for our business services we have to define a lot of PHP-DI interface => implementation mappings.
For exemple (this sample can also be configured using a PHP-DI configuration array) :
$container -> set('Application\Service\IAuthorAccountService', \DI\object('Application\Service\Impl\AuthorAccountService'));
$container -> set('Application\Service\IAuthorNbReadingService', \DI\object('Application\Service\Impl\AuthorNbReadingService'));
$container -> set('Application\Service\IAuthorPublicationService', \DI\object('Application\Service\Impl\AuthorPublicationService'));
$container -> set('Application\Service\ILastUpdatedService', \DI\object('Application\Service\Impl\LastUpdatedService'));
$container -> set('Application\Service\IMailService', \DI\object('Application\Service\Impl\MailService'));
$container -> set('Application\Service\IPublicationNbReadingService', \DI\object('Application\Service\Impl\PublicationNbReadingService'));
$container -> set('Application\Service\ISubjectNbReadingService', \DI\object('Application\Service\Impl\SubjectNbReadingService'));
This is correctly working but at the end we will have a lot of mappings even if we have only one instance of our business services in the DI container.
I think a really good improvement for future releases of PHP-DI would be to have an auto-scanning feature to scan classes with the @Injectable annotation. This is exactly what the Spring Framework does.
For example in Spring we can do the following to automatically scan classes using the @Component annotation and create an instance of each component in the DI container at starting :
<context:component-scan base-package="application.service.impl" />
For this reason interface mappgins is not necessary in Spring, each time a @Component annotation is found on a class Spring instanciate this class and injects the associated object inside its DI Container. Then when a @Resource annotation is found on a class attribute Spring look inside its DI container to find one object which implements the specified interface. If multiple objects implementing the interface are present inside the container Spring fails or forces you to define the name of the object to inject.
In PHP-DI it would be great to have something similar, for example :
$container = $containerBuilder -> buildDevContainer();
$container -> scanPackage('Application\Service\Impl');
This would prevent developers to define lots of interface => implementation mappings in PHP-DI.
Any thoughts about this feature ?
Thanks,
Baptiste