Go to file
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
app Add controller method autowiring 2023-08-09 00:49:47 +02:00
public Properly implement the router with config and interfaces 2023-08-07 17:51:53 +02:00
src Add controller method autowiring 2023-08-09 00:49:47 +02:00
.gitignore First working version with non working routes 2023-07-28 16:14:55 +02:00
composer.json Properly implement the router with config and interfaces 2023-08-07 17:51:53 +02:00
composer.lock Properly implement the router with config and interfaces 2023-08-07 17:51:53 +02:00
README.md Add response and request objects and interfaces 2023-08-07 14:23:00 +02:00

Todos:

  • Collect routes properly
  • Process routes correctly
  • Add request and response objects

Routing: Currently the routes are hardcoded, we need we way to process all classes maybe and sort them into a list of routes.