Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up php versions #21

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 6 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ jobs:
matrix:
experimental: [false]
php:
- 7.3
- 7.4
- 8.0
- 8.1
- 8.2
- 8.3
include:
- php: 8.1
- php: 8.4
prefer: stable
experimental: true
prefer:
Expand All @@ -26,7 +27,7 @@ jobs:

steps:
- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
Expand All @@ -41,7 +42,7 @@ jobs:

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}-${{ matrix.prefer }}-
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- Logger mapping configuration

### Changed
- Test with new php versions, drop 7.x support

## [3.1.1] - 2021-09-02
### Changed
- Support all versions of psr/log and psr/container
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.3-cli

RUN apt-get update \
&& apt-get install -y \
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Framework-agnostic Dependency Injection tool and PSR-11 implementation provider.

## Requirements

PHP >=7.3|8.0
PHP >=8.0

## Installation

Expand Down Expand Up @@ -63,6 +63,12 @@ return [
500,
],
],
'loggers' => [
// For suitable logger injections use map, where keys are your services, that implement LoggerAwareInterface
// and value is logger instances
LoggerAwareClass::class => $logger,
AnotherLoggerAwareClass::class => $anotherLogger,
],
];
```

Expand All @@ -83,15 +89,24 @@ $di = (new \FreeElephants\DI\InjectorBuilder)->buildFromArray($components);
## Options:

### `allowNullableConstructorArgs`

Default value is `false`.

### `allowInstantiateNotRegisteredTypes`

Default value is `false`. When you set it `true`, you can register only specific interfaces instances.
All final typed dependency will be lazy-instantiated by chain!

### `useIdAsTypeName`

Default value is `true`.

### `enableLoggerAwareInjection`

Default value is `false`.

Allow to set LoggerInterface into LoggerAwareInterface instances after constructing, or use loggers map if type present.

## Conception

## [In Russian] Простейшее внедрение зависимостей через конструктор для PHP
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"prefer-stable": true,
"config": {
"platform": {
"php": "7.4"
"php": "8.3"
}
},
"require-dev": {
Expand All @@ -21,7 +21,7 @@
"require": {
"psr/log": "^1|^2|^3",
"psr/container": "^1|^2",
"php": "^7.3|^8.0"
"php": "^8.0"
},
"provide": {
"psr/container-implementation": "^1|^2"
Expand Down
30 changes: 12 additions & 18 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
processIsolation="false"
colors="true"
>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" backupGlobals="false" processIsolation="false" colors="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<clover outputFile="clover.xml"/>
<html outputDirectory="coverage" lowUpperBound="35" highLowerBound="70"/>
<text outputFile="php://stdout"/>
</report>
</coverage>
<php>
<ini name="error_reporting" value="E_ALL"/>
</php>
Expand All @@ -15,14 +18,5 @@
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-html" target="coverage" lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-text" target="php://stdout" lowUpperBound="70" highLowerBound="99" />
</logging>
<logging/>
</phpunit>
9 changes: 3 additions & 6 deletions src/CallableBeanContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
class CallableBeanContainer
{
/**
* @var callable|mixed|null
* @var callable|mixed
*/
private $function;
/**
* @var array
*/
private $args;
private mixed $function;
private array $args;

public function __construct(string $interface, $callable, ContainerInterface $container)
{
Expand Down
4 changes: 2 additions & 2 deletions src/EnvAwareConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
class EnvAwareConfigLoader implements ConfigLoaderInterface
{
private const DEFAULT_ENV_VAR_NAME = 'ENV';
private $envVariableName;
private $configBasePath;
private string $envVariableName;
private string $configBasePath;

public function __construct(string $configBasePath, string $envVariableName = self::DEFAULT_ENV_VAR_NAME)
{
Expand Down
62 changes: 43 additions & 19 deletions src/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@
class Injector implements ContainerInterface
{

private $serviceMap = [];
private array $serviceMap = [];

private $loggerHelper;
private LoggerHelper $loggerHelper;

private $allowNullableConstructorArgs = false;
private bool $allowNullableConstructorArgs = false;

private $allowInstantiateNotRegisteredTypes = false;
private bool $allowInstantiateNotRegisteredTypes = false;

private $useIdAsTypeName = true;
private bool $useIdAsTypeName = true;

private $enableLoggerAwareInjection = false;
private bool $enableLoggerAwareInjection = false;

private array $loggersMap = [];

public function __construct(LoggerInterface $logger = null)
{
Expand All @@ -37,7 +39,7 @@ public function getLoggerHelper(): LoggerHelper
return $this->loggerHelper;
}

public function setService(string $typeName, $service)
public function setService(string $typeName, $service): void
{
if ($this->useIdAsTypeName && false === $service instanceof $typeName) {
$this->loggerHelper->logNotMatchedTypeInstance($typeName, $service);
Expand All @@ -51,7 +53,7 @@ public function setService(string $typeName, $service)
$this->loggerHelper->logServiceSetting($typeName, $service);
}

public function createInstance($class)
public function createInstance($class): object
{
$reflectedClass = new \ReflectionClass($class);
if ($reflectedClass->isAbstract() || $reflectedClass->isInterface()) {
Expand Down Expand Up @@ -82,14 +84,20 @@ public function createInstance($class)

$instance = new $class(...$constructorParams);

if ($instance instanceof LoggerAwareInterface && $this->enableLoggerAwareInjection) {
$instance->setLogger($this->getService(LoggerInterface::class));
if ($this->isLoggerInjectionRequired($instance)) {
if(array_key_exists($class, $this->loggersMap)) {
$logger = $this->loggersMap[$class];
} else {
$logger = $this->getService(LoggerInterface::class);
}

$instance->setLogger($logger);
}

return $instance;
}

public function registerService($implementation, string $interface = null)
public function registerService($implementation, string $interface = null): void
{
$interface = $interface ?: $implementation;
if (isset($this->serviceMap[$interface])) {
Expand All @@ -100,7 +108,7 @@ public function registerService($implementation, string $interface = null)
$this->loggerHelper->logServiceRegistration($implementation, $interface);
}

public function getService(string $type)
public function getService(string $type): object
{
if (!array_key_exists($type, $this->serviceMap)) {
if ($this->allowInstantiateNotRegisteredTypes) {
Expand Down Expand Up @@ -135,7 +143,7 @@ public function merge(
string $instancesKey = InjectorBuilder::INSTANCES_KEY,
string $registerKey = InjectorBuilder::REGISTER_KEY,
string $callableKey = InjectorBuilder::CALLABLE_KEY
)
): void
{
$beansInstances = $components[$instancesKey] ?? [];
foreach ($beansInstances as $interface => $instance) {
Expand All @@ -159,17 +167,17 @@ public function merge(
}
}

public function allowNullableConstructorArgs(bool $allow)
public function allowNullableConstructorArgs(bool $allow): void
{
$this->allowNullableConstructorArgs = $allow;
}

public function allowInstantiateNotRegisteredTypes(bool $allowInstantiateNotRegisteredTypes)
public function allowInstantiateNotRegisteredTypes(bool $allowInstantiateNotRegisteredTypes): void
{
$this->allowInstantiateNotRegisteredTypes = $allowInstantiateNotRegisteredTypes;
}

public function registerItSelf(bool $asPsrContainer = true)
public function registerItSelf(bool $asPsrContainer = true): void
{
$this->setService(Injector::class, $this);

Expand All @@ -194,13 +202,29 @@ public function has($id): bool
return $this->hasImplementation($id);
}

public function useIdAsTypeName(bool $useIdAsTypeName = true)
public function useIdAsTypeName(bool $useIdAsTypeName = true): void
{
$this->useIdAsTypeName = $useIdAsTypeName;
}

public function enableLoggerAwareInjection(bool $enable = true)
public function enableLoggerAwareInjection(bool $enable = true): void
{
$this->enableLoggerAwareInjection = $enable;
}}
}

public function getLoggersMap(): array
{
return $this->loggersMap;
}

public function setLoggersMap(array $map): void
{
$this->loggersMap = $map;
}

private function isLoggerInjectionRequired(mixed $instance): bool
{
return $instance instanceof LoggerAwareInterface && $this->enableLoggerAwareInjection;
}
}

26 changes: 13 additions & 13 deletions src/InjectorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,24 @@ class InjectorBuilder
public const INSTANCES_KEY = 'instances';
public const REGISTER_KEY = 'register';
public const CALLABLE_KEY = 'callable';
/**
* @var string
*/
private $instancesKey;
/**
* @var string
*/
private $registerKey;
/**
* @var string
*/
private $callableKey;
public const LOGGERS_KEY = 'loggers';

private string $instancesKey;
private string $registerKey;
private string $callableKey;
private string $loggersKey;

public function __construct(
string $instancesKey = self::INSTANCES_KEY,
string $registerKey = self::REGISTER_KEY,
string $callableKey = self::CALLABLE_KEY
string $callableKey = self::CALLABLE_KEY,
string $loggersKey = self::LOGGERS_KEY,
)
{
$this->instancesKey = $instancesKey;
$this->registerKey = $registerKey;
$this->callableKey = $callableKey;
$this->loggersKey = $loggersKey;
}

public function buildFromArray(array $components): Injector
Expand All @@ -40,6 +36,10 @@ public function buildFromArray(array $components): Injector

$injector->merge($components, $this->instancesKey, $this->registerKey, $this->callableKey);

if (isset($components[$this->loggersKey])) {
$injector->setLoggersMap($components[$this->loggersKey]);
}

return $injector;
}
}
Loading