I'm currently experiment with Auryn and am attempting to make a specific instance of a Monolog logger available everywhere via a dependency injection. The codebase I'm working with is using the PSR LoggerAwareInterface to indicate that a class can have a logger injected (via setlogger).
I'm trying to set it up as follows:
$logger = new Logger ("myApp", [new SyslogHandler ("myAppLog", LOG_USER, Logger::DEBUG)]);
$auryn = new Injector ();
$auryn -> share ($logger);
$auryn -> prepare (LoggerAwareInterface::class, function (LoggerAwareInterface $client)
{
$client -> setLogger ($this -> make (Logger::class));
});
However, this doesn't appear to work because $this is not in an object context (though PHP 5.4 is supposed to support use of $this in a closure).
I've also tried writing it as follows.
$auryn -> prepare (LoggerAwareInterface::class, function (LoggerAwareInterface $client) use ($logger)
{
$client -> setLogger ($logger);
});
This does work if $logger
exists in the scope where the $auryn -> prepare ()
call is made, but I really don't like it at all because it now requires $logger
to exist outside Auryn which effectively makes it global state and seems to defeat the point of a dependency injection container in the first place. I'd much rather share the logger into Auryn and then use the shared instance inside the callback.
As I'm new to Auryn there might be an obvious solution I'm overlooking. If so, then what am I doing wrong here?