File tree Expand file tree Collapse file tree 4 files changed +8
-3
lines changed Expand file tree Collapse file tree 4 files changed +8
-3
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
10
10
- Implicitly marking parameters as nullable is deprecated, the explicit nullable type must be used instead
11
11
12
12
### Added
13
+ - Loggers map callable members
13
14
- PHP 8.4 mainline support
14
15
- PHP 8.5 experimental support
15
16
Original file line number Diff line number Diff line change @@ -65,9 +65,9 @@ return [
65
65
],
66
66
'loggers' => [
67
67
// For suitable logger injections use map, where keys are your services, that implement LoggerAwareInterface
68
- // and value is logger instances
68
+ // and value is logger instances or callable with logic for instantiate it.
69
69
LoggerAwareClass::class => $logger,
70
- AnotherLoggerAwareClass::class => $anotherLogger ,
70
+ AnotherLoggerAwareClass::class => fn(\Psr\Container\ContainerInterface $container) => new \Psr\Log\NullLogger() ,
71
71
],
72
72
];
73
73
```
Original file line number Diff line number Diff line change @@ -87,6 +87,9 @@ public function createInstance($class): object
87
87
if ($ this ->isLoggerInjectionRequired ($ instance )) {
88
88
if (array_key_exists ($ class , $ this ->loggersMap )) {
89
89
$ logger = $ this ->loggersMap [$ class ];
90
+ if (!$ logger instanceof LoggerInterface && is_callable ($ logger )) {
91
+ $ logger = $ logger ($ this );
92
+ }
90
93
} else {
91
94
$ logger = $ this ->getService (LoggerInterface::class);
92
95
}
Original file line number Diff line number Diff line change @@ -232,13 +232,14 @@ public function testLoggerMap()
232
232
{
233
233
$ logger = new NullLogger ();
234
234
$ anotherLogger = new NullLogger ();
235
+ $ anotherLoggerCallable = fn () => $ anotherLogger ;
235
236
236
237
$ injector = new Injector ();
237
238
$ injector ->allowInstantiateNotRegisteredTypes (true );
238
239
$ injector ->enableLoggerAwareInjection ();
239
240
$ injector ->setLoggersMap ([
240
241
LoggerAwareClass::class => $ logger ,
241
- AnotherLoggerAwareClass::class => $ anotherLogger ,
242
+ AnotherLoggerAwareClass::class => $ anotherLoggerCallable ,
242
243
]);
243
244
244
245
/** @var LoggerAwareClass $loggerAwareInstance */
You can’t perform that action at this time.
0 commit comments