Router/TrieRouter.php
2024-09-07 13:02:52 -05:00

72 lines
1.6 KiB
PHP

<?php
class TrieRouter
{
private array $GET = [];
private array $POST = [];
private array $PUT = [];
private array $DELETE = [];
// Add route to the trie
public function add(string $method, string $route, callable $handler)
{
$node = &$this->{strtoupper($method)};
$segments = explode('/', trim($route, '/'));
foreach ($segments as $segment) {
if (!isset($node[$segment])) {
$node[$segment] = ['_children' => [], '_handler' => null];
}
$node = &$node[$segment]['_children'];
}
$node['_handler'] = $handler;
}
// Find and handle the route
public function handleRequest(string $method, string $uri)
{
$node = &$this->{strtoupper($method)};
$segments = explode('/', trim($uri, '/'));
$params = [];
foreach ($segments as $segment) {
if (isset($node[$segment])) {
$node = &$node[$segment]['_children'];
} else {
// Handle dynamic parameters (e.g., :id)
$dynamicSegment = $this->matchDynamicSegment($node, $segment);
if ($dynamicSegment) {
$params[] = $segment;
$node = &$node[$dynamicSegment]['_children'];
} else {
return $this->notFound();
}
}
}
// Check if a handler exists for the current node
if (isset($node['_handler'])) {
return call_user_func_array($node['_handler'], $params);
}
return $this->notFound();
}
// Match dynamic route segments like ':id'
private function matchDynamicSegment(array $node, string $segment)
{
foreach ($node as $key => $value) {
if (strpos($key, ':') === 0) return $key;
}
return null;
}
// Default 404 handler
private function notFound()
{
echo "404 Not Found";
return false;
}
}