Go to file
Tim 2fd6e50b90 Start with controller parameter injection
diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php
index 23e9311..d5ffb95 100644
--- a/src/Http/GenericRouter.php
+++ b/src/Http/GenericRouter.php
@@ -22,16 +22,47 @@ class GenericRouter implements RouterInterface
     public function dispatch(RequestInterface $request): ResponseInterface
     {
         foreach ($this->config->getRoutes() as $route) {
-            if ($route->getRoute()->path === $request->getUri()) {
-                $controller = $this->container->get($route->getController());
-                $method = $route->getMethod();
-                return $controller->$method(...$this->autowire($route));
+            $params = $this->resolveParams($route->getRoute()->path, $request->getUri());
+
+            if ($params === null) {
+                continue;
             }
+
+            $controller = $this->container->get($route->getController());
+            $method = $route->getMethod();
+//            $params = $this->autowire($route) + $this->resolveParams($route->getRoute()->path, $request->getUri());
+            $params = $this->resolveParams($route->getRoute()->path, $request->getUri());
+            return $controller->$method(...$params);
         }

         throw new RouteNotFoundException($request);
     }

+    private function resolveParams(string $routeUri, string $requestUri): ?array
+    {
+        $result = preg_match_all('/{(\w+)}/', $routeUri, $tokens);
+
+        if (!$result) {
+            return null;
+        }
+
+        $tokens = $tokens[0];
+
+        $matchingRegex = '/^' . str_replace(
+                ['/', ...$tokens],
+                ['\/', ...array_fill(0, count($tokens), '([\w\s]+)')],
+                $routeUri
+            ) . '$/';
+
+        $result = preg_match($matchingRegex, $requestUri, $matches);
+        if ($result === 0) {
+            return [];
+        }
+        $matches = array_slice($matches, 1);
+
+        return array_combine(array_map(fn($token) => trim($token, '{}'), $tokens), $matches);
+    }
+
     private function autowire(RouteConfig $route): array
     {
         $reflectionMethod = new ReflectionMethod($route->getController(), $route->getMethod());
2023-08-09 18:17:34 +02:00
app Make route debug more readable 2023-08-09 00:59:07 +02:00
public Properly implement the router with config and interfaces 2023-08-07 17:51:53 +02:00
src Start with controller parameter injection 2023-08-09 18:17:34 +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 Update README 2023-08-09 00:55:11 +02:00

Simple PHP micro framework

Current features:

  • Routing with method dependency injection
  • Controller configuration with annotations
  • Dependency injection application configuration