diff --git a/config/autoload/locks.global.php b/config/autoload/locks.global.php index cbf3a000..be9e7736 100644 --- a/config/autoload/locks.global.php +++ b/config/autoload/locks.global.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Shlinkio\Shlink\Common\Cache\RedisFactory; use Shlinkio\Shlink\Common\Lock\RetryLockStoreDelegatorFactory; +use Shlinkio\Shlink\Common\Logger\LoggerAwareDelegatorFactory; use Symfony\Component\Lock; use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory; @@ -27,6 +28,9 @@ return [ Lock\Store\RedisStore::class => [ RetryLockStoreDelegatorFactory::class, ], + Lock\Factory::class => [ + LoggerAwareDelegatorFactory::class, + ], ], ], diff --git a/module/Common/src/Logger/LoggerAwareDelegatorFactory.php b/module/Common/src/Logger/LoggerAwareDelegatorFactory.php new file mode 100644 index 00000000..8cb04499 --- /dev/null +++ b/module/Common/src/Logger/LoggerAwareDelegatorFactory.php @@ -0,0 +1,20 @@ +setLogger($container->get(Log\LoggerInterface::class)); + } + + return $instance; + } +} diff --git a/module/Common/test/Logger/LoggerAwareDelegatorFactoryTest.php b/module/Common/test/Logger/LoggerAwareDelegatorFactoryTest.php new file mode 100644 index 00000000..8723077b --- /dev/null +++ b/module/Common/test/Logger/LoggerAwareDelegatorFactoryTest.php @@ -0,0 +1,55 @@ +container = $this->prophesize(ContainerInterface::class); + $this->delegator = new LoggerAwareDelegatorFactory(); + } + + /** + * @test + * @dataProvider provideInstances + */ + public function injectsLoggerOnInstanceWhenImplementingLoggerAware($instance, int $expectedCalls): void + { + $callback = function () use ($instance) { + return $instance; + }; + $getLogger = $this->container->get(Log\LoggerInterface::class)->willReturn(new Log\NullLogger()); + + $result = ($this->delegator)($this->container->reveal(), '', $callback); + + $this->assertSame($instance, $result); + $getLogger->shouldHaveBeenCalledTimes($expectedCalls); + } + + public function provideInstances(): iterable + { + yield 'no logger aware' => [new stdClass(), 0]; + yield 'logger aware' => [new class implements Log\LoggerAwareInterface { + public function setLogger(LoggerInterface $logger): void + { + Assert::assertInstanceOf(Log\NullLogger::class, $logger); + } + }, 1]; + } +}