166 lines
4.8 KiB
PHP
166 lines
4.8 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace Sharkk\Router;
|
|
|
|
trait SegmentRouterTrait
|
|
{
|
|
/**
|
|
* Add a route to the route tree. The route must be a URI path, and contain dynamic segments
|
|
* using a colon prefix. (:id, :slug, etc)
|
|
*
|
|
* Example:
|
|
* `$r->add('GET', '/posts/:id', function($id) { echo "Viewing post $id"; });`
|
|
*/
|
|
public static function add(array &$routes, string $method, string $route, callable $handler): void
|
|
{
|
|
// Recursively build the node tree
|
|
self::addNode(
|
|
$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
|
|
);
|
|
}
|
|
|
|
private static function addNode(array &$node, array $segments, string $method, callable $handler): void
|
|
{
|
|
// Base case: if no more segments, add the handler
|
|
if (empty($segments)) {
|
|
$node[$method] = $handler;
|
|
return;
|
|
}
|
|
|
|
// Get the current segment
|
|
$segment = array_shift($segments);
|
|
|
|
// Skip empty segments
|
|
if ($segment === '') {
|
|
self::addNode($node, $segments, $method, $handler);
|
|
return;
|
|
}
|
|
|
|
// Ensure the segment exists in the node
|
|
if (!isset($node[$segment])) {
|
|
$node[$segment] = [];
|
|
}
|
|
|
|
// Recur for the next segment
|
|
self::addNode($node[$segment], $segments, $method, $handler);
|
|
}
|
|
|
|
/**
|
|
* Perform a lookup in the route tree for a given method and URI. Returns an array with a result code,
|
|
* a handler if found, and any dynamic parameters. Codes are 200 for success, 404 for not found, and
|
|
* 405 for method not allowed.
|
|
*
|
|
* @return array ['code', 'handler', 'params']
|
|
*/
|
|
public static function lookup(array &$routes, string $method, string $uri): array
|
|
{
|
|
// node is a reference to our current location in the node tree
|
|
$node = $routes;
|
|
|
|
// if the URI is just a slash, we can return the handler for the root node
|
|
if ($uri === '/') {
|
|
return isset($node[$method])
|
|
? ['code' => 200, 'handler' => $node[$method], 'params' => null]
|
|
: ['code' => 405, 'handler' => null, 'params' => null];
|
|
}
|
|
|
|
// params will hold any dynamic segments we find
|
|
$params = array_reduce(
|
|
explode('/', trim($uri, '/')),
|
|
function ($carry, $segment) use (&$node) {
|
|
if (isset($node[$segment])) {
|
|
$node = $node[$segment];
|
|
} elseif (isset($node[':x'])) {
|
|
$carry[] = $segment;
|
|
$node = $node[':x'];
|
|
} else {
|
|
throw new \Exception('404');
|
|
}
|
|
return $carry;
|
|
},
|
|
[]
|
|
);
|
|
|
|
// if we found a handler for the method, return it and any params. if not, return a 405
|
|
return isset($node[$method])
|
|
? [
|
|
'code' => 200,
|
|
'handler' => $node[$method],
|
|
'params' => $params ?? []]
|
|
: [
|
|
'code' => 405,
|
|
'handler' => null,
|
|
'params' => []
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Clear all routes from the router.
|
|
*/
|
|
public static function clear(array &$routes): void
|
|
{
|
|
$routes = [];
|
|
}
|
|
|
|
/**
|
|
* Dump the route tree as an array.
|
|
*/
|
|
/*public static function dump(array &$routes): array
|
|
{
|
|
return $routes;
|
|
}*/
|
|
|
|
/**
|
|
* Register a GET route.
|
|
*/
|
|
public static function get(array &$routes, string $route, callable $handler): void
|
|
{
|
|
self::add($routes, 'GET', $route, $handler);
|
|
}
|
|
|
|
/**
|
|
* Register a POST route.
|
|
*/
|
|
public static function post(array &$routes, string $route, callable $handler): void
|
|
{
|
|
self::add($routes, 'POST', $route, $handler);
|
|
}
|
|
|
|
/**
|
|
* Register a PUT route.
|
|
*/
|
|
public static function put(array &$routes, string $route, callable $handler): void
|
|
{
|
|
self::add($routes, 'PUT', $route, $handler);
|
|
}
|
|
|
|
/**
|
|
* Register a PATCH route.
|
|
*/
|
|
public static function patch(array &$routes, string $route, callable $handler): void
|
|
{
|
|
self::add($routes, 'PATCH', $route, $handler);
|
|
}
|
|
|
|
/**
|
|
* Register a DELETE route.
|
|
*/
|
|
public static function delete(array &$routes, string $route, callable $handler): void
|
|
{
|
|
self::add($routes, 'DELETE', $route, $handler);
|
|
}
|
|
|
|
/**
|
|
* Register a HEAD route.
|
|
*/
|
|
public static function head(array &$routes, string $route, callable $handler): void
|
|
{
|
|
self::add($routes, 'HEAD', $route, $handler);
|
|
}
|
|
} |