From ce9b2119ceb911faab15fa43e741d63e3fb7834c Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Tue, 21 May 2024 10:09:46 +0200
Subject: [PATCH] aggiunto ref a ogni product e aggiunta eccezione di prodotto non trovato in caso non esista nel database (parte 119)

---
 src/Framework/Container.php |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/Framework/Container.php b/src/Framework/Container.php
index 5192389..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 {
 
@@ -26,10 +28,20 @@
 
         foreach($contructor->getParameters() as $param) {
             $type = $param->getType();
-            if($type->isBuiltin()) {
-                exit("Unable to resolve costructor parameter '{$param->getName()}' of type '$type' in the '$class_name' class");
+            
+            if($type === null) {
+                throw new InvalidArgumentException("Constructor parameter '{$param->getName()}' in the $class_name class has no type declaration");
             }
-            $dependencies[] = $this->get($type);
+
+            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