From 4d0a4edbf15e496a52803782750372d1112fd8fc Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Sat, 2 Mar 2024 22:13:09 -0500 Subject: [PATCH 1/2] feat: support for dependency injection has been extended for its classes at the defined levels --- src/DependencyInjection/Container.php | 4 +++- tests/ContainerTest.php | 27 ++++++++++++++++++++++----- tests/Provider/ClassProvider.php | 18 ++++++++++++++++++ tests/Provider/ExtendsProvider.php | 5 +++++ tests/Provider/SubClassProvider.php | 27 +++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 tests/Provider/SubClassProvider.php diff --git a/src/DependencyInjection/Container.php b/src/DependencyInjection/Container.php index da2af86..11a6451 100644 --- a/src/DependencyInjection/Container.php +++ b/src/DependencyInjection/Container.php @@ -125,7 +125,9 @@ private function getParameters(ReflectionFunctionAbstract $method, array $params if (!empty($params[$parameter->getName()])) { $args[] = $params[$parameter->getName()]; } else { - $args[] = $this->container->get($this->getParameterClassName($parameter)); + $args[] = $this->injectDependencies( + $this->container->get($this->getParameterClassName($parameter)) + ); } } } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 9de768d..c722ca8 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -29,7 +29,8 @@ class ContainerTest extends Test './tests/Provider/ContainerProviderTrait.php', './tests/Provider/CustomClass.php', './tests/Provider/ExtendsProvider.php', - './tests/Provider/FactoryProvider.php' + './tests/Provider/FactoryProvider.php', + './tests/Provider/SubClassProvider.php' ]; const REFLECTION_PARAMETERS = [CustomClass::class, 'setFactoryProvider']; @@ -81,16 +82,16 @@ public function testGetParameters(): void { $parameters = $this->getPrivateMethod( 'getParameters', - [new ReflectionMethod(new CustomClass(), 'setFactoryProvider') - ]); + [new ReflectionMethod(new CustomClass(), 'setFactoryProvider')] + ); $this->assertIsArray($parameters); $this->assertInstanceOf(FactoryProvider::class, reset($parameters)); $parameters = $this->getPrivateMethod( 'getParameters', - [new ReflectionMethod(new CustomClass(), 'setMultiple') - ]); + [new ReflectionMethod(new CustomClass(), 'setMultiple')] + ); $this->assertIsArray($parameters); @@ -163,6 +164,22 @@ public function testInjectDependenciesWithExtendsClass(): void $this->assertInstanceOf(FactoryProvider::class, $classProvider->getFactoryProvider()); } + public function testInjectDependenciesWithSubDependencies(): void + { + /** @var ClassProvider $classProvider */ + $classProvider = $this->container->injectDependencies(new ClassProvider); + + $str = $classProvider + ->getSubClassProvider() + ->getExtendsProvider() + ->getFactoryProviderExtends() + ->setStr(self::STR) + ->getStr(); + + $this->assertIsString($str); + $this->assertSame(self::STR, $str); + } + public function testGetParameterClassName() { $reflectionParameter = new ReflectionParameter(self::REFLECTION_PARAMETERS, 'factoryProvider'); diff --git a/tests/Provider/ClassProvider.php b/tests/Provider/ClassProvider.php index 42b47b8..6c70a7c 100644 --- a/tests/Provider/ClassProvider.php +++ b/tests/Provider/ClassProvider.php @@ -5,9 +5,27 @@ namespace Tests\Provider; use Tests\Provider\ExtendsProvider; +use Tests\Provider\SubClassProvider; class ClassProvider extends ExtendsProvider { + private SubClassProvider $subClassProvider; + + /** + * @required + */ + public function setSubClassProvider(SubClassProvider $subClassProvider): ClassProvider + { + $this->subClassProvider = $subClassProvider; + + return $this; + } + + public function getSubClassProvider(): SubClassProvider + { + return $this->subClassProvider; + } + public function getFactoryProvider(): FactoryProvider { return parent::getFactoryProvider(); diff --git a/tests/Provider/ExtendsProvider.php b/tests/Provider/ExtendsProvider.php index dd1b6da..68d20be 100644 --- a/tests/Provider/ExtendsProvider.php +++ b/tests/Provider/ExtendsProvider.php @@ -18,6 +18,11 @@ public function setFactoryProvider(FactoryProvider $factoryProvider): void $this->factoryProvider = $factoryProvider; } + public function getFactoryProviderExtends(): FactoryProvider + { + return $this->factoryProvider; + } + protected function getFactoryProvider(): FactoryProvider { return $this->factoryProvider; diff --git a/tests/Provider/SubClassProvider.php b/tests/Provider/SubClassProvider.php new file mode 100644 index 0000000..971c601 --- /dev/null +++ b/tests/Provider/SubClassProvider.php @@ -0,0 +1,27 @@ +extendsProvider = $extendsProvider; + + return $this; + } + + public function getExtendsProvider(): ExtendsProvider + { + return $this->extendsProvider; + } +} From dff25bed72d715fadad36842c01407d693c3e9dd Mon Sep 17 00:00:00 2001 From: Sleon4 Date: Sat, 2 Mar 2024 22:23:57 -0500 Subject: [PATCH 2/2] perf: obtaining values obtained from variables that have default values has been supported --- src/DependencyInjection/Container.php | 6 +++++- tests/ContainerTest.php | 26 ++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/DependencyInjection/Container.php b/src/DependencyInjection/Container.php index 11a6451..fefbe91 100644 --- a/src/DependencyInjection/Container.php +++ b/src/DependencyInjection/Container.php @@ -120,7 +120,11 @@ private function getParameters(ReflectionFunctionAbstract $method, array $params foreach ($method->getParameters() as $parameter) { if ($parameter->isDefaultValueAvailable()) { - $args[] = $parameter->getDefaultValue(); + if (!empty($params[$parameter->getName()])) { + $args[] = $params[$parameter->getName()]; + } else { + $args[] = $parameter->getDefaultValue(); + } } else { if (!empty($params[$parameter->getName()])) { $args[] = $params[$parameter->getName()]; diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index c722ca8..09422e2 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -21,6 +21,7 @@ class ContainerTest extends Test use ContainerProviderTrait; const STR = 'test'; + const DEFAULT_VALUE = 'default-value'; const FOLDER = './tests/'; const PATH_FILE = './Provider/CustomClass.php'; const FILES = [ @@ -87,7 +88,10 @@ public function testGetParameters(): void $this->assertIsArray($parameters); $this->assertInstanceOf(FactoryProvider::class, reset($parameters)); + } + public function testGetParametersWithDefaultValue(): void + { $parameters = $this->getPrivateMethod( 'getParameters', [new ReflectionMethod(new CustomClass(), 'setMultiple')] @@ -95,14 +99,32 @@ public function testGetParameters(): void $this->assertIsArray($parameters); - $first = reset($parameters); $second = end($parameters); - $this->assertInstanceOf(FactoryProvider::class, $first); + $this->assertInstanceOf(FactoryProvider::class, reset($parameters)); $this->assertIsString($second); $this->assertSame(self::STR, $second); } + public function testGetParametersWithDefaultDeclaredValue(): void + { + $parameters = $this->getPrivateMethod( + 'getParameters', + [ + new ReflectionMethod(new CustomClass(), 'setMultiple'), + ['str' => self::DEFAULT_VALUE] + ] + ); + + $this->assertIsArray($parameters); + + $second = end($parameters); + + $this->assertInstanceOf(FactoryProvider::class, reset($parameters)); + $this->assertIsString($second); + $this->assertSame(self::DEFAULT_VALUE, $second); + } + public function testInjectDependenciesMethod(): void { /** @var FactoryProvider $factoryProvider */