diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d2549cc..4a440e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,8 +14,9 @@ jobs: - 8.1 - 8.2 - 8.3 + - 8.4 include: - - php: 8.4 + - php: 8.5 prefer: stable experimental: true prefer: diff --git a/CHANGELOG.md b/CHANGELOG.md index 29cf776..d4d8c25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed +- Implicitly marking parameters as nullable is deprecated, the explicit nullable type must be used instead + +### Added +- Loggers map callable members +- PHP 8.4 mainline support +- PHP 8.5 experimental support + ## [4.0.1] - 2024-10-09 ### Fixed diff --git a/Dockerfile b/Dockerfile index 8c62c34..6b32a2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM php:8.0-cli +FROM php:8.4-cli RUN apt-get update \ && apt-get install -y \ diff --git a/README.md b/README.md index 91a7d24..d60cc07 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ return [ ], 'loggers' => [ // For suitable logger injections use map, where keys are your services, that implement LoggerAwareInterface - // and value is logger instances + // and value is logger instances or callable with logic for instantiate it. LoggerAwareClass::class => $logger, - AnotherLoggerAwareClass::class => $anotherLogger, + AnotherLoggerAwareClass::class => fn(\Psr\Container\ContainerInterface $container) => new \Psr\Log\NullLogger(), ], ]; ``` diff --git a/composer.json b/composer.json index 2beb8ac..e0178bd 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^9.3" + "phpunit/phpunit": "^9.6" }, "license": "BSD-2-Clause", "authors": [ diff --git a/src/Injector.php b/src/Injector.php index 9d1c113..e49e4e7 100644 --- a/src/Injector.php +++ b/src/Injector.php @@ -29,7 +29,7 @@ class Injector implements ContainerInterface private array $loggersMap = []; - public function __construct(LoggerInterface $logger = null) + public function __construct(?LoggerInterface $logger = null) { $this->loggerHelper = new LoggerHelper($logger ?: new NullLogger()); } @@ -87,6 +87,9 @@ public function createInstance($class): object if ($this->isLoggerInjectionRequired($instance)) { if(array_key_exists($class, $this->loggersMap)) { $logger = $this->loggersMap[$class]; + if(!$logger instanceof LoggerInterface && is_callable($logger)) { + $logger = $logger($this); + } } else { $logger = $this->getService(LoggerInterface::class); } @@ -97,7 +100,7 @@ public function createInstance($class): object return $instance; } - public function registerService($implementation, string $interface = null): void + public function registerService($implementation, ?string $interface = null): void { $interface = $interface ?: $implementation; if (isset($this->serviceMap[$interface])) { diff --git a/tests/Fixture/ClassWithNullableConstructorArgs.php b/tests/Fixture/ClassWithNullableConstructorArgs.php index 93becc9..b4b4f71 100644 --- a/tests/Fixture/ClassWithNullableConstructorArgs.php +++ b/tests/Fixture/ClassWithNullableConstructorArgs.php @@ -14,7 +14,7 @@ class ClassWithNullableConstructorArgs */ private $anotherService; - public function __construct(AnotherServiceInterface $anotherService = null) + public function __construct(?AnotherServiceInterface $anotherService = null) { $this->anotherService = $anotherService ?: new DefaultAnotherServiceImpl(); } @@ -28,4 +28,4 @@ public function getAnotherService(): AnotherServiceInterface class DefaultAnotherServiceImpl implements AnotherServiceInterface { -} \ No newline at end of file +} diff --git a/tests/Fixture/ClassWithTypedScalarConstructorArgDefaultValue.php b/tests/Fixture/ClassWithTypedScalarConstructorArgDefaultValue.php index e4ffd16..0499081 100644 --- a/tests/Fixture/ClassWithTypedScalarConstructorArgDefaultValue.php +++ b/tests/Fixture/ClassWithTypedScalarConstructorArgDefaultValue.php @@ -22,7 +22,7 @@ class ClassWithTypedScalarConstructorArgDefaultValue */ private $notTypedArg; - public function __construct(int $value = self::DEFAULT_VALUE, array $array = null, $notTypedArg = null) + public function __construct(int $value = self::DEFAULT_VALUE, ?array $array = null, $notTypedArg = null) { $this->value = $value; $this->array = $array; @@ -33,4 +33,4 @@ public function getValue() { return $this->value; } -} \ No newline at end of file +} diff --git a/tests/InjectorTest.php b/tests/InjectorTest.php index 34c19df..60ed8b8 100644 --- a/tests/InjectorTest.php +++ b/tests/InjectorTest.php @@ -232,13 +232,14 @@ public function testLoggerMap() { $logger = new NullLogger(); $anotherLogger = new NullLogger(); + $anotherLoggerCallable = fn() => $anotherLogger; $injector = new Injector(); $injector->allowInstantiateNotRegisteredTypes(true); $injector->enableLoggerAwareInjection(); $injector->setLoggersMap([ LoggerAwareClass::class => $logger, - AnotherLoggerAwareClass::class => $anotherLogger, + AnotherLoggerAwareClass::class => $anotherLoggerCallable, ]); /** @var LoggerAwareClass $loggerAwareInstance */