UnderCurrent/src
tim 64ff0c243d 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
2023-08-09 00:49:47 +02:00
..
Attribute Implement basic version of route matching 2023-08-08 16:53:27 +02:00
Collector Properly implement the router with config and interfaces 2023-08-07 17:51:53 +02:00
Container Expand the response with headers and set status code 2023-08-08 17:18:18 +02:00
Http Add controller method autowiring 2023-08-09 00:49:47 +02:00
Kernel Expand the response with headers and set status code 2023-08-08 17:18:18 +02:00