Update SegmentRouter.php

This commit is contained in:
Valithor Obsidion 2024-12-25 10:22:33 -05:00
parent 393022b342
commit 4f5f2599f5

View File

@ -15,13 +15,16 @@ class SegmentRouter implements RouterInterface
*/ */
public function add(string $method, string $route, callable $handler): RouterInterface public function add(string $method, string $route, callable $handler): RouterInterface
{ {
// Expand the route into segments and make dynamic segments into a common placeholder
$segments = array_map(function($segment) {
return str_starts_with($segment, ':') ? ':x' : $segment;
}, explode('/', trim($route, '/')));
// Recursively build the node tree // Recursively build the node tree
$this->addNode($this->routes, $segments, $method, $handler); $this->addNode(
$this->routes,
array_map( // Expand the route into segments and make dynamic segments into a common placeholder
fn($segment) => str_starts_with($segment, ':') ? ':x' : $segment,
explode('/', trim($route, '/'))
),
$method,
$handler
);
// Add the handler to the last node // Add the handler to the last node
$node[$method] = $handler; $node[$method] = $handler;