laminas-servicemanager
laminas-servicemanager copied to clipboard
DI-like PHP Attributes (Autowiring feature)
Feature Request
Q | A |
---|---|
New Feature | yes |
RFC | no |
BC Break | no |
Summary
Hi! We have MVP solution for autowiring (auto-generate PHP configs for service-manager): https://github.com/opsway/laminas-service-manager-attributes
It is looks like:
#[DI\Factory(InvokableFactory::class)]
#[DI\AliasTo(Example6Service::class)]
class Example1Service implements Example6Service
{
}
or even
#[DI\AutoWireFactory]
class Example4Service
{
public function __construct(
#[DI\InjectService(Example2Service::class)] private $example3,
private Example7ServiceInterface $example7
) {
}
}
It is solution help us decrease a lot human mistakes and misconfiguration between code and configuration for dependencies.
So my question: Should we continue develop & maintain this package as separate solution? Or we can make separate package as part of laminas vendor packages? Or even make this solution as part of core service-manager package? What you think about it?)
I'm personally not a fan of having DI happen via annotations, as it wires implementation with container definition, defeating its original purpose.
Still, did you see bitexpert/disco? Should be close to your work...
@shandyDev Check also https://github.com/reinfi/zf-dependency-injection
I do not like this kind of magic as well, mostly due to the way that it is some magic hidden in the application rather than having a proper configuration. Another problem is, that there can be multiple services aliasing the same interface which is not properly addressable with these annotations. If there is a config, almost every IDE will already show you if an array key is declared twice in the same array.
But I am actually trying to provide an AOT factory which is based on ReflectionBasedAbstractFactory
: #180
This will still require upstream projects to have a minimum configuration (i.e. manual alias mapping and manual factory mapping - to ReflectionBasedAbstractFactory
).
However, since there are already 3rd-party libraries out there which does provide this feature, I'd say we are not gonna work more on this than necessary.