From 3880d4c49c3ee3a5e363683be257f895ebf5cd83 Mon Sep 17 00:00:00 2001
From: filippo.bertilotti <filippobertilotti@gmail.com>
Date: Wed, 15 May 2024 10:37:36 +0200
Subject: [PATCH] controllo errori sul tipo (parte 92)

---
 src/Framework/Container.php |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/Framework/Container.php b/src/Framework/Container.php
index cb4a918..3bef332 100644
--- a/src/Framework/Container.php
+++ b/src/Framework/Container.php
@@ -2,9 +2,20 @@
 
 namespace Framework;
 use ReflectionClass;
+use Closure;
+use ReflectionNamedType;
 
 class Container {
-    public function get(string $class_name):object {
+
+    private array $registry = [];
+
+    public function set(string $name, Closure $value): void {
+        $this->registry[$name] = $value;
+    }
+    public function get(string $class_name): object {
+        if(array_key_exists($class_name, $this->registry)) {
+            return $this->registry[$class_name]();
+        }
         $reflector = new ReflectionClass($class_name);
         $contructor = $reflector->getConstructor();
 
@@ -15,7 +26,20 @@
         }
 
         foreach($contructor->getParameters() as $param) {
-            $type = (string) $param->getType();
+            $type = $param->getType();
+            
+            if($type === null) {
+                exit("Constructor parameter '{$param->getName()}' in the $class_name class has no type declaration");
+            }
+
+            if( ! ($type instanceof ReflectionNamedType)) {
+                exit("Constructor parameter '{$param->getName()}' in the $class_name class is an invalid type: $type
+                     - only single named type supported");
+            }
+
+            if($type->isBuiltin()) {
+                exit("Unable to resolve costructor parameter '{$param->getName()}' of type '$type' in the '$class_name' class");
+            }
             $dependencies[] = $this->get($type);
         }
         

--
Gitblit v1.8.0