You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's very common to have multiple different logs within a single application, especially if it's a larger application. Therefore it would be nice if one could override an interface-definition just for a specific class.
Example: let's say we have two services: ServiceA and ServiceB. Both services demand a LoggerInterface via constructor but the actual logfiles have to be different (let's say "a.log" and "b.log"). Now it would be cool if I could express this requirement with a definition that looks something like that:
[
'app.log' => \DI\factory(fn() => newFileLogger('app.log')),
'a.log' => \DI\factory(fn() => newFileLogger('a.log')),
'b.log' => \DI\factory(fn() => newFileLogger('b.log')),
// Use "app.log" as default
LoggerInterface::class => \DI\get('app.log'),
// Use "a.log" for ServiceA
ServiceA::class => \DI\autowire()
->inject(LoggerInterface::class, \DI\get('a.log')),
// Use "b.log" for ServiceB
ServiceB::class => \DI\autowire()
->inject(LoggerInterface::class, \DI\get('b.log')),
]
I'd really like to see such a feature in php-di.
My current solution to this is to move this very specific configuration into my service-classes, which feels very unclean to me.
EDIT: I forgot to mention that using constructorParameter also works here and helps keeping the class clean.
class ServiceA {
publicfunction__construct(
#[Inject('a.log')]
privateLoggerInterface$log
){}
}
How would you guys solve this "problem"?
The text was updated successfully, but these errors were encountered:
This is actually my current solution in older projects. It kinda solves the problem and keeps the configuration aspect outside of the class. What I don‘t like about it is that it relies on the very specific parameter name. But maybe I‘m just too autistic here 😬
Do you think my idea is worth considering in future versions or is it too much off the rails / too niche?
It's very common to have multiple different logs within a single application, especially if it's a larger application. Therefore it would be nice if one could override an interface-definition just for a specific class.
Example: let's say we have two services:
ServiceA
andServiceB
. Both services demand aLoggerInterface
via constructor but the actual logfiles have to be different (let's say "a.log" and "b.log"). Now it would be cool if I could express this requirement with a definition that looks something like that:I'd really like to see such a feature in
php-di
.My current solution to this is to move this very specific configuration into my service-classes, which feels very unclean to me.
EDIT: I forgot to mention that using constructorParameter also works here and helps keeping the class clean.
How would you guys solve this "problem"?
The text was updated successfully, but these errors were encountered: