zappy-base/src/Routing/RouteRepository.php
2024-05-19 15:45:12 +02:00

113 lines
3.5 KiB
PHP

<?php
namespace Musoka\Zap\Routing;
class RouteRepository
{
/**
* @var array The array with registered routes.
*/
protected array $routes;
/**
* RoutingRepository constructor.
*/
public function __construct()
{
$this->routes = [];
}
/**
* Create a group of localized routes.
*
* Group a number of routes as localized routes. The callback function will
* get an instance of LocalizedRepository passed to it, which will expose
* extra functions for localization support.
*
* @param callable $callback
* @return void
*/
/*public function localized(callable $callback): void
{
$localizedGroup = new LocalizedRepository();
call_user_func($callback, $localizedGroup);
//Add localized routes to list
$this->routes = array_merge_recursive($this->routes, $localizedGroup->collectRoutes());
}*/
/**
* Register a new route.
*
* @param string $pattern The uri pattern to match
* @param array|string $methods One or more HTTP methods to match
* @param string|array|callable $callback A callback to run if this route is called
* @param array $middlewares List of extra middlewares to run for this route
* @return Route
*/
public function register(string $pattern, array|string $methods, string|array|callable $callback, array $middlewares = []): Route
{
$pattern = '/' . trim($pattern, '/');
$route = new Route($pattern, $methods, $callback, $middlewares);
foreach($route->getMethods() as $method) {
$this->routes[$method][] = $route;
}
return $route;
}
public function get(string $pattern, string|array|callable $callback, array $middlewares = []): Route
{
return $this->register($pattern, 'GET', $callback, $middlewares);
}
public function post(string $pattern, string|array|callable $callback, array $middlewares = []): Route
{
return $this->register($pattern, 'POST', $callback, $middlewares);
}
public function put(string $pattern, string|array|callable $callback, array $middlewares = []): Route
{
return $this->register($pattern, 'PUT', $callback, $middlewares);
}
public function delete(string $pattern, string|array|callable $callback, array $middlewares = []): Route
{
return $this->register($pattern, 'DELETE', $callback, $middlewares);
}
public function options(string $pattern, string|array|callable $callback, array $middlewares = []): Route
{
return $this->register($pattern, 'OPTIONS', $callback, $middlewares);
}
public function patch(string $pattern, string|array|callable $callback, array $middlewares = []): Route
{
return $this->register($pattern, 'PATCH', $callback, $middlewares);
}
public function head(string $pattern, string|array|callable $callback, array $middlewares = []): Route
{
return $this->register($pattern, 'HEAD', $callback, $middlewares);
}
/**
* Get the list of registered routes.
*
* @return array
*/
public function collectRoutes(): array
{
//Loop through groups and register their routes
/*foreach($this->groups as $group) {
$group(); //Invoke group to run its callback
$groupRoutes = $group->getRoutes();
//Merge into main routes list.
$this->routes = array_merge_recursive($groupRoutes, $this->routes);
}*/
return $this->routes;
}
}