diff --git a/CHANGELOG.md b/CHANGELOG.md index a3a98aa..3a1d933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +## [4.1.1] - 2025-03-21 + +## Fixed +- Inject NullLogger to LoggerAwareInterface services, when logger is not set in container + ## [4.1.0] - 2025-03-15 ### Fixed @@ -125,7 +130,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - All classes. -[Unreleased]: https://github.com/FreeElephants/php-di/compare/4.1.0...HEAD +[Unreleased]: https://github.com/FreeElephants/php-di/compare/4.1.1...HEAD +[4.1.1]: https://github.com/FreeElephants/php-di/compare/4.1.0...4.1.1 [4.1.0]: https://github.com/FreeElephants/php-di/compare/4.0.1...4.1.0 [4.0.1]: https://github.com/FreeElephants/php-di/compare/4.0.0...4.0.1 [4.0.0]: https://github.com/FreeElephants/php-di/compare/3.1.1...4.0.0 diff --git a/src/Injector.php b/src/Injector.php index e49e4e7..250a5ce 100644 --- a/src/Injector.php +++ b/src/Injector.php @@ -91,7 +91,7 @@ public function createInstance($class): object $logger = $logger($this); } } else { - $logger = $this->getService(LoggerInterface::class); + $logger = $this->has(LoggerInterface::class) ? $this->getService(LoggerInterface::class) : new NullLogger(); } $instance->setLogger($logger); diff --git a/tests/InjectorTest.php b/tests/InjectorTest.php index 60ed8b8..431fffe 100644 --- a/tests/InjectorTest.php +++ b/tests/InjectorTest.php @@ -228,6 +228,17 @@ public function testLoggerInjection() $this->assertSame($logger, $loggerAware->getLogger()); } + public function testLoggerInjectionWithoutSpecifiedDefaultLoggerImpl() + { + $injector = new Injector(); + $injector->enableLoggerAwareInjection(true); + + /**@var LoggerAwareClass $loggerAware */ + $loggerAware = $injector->createInstance(LoggerAwareClass::class); + + $this->assertInstanceOf(NullLogger::class, $loggerAware->getLogger()); + } + public function testLoggerMap() { $logger = new NullLogger();