Skip to content

Commit

Permalink
Merge pull request #3 from lion-packages/new
Browse files Browse the repository at this point in the history
Extended support for dependency injection
  • Loading branch information
Sleon4 authored Jan 19, 2024
2 parents cfe1e17 + 5b35e22 commit bb80bad
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 35 deletions.
42 changes: 21 additions & 21 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions src/DependencyInjection/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use DI\Container as DIContainer;
use DI\ContainerBuilder;
use LionHelpers\Str;
use Lion\Helpers\Str;
use ReflectionClass;
use ReflectionMethod;
use ReflectionParameter;

class Container
Expand Down Expand Up @@ -48,6 +49,21 @@ public function getNamespace(string $file, string $namespace, string $split): st
return $this->str->of("{$namespace}{$splitFile[1]}")->replace("/", "\\")->replace('.php', '')->trim()->get();
}

private function getParameters(ReflectionMethod $method): array
{
return array_map(
fn($parameter) => $this->container->get($this->getParameterClassName($parameter)),
$method->getParameters()
);
}

public function injectDependenciesMethod(object $object, string $method): mixed
{
$method = (new ReflectionClass($object))->getMethod($method);

return $method->invoke($object, ...$this->getParameters($method));
}

public function injectDependencies(object $object): object
{
$reflectionClass = new ReflectionClass($object);
Expand All @@ -57,15 +73,7 @@ public function injectDependencies(object $object): object

if (is_string($docDocument)) {
if ((bool) preg_match('/@required/', $docDocument)) {
$parameters = $method->getParameters();
$listParameters = [];

foreach ($parameters as $parameter) {
$dependencyName = $this->getParameterClassName($parameter);
$listParameters[] = $this->container->get($dependencyName);
}

$method->invoke($object, ...$listParameters);
$method->invoke($object, ...$this->getParameters($method));
}
}
}
Expand Down
38 changes: 34 additions & 4 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
namespace Tests;

use DI\Container as DIContainer;
use LionHelpers\Str;
use Lion\DependencyInjection\Container;
use LionTest\Test;
use Lion\Helpers\Str;
use Lion\Test\Test;
use ReflectionMethod;
use ReflectionParameter;
use Tests\Provider\CustomClass;
use Tests\Provider\FactoryProvider;

class ContainerTest extends Test
{
const FOLDER = './tests/';
const PATH_FILE = './Provider/CustomClass.php';
const FILES = [
'/var/www/html/tests/ContainerTest.php',
'/var/www/html/tests/Provider/CustomClass.php',
Expand Down Expand Up @@ -47,13 +49,41 @@ public function testGetFiles(): void
$this->assertSame(self::FILES, $files);
}

public function testGetNamespace(): void
{
$namespace = $this->container->getNamespace(self::PATH_FILE, 'Tests\\Provider\\', 'Provider/');

$this->assertIsString($namespace);
$this->assertSame(CustomClass::class, $namespace);
}

public function testGetParameters(): void
{
$class = new class {
public function exampleMethod(FactoryProvider $factoryProvider): FactoryProvider
{
return $factoryProvider;
}
};

$parameters = $this->getPrivateMethod('getParameters', [new ReflectionMethod($class, 'exampleMethod')]);

$this->assertIsArray($parameters);
$this->assertInstanceOf(FactoryProvider::class, reset($parameters));
}

public function testInjectDependenciesMethod(): void
{
$returnValue = $this->container->injectDependenciesMethod($this->customClass, 'setFactoryProviderSecond');

$this->assertInstanceOf(FactoryProvider::class, $returnValue);
}

public function testInjectDependencies(): void
{
/** @var CustomClass $customClass */
$customClass = $this->container->injectDependencies($this->customClass);

$this->initReflection($customClass);

$this->assertInstanceOf(CustomClass::class, $customClass);
$this->assertInstanceOf(FactoryProvider::class, $customClass->getFactoryProvider());
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Provider/CustomClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function setFactoryProvider(FactoryProvider $factoryProvider): void
{
$this->factoryProvider = $factoryProvider;
}

public function setFactoryProviderSecond(FactoryProvider $factoryProvider): FactoryProvider
{
return $factoryProvider;
}
}

0 comments on commit bb80bad

Please sign in to comment.