From 045ee67824c378edf7719eea10ab0cb29a48659d Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Thu, 18 Jan 2024 21:52:43 -0500 Subject: [PATCH 1/3] ci: import usage has been updated --- composer.lock | 42 +++++++++++++-------------- src/DependencyInjection/Container.php | 2 +- tests/ContainerTest.php | 4 +-- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/composer.lock b/composer.lock index b0c5f8e..fa428d6 100644 --- a/composer.lock +++ b/composer.lock @@ -68,29 +68,29 @@ }, { "name": "lion/helpers", - "version": "v3.0.0", + "version": "v3.1.0", "source": { "type": "git", "url": "https://github.com/lion-packages/helpers.git", - "reference": "19fdbd3ad6a10c69c59f24776df9224acb2f20e2" + "reference": "34799142861db5292263e0d6ca5728b581a7973c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/lion-packages/helpers/zipball/19fdbd3ad6a10c69c59f24776df9224acb2f20e2", - "reference": "19fdbd3ad6a10c69c59f24776df9224acb2f20e2", + "url": "https://api.github.com/repos/lion-packages/helpers/zipball/34799142861db5292263e0d6ca5728b581a7973c", + "reference": "34799142861db5292263e0d6ca5728b581a7973c", "shasum": "" }, "require": { "php": ">=8.2" }, "require-dev": { - "lion/test": "^1.1", + "lion/test": "^1.4", "phpunit/phpunit": "^10.5" }, "type": "library", "autoload": { "psr-4": { - "LionHelpers\\": "src/LionHelpers/" + "Lion\\Helpers\\": "src/LionHelpers/" } }, "notification-url": "https://packagist.org/downloads/", @@ -100,9 +100,9 @@ "description": "easy to use helpers for any type of use", "support": { "issues": "https://github.com/lion-packages/helpers/issues", - "source": "https://github.com/lion-packages/helpers/tree/v3.0.0" + "source": "https://github.com/lion-packages/helpers/tree/v3.1.0" }, - "time": "2023-12-15T00:42:36+00:00" + "time": "2024-01-19T02:47:44+00:00" }, { "name": "php-di/invoker", @@ -289,16 +289,16 @@ "packages-dev": [ { "name": "lion/test", - "version": "v1.3.0", + "version": "v1.4.0", "source": { "type": "git", "url": "https://github.com/lion-packages/test.git", - "reference": "47e8da88abe944e14fcba78c4f754b452bb226a1" + "reference": "c0eed0c33478b27a521258c94b053dd68d8e7bbc" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/lion-packages/test/zipball/47e8da88abe944e14fcba78c4f754b452bb226a1", - "reference": "47e8da88abe944e14fcba78c4f754b452bb226a1", + "url": "https://api.github.com/repos/lion-packages/test/zipball/c0eed0c33478b27a521258c94b053dd68d8e7bbc", + "reference": "c0eed0c33478b27a521258c94b053dd68d8e7bbc", "shasum": "" }, "require": { @@ -310,7 +310,7 @@ "type": "library", "autoload": { "psr-4": { - "LionTest\\": "src/LionTest/" + "Lion\\Test\\": "src/LionTest/" } }, "notification-url": "https://packagist.org/downloads/", @@ -320,9 +320,9 @@ "description": "library to implement testing with helpers that allow easy testing with PHPUnit", "support": { "issues": "https://github.com/lion-packages/test/issues", - "source": "https://github.com/lion-packages/test/tree/v1.3.0" + "source": "https://github.com/lion-packages/test/tree/v1.4.0" }, - "time": "2023-12-26T16:33:29+00:00" + "time": "2024-01-09T14:44:37+00:00" }, { "name": "myclabs/deep-copy", @@ -875,16 +875,16 @@ }, { "name": "phpunit/phpunit", - "version": "10.5.5", + "version": "10.5.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "ed21115d505b4b4f7dc7b5651464e19a2c7f7856" + "reference": "e5c5b397a95cb0db013270a985726fcae93e61b8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ed21115d505b4b4f7dc7b5651464e19a2c7f7856", - "reference": "ed21115d505b4b4f7dc7b5651464e19a2c7f7856", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/e5c5b397a95cb0db013270a985726fcae93e61b8", + "reference": "e5c5b397a95cb0db013270a985726fcae93e61b8", "shasum": "" }, "require": { @@ -956,7 +956,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.5" + "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.7" }, "funding": [ { @@ -972,7 +972,7 @@ "type": "tidelift" } ], - "time": "2023-12-27T15:13:52+00:00" + "time": "2024-01-14T16:40:30+00:00" }, { "name": "sebastian/cli-parser", diff --git a/src/DependencyInjection/Container.php b/src/DependencyInjection/Container.php index c83279d..1983e57 100644 --- a/src/DependencyInjection/Container.php +++ b/src/DependencyInjection/Container.php @@ -6,7 +6,7 @@ use DI\Container as DIContainer; use DI\ContainerBuilder; -use LionHelpers\Str; +use Lion\Helpers\Str; use ReflectionClass; use ReflectionParameter; diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 61064c3..a0e9bbd 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -5,9 +5,9 @@ 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 ReflectionParameter; use Tests\Provider\CustomClass; use Tests\Provider\FactoryProvider; From 05968fa4cf7e476f7923027c3fd341fd434f7699 Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Thu, 18 Jan 2024 22:16:48 -0500 Subject: [PATCH 2/3] feat: getParameters method has been added --- src/DependencyInjection/Container.php | 19 ++++++++++--------- tests/ContainerTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/DependencyInjection/Container.php b/src/DependencyInjection/Container.php index 1983e57..94c95f4 100644 --- a/src/DependencyInjection/Container.php +++ b/src/DependencyInjection/Container.php @@ -8,6 +8,7 @@ use DI\ContainerBuilder; use Lion\Helpers\Str; use ReflectionClass; +use ReflectionMethod; use ReflectionParameter; class Container @@ -48,6 +49,14 @@ 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 injectDependencies(object $object): object { $reflectionClass = new ReflectionClass($object); @@ -57,15 +66,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)); } } } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index a0e9bbd..7da56d3 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -8,6 +8,7 @@ use Lion\DependencyInjection\Container; use Lion\Helpers\Str; use Lion\Test\Test; +use ReflectionMethod; use ReflectionParameter; use Tests\Provider\CustomClass; use Tests\Provider\FactoryProvider; @@ -15,6 +16,7 @@ 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', @@ -47,6 +49,29 @@ 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 testInjectDependencies(): void { /** @var CustomClass $customClass */ From 5b35e2242ebe947c48550053acfaa444331fb943 Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Thu, 18 Jan 2024 22:28:14 -0500 Subject: [PATCH 3/3] feat: injectDependenciesMethod method added --- src/DependencyInjection/Container.php | 7 +++++++ tests/ContainerTest.php | 9 +++++++-- tests/Provider/CustomClass.php | 5 +++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/DependencyInjection/Container.php b/src/DependencyInjection/Container.php index 94c95f4..816feb6 100644 --- a/src/DependencyInjection/Container.php +++ b/src/DependencyInjection/Container.php @@ -57,6 +57,13 @@ private function getParameters(ReflectionMethod $method): array ); } + 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); diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 7da56d3..7cadf03 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -72,13 +72,18 @@ public function exampleMethod(FactoryProvider $factoryProvider): FactoryProvider $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()); } diff --git a/tests/Provider/CustomClass.php b/tests/Provider/CustomClass.php index 9da0a62..a97224c 100644 --- a/tests/Provider/CustomClass.php +++ b/tests/Provider/CustomClass.php @@ -22,4 +22,9 @@ public function setFactoryProvider(FactoryProvider $factoryProvider): void { $this->factoryProvider = $factoryProvider; } + + public function setFactoryProviderSecond(FactoryProvider $factoryProvider): FactoryProvider + { + return $factoryProvider; + } }