Add controller method autowiring
diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php
index 8361e9d..282a990 100644
--- a/app/Controller/HelloWorldController.php
+++ b/app/Controller/HelloWorldController.php
@@ -5,6 +5,7 @@ namespace App\Controller;
 use Ardent\Undercurrent\Attribute\Route;
 use Ardent\Undercurrent\Http\GenericResponse;
 use Ardent\Undercurrent\Http\ResponseInterface;
+use Ardent\Undercurrent\Http\RouterConfig;
 use Ardent\Undercurrent\Http\StatusEnum;
 class HelloWorldController
@@ -26,4 +27,21 @@ class HelloWorldController
     {
         return new GenericResponse('Hello World!');
     }
+
+    #[Route('/routes')]
+    public function routes(RouterConfig $config): ResponseInterface
+    {
+        $routes = implode('<br>', array_map(
+            fn($route) => $route->getRoute()->path,
+            $config->getRoutes()
+        ));
+
+        return new GenericResponse($routes);
+    }
+
+    #[Route('/world/{name}')]
+    public function world(string $name): ResponseInterface
+    {
+        return new GenericResponse("Hello $name!");
+    }
 }
\ No newline at end of file
diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php
index b2d8e1d..23e9311 100644
--- a/src/Http/GenericRouter.php
+++ b/src/Http/GenericRouter.php
@@ -4,6 +4,8 @@ namespace Ardent\Undercurrent\Http;
 use Ardent\Undercurrent\Container\ClassNotFoundException;
 use Ardent\Undercurrent\Container\ContainerInterface;
+use Exception;
+use ReflectionMethod;
 class GenericRouter implements RouterInterface
 {
@@ -23,10 +25,26 @@ class GenericRouter implements RouterInterface
             if ($route->getRoute()->path === $request->getUri()) {
                 $controller = $this->container->get($route->getController());
                 $method = $route->getMethod();
-                return $controller->$method();
+                return $controller->$method(...$this->autowire($route));
             }
         }
         throw new RouteNotFoundException($request);
     }
+
+    private function autowire(RouteConfig $route): array
+    {
+        $reflectionMethod = new ReflectionMethod($route->getController(), $route->getMethod());
+
+        $params = [];
+        foreach ($reflectionMethod->getParameters() as $parameter) {
+            $type = $parameter->getType();
+            if (!$type) {
+                throw new Exception('Cannot autowire parameter without type');
+            }
+            $params[] = $this->container->get($type->getName());
+        }
+
+        return $params;
+    }
 }
\ No newline at end of file
			
			
This commit is contained in:
		| @@ -5,6 +5,7 @@ namespace App\Controller; | ||||
| use Ardent\Undercurrent\Attribute\Route; | ||||
| use Ardent\Undercurrent\Http\GenericResponse; | ||||
| use Ardent\Undercurrent\Http\ResponseInterface; | ||||
| use Ardent\Undercurrent\Http\RouterConfig; | ||||
| use Ardent\Undercurrent\Http\StatusEnum; | ||||
|  | ||||
| class HelloWorldController | ||||
| @@ -26,4 +27,21 @@ class HelloWorldController | ||||
|     { | ||||
|         return new GenericResponse('Hello World!'); | ||||
|     } | ||||
|  | ||||
|     #[Route('/routes')] | ||||
|     public function routes(RouterConfig $config): ResponseInterface | ||||
|     { | ||||
|         $routes = implode('<br>', array_map( | ||||
|             fn($route) => $route->getRoute()->path, | ||||
|             $config->getRoutes() | ||||
|         )); | ||||
|  | ||||
|         return new GenericResponse($routes); | ||||
|     } | ||||
|  | ||||
|     #[Route('/world/{name}')] | ||||
|     public function world(string $name): ResponseInterface | ||||
|     { | ||||
|         return new GenericResponse("Hello $name!"); | ||||
|     } | ||||
| } | ||||
| @@ -4,6 +4,8 @@ namespace Ardent\Undercurrent\Http; | ||||
|  | ||||
| use Ardent\Undercurrent\Container\ClassNotFoundException; | ||||
| use Ardent\Undercurrent\Container\ContainerInterface; | ||||
| use Exception; | ||||
| use ReflectionMethod; | ||||
|  | ||||
| class GenericRouter implements RouterInterface | ||||
| { | ||||
| @@ -23,10 +25,26 @@ class GenericRouter implements RouterInterface | ||||
|             if ($route->getRoute()->path === $request->getUri()) { | ||||
|                 $controller = $this->container->get($route->getController()); | ||||
|                 $method = $route->getMethod(); | ||||
|                 return $controller->$method(); | ||||
|                 return $controller->$method(...$this->autowire($route)); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         throw new RouteNotFoundException($request); | ||||
|     } | ||||
|  | ||||
|     private function autowire(RouteConfig $route): array | ||||
|     { | ||||
|         $reflectionMethod = new ReflectionMethod($route->getController(), $route->getMethod()); | ||||
|  | ||||
|         $params = []; | ||||
|         foreach ($reflectionMethod->getParameters() as $parameter) { | ||||
|             $type = $parameter->getType(); | ||||
|             if (!$type) { | ||||
|                 throw new Exception('Cannot autowire parameter without type'); | ||||
|             } | ||||
|             $params[] = $this->container->get($type->getName()); | ||||
|         } | ||||
|  | ||||
|         return $params; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user