diff --git a/src/Traits/GetDependenciesTrait.php b/src/Traits/GetDependenciesTrait.php index c0e8715e..b982346a 100644 --- a/src/Traits/GetDependenciesTrait.php +++ b/src/Traits/GetDependenciesTrait.php @@ -18,7 +18,7 @@ public function resolveClassMethodDependencies(object $instance, string $method) }, (new ReflectionMethod($instance, $method))->getParameters()); } - protected function transformDependency(ReflectionParameter $parameter) + protected function transformDependency(ReflectionParameter $parameter): ?string { $type = $parameter->getType(); @@ -29,9 +29,11 @@ protected function transformDependency(ReflectionParameter $parameter) return interface_exists($type->getName()) ? $this->getClassByInterface($type->getName()) : $type->getName(); } - protected function getClassByInterface($interfaceName) + protected function getClassByInterface($interfaceName): ?string { - $bindings = Container::getInstance()->getBindings(); + $app = Container::getInstance(); + + $bindings = $app->getBindings(); $implementation = Arr::get($bindings, "{$interfaceName}.concrete"); @@ -39,8 +41,6 @@ protected function getClassByInterface($interfaceName) return null; } - $classFields = (new ReflectionFunction($implementation))->getStaticVariables(); - - return $classFields['concrete']; + return get_class(call_user_func($implementation, $app)); } } diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index bef5d334..0516d5ca 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -682,9 +682,23 @@ public function testAddDataWithNotExistsMethodOnController() $service->addData($request, $response); } - public function testAddDataWithBindingInterface() + public static function getImplementations(): array { - $this->app->bind(TestContract::class, TestRequest::class); + return [ + [ + 'implementation' => TestRequest::class, + ], + [ + 'implementation' => fn ($app) => new TestRequest(), + ], + ]; + } + + #[DataProvider('getImplementations')] + public function testAddDataWithBindingInterface($implementation) + { + $this->app->bind(TestContract::class, $implementation); + $this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request')); $service = app(SwaggerService::class);