Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:8.0-cli
FROM php:8.4-cli

RUN apt-get update \
&& apt-get install -y \
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
];
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.6"
},
"license": "BSD-2-Clause",
"authors": [
Expand Down
7 changes: 5 additions & 2 deletions src/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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])) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/ClassWithNullableConstructorArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -28,4 +28,4 @@ public function getAnotherService(): AnotherServiceInterface
class DefaultAnotherServiceImpl implements AnotherServiceInterface
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,4 +33,4 @@ public function getValue()
{
return $this->value;
}
}
}
3 changes: 2 additions & 1 deletion tests/InjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down