Added some small notes.

This commit is contained in:
tracer 2022-10-23 12:11:57 +02:00
parent f571d7548a
commit e9fd8e153d
1 changed files with 11 additions and 1 deletions

View File

@ -6,6 +6,11 @@ namespace App\Service;
use App\Entity\Route; use App\Entity\Route;
use Closure; use Closure;
/*
* A small router implementation for the address book demo.
* Currently it doesn't handle GET requests, as not needed.
* But if I reuse the code in my bind Api I'll enable GET as well.
*/
class Router class Router
{ {
private array $routes; private array $routes;
@ -31,12 +36,16 @@ class Router
$this->routes[] = $route; $this->routes[] = $route;
} }
/*
* Check is there is a known route and executed the callback.
* Currently no 404 handling.
*/
public function handleRouting(): void public function handleRouting(): void
{ {
$requestUri = $_SERVER['REQUEST_URI']; $requestUri = $_SERVER['REQUEST_URI'];
foreach ($this->routes as $route) { foreach ($this->routes as $route) {
if (preg_match(pattern: $route->getRegex(), subject: $requestUri, matches: $matches)) { if (preg_match(pattern: $route->getRegex(), subject: $requestUri, matches: $matches)) {
$parameters = []; $parameters = [];
foreach ($route->getParameters() as $id => $parameter) { foreach ($route->getParameters() as $id => $parameter) {
$parameters[$parameter] = $matches[$id +1]; $parameters[$parameter] = $matches[$id +1];
@ -48,6 +57,7 @@ class Router
return; return;
} }
} }
// Throw a 404 result later …
die("Invalid route: $requestUri"); die("Invalid route: $requestUri");
} }
} }