From a212c53dc537ce66800b8e987fb18b1aab994bb4 Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Fri, 17 May 2024 11:59:51 +0200
Subject: [PATCH] creazione file config apposito per i servizi vari (accesso a database per esempio) (parte 108)

---
 src/Framework/Container.php |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/Framework/Container.php b/src/Framework/Container.php
index 7e90757..cde3165 100644
--- a/src/Framework/Container.php
+++ b/src/Framework/Container.php
@@ -1,8 +1,10 @@
 <?php
-
+declare(strict_types= 1);
 namespace Framework;
 use ReflectionClass;
 use Closure;
+use ReflectionNamedType;
+use InvalidArgumentException;
 
 class Container {
 
@@ -25,8 +27,21 @@
         }
 
         foreach($contructor->getParameters() as $param) {
-            $type = (string) $param->getType();
-            $dependencies[] = $this->get($type);
+            $type = $param->getType();
+            
+            if($type === null) {
+                throw new InvalidArgumentException("Constructor parameter '{$param->getName()}' in the $class_name class has no type declaration");
+            }
+
+            if( ! ($type instanceof ReflectionNamedType)) {
+                throw new InvalidArgumentException("Constructor parameter '{$param->getName()}' in the $class_name class is an invalid type: $type
+                     - only single named type supported");
+            }
+
+            if($type->isBuiltin()) {
+                throw new InvalidArgumentException("Unable to resolve costructor parameter '{$param->getName()}' of type '$type' in the '$class_name' class");
+            }
+            $dependencies[] = $this->get((string) $type);
         }
         
         return new $class_name(...$dependencies);

--
Gitblit v1.8.0