|
|
|
@ -13,7 +13,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
|
|
|
|
|
|
|
|
|
namespace Config;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use RuntimeException;
|
|
|
|
|
use Traversable;
|
|
|
|
|
|
|
|
|
|
class AltoRouter
|
|
|
|
|
{
|
|
|
|
@ -21,22 +23,22 @@ class AltoRouter
|
|
|
|
|
/**
|
|
|
|
|
* @var array Array of all routes (incl. named routes).
|
|
|
|
|
*/
|
|
|
|
|
protected $routes = [];
|
|
|
|
|
protected array $routes = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Array of all named routes.
|
|
|
|
|
*/
|
|
|
|
|
protected $namedRoutes = [];
|
|
|
|
|
protected array $namedRoutes = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string Can be used to ignore leading part of the Request URL (if main file lives in subdirectory of host)
|
|
|
|
|
*/
|
|
|
|
|
protected $basePath = '';
|
|
|
|
|
protected string $basePath = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array Array of default match types (regex helpers)
|
|
|
|
|
*/
|
|
|
|
|
protected $matchTypes = [
|
|
|
|
|
protected array $matchTypes = [
|
|
|
|
|
'i' => '[0-9]++',
|
|
|
|
|
'a' => '[0-9A-Za-z]++',
|
|
|
|
|
'h' => '[0-9A-Fa-f]++',
|
|
|
|
@ -53,7 +55,7 @@ class AltoRouter
|
|
|
|
|
* @param array $matchTypes
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(array $routes = [], $basePath = '', array $matchTypes = [])
|
|
|
|
|
public function __construct(array $routes = [], string $basePath = '', array $matchTypes = [])
|
|
|
|
|
{
|
|
|
|
|
$this->addRoutes($routes);
|
|
|
|
|
$this->setBasePath($basePath);
|
|
|
|
@ -65,7 +67,7 @@ class AltoRouter
|
|
|
|
|
* Useful if you want to process or display routes.
|
|
|
|
|
* @return array All routes.
|
|
|
|
|
*/
|
|
|
|
|
public function getRoutes()
|
|
|
|
|
public function getRoutes(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->routes;
|
|
|
|
|
}
|
|
|
|
@ -79,10 +81,10 @@ class AltoRouter
|
|
|
|
|
*
|
|
|
|
|
* @param array $routes
|
|
|
|
|
* @return void
|
|
|
|
|
* @author Koen Punt
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*@author Koen Punt
|
|
|
|
|
*/
|
|
|
|
|
public function addRoutes($routes)
|
|
|
|
|
public function addRoutes(mixed $routes): void
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($routes) && !$routes instanceof Traversable) {
|
|
|
|
|
throw new RuntimeException('Routes should be an array or an instance of Traversable');
|
|
|
|
@ -97,7 +99,7 @@ class AltoRouter
|
|
|
|
|
* Useful if you are running your application from a subdirectory.
|
|
|
|
|
* @param string $basePath
|
|
|
|
|
*/
|
|
|
|
|
public function setBasePath($basePath)
|
|
|
|
|
public function setBasePath(string $basePath): void
|
|
|
|
|
{
|
|
|
|
|
$this->basePath = $basePath;
|
|
|
|
|
}
|
|
|
|
@ -107,7 +109,7 @@ class AltoRouter
|
|
|
|
|
*
|
|
|
|
|
* @param array $matchTypes The key is the name and the value is the regex.
|
|
|
|
|
*/
|
|
|
|
|
public function addMatchTypes(array $matchTypes)
|
|
|
|
|
public function addMatchTypes(array $matchTypes): void
|
|
|
|
|
{
|
|
|
|
|
$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
|
|
|
|
|
}
|
|
|
|
@ -118,22 +120,21 @@ class AltoRouter
|
|
|
|
|
* @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE)
|
|
|
|
|
* @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]
|
|
|
|
|
* @param mixed $target The target where this route should point to. Can be anything.
|
|
|
|
|
* @param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
|
|
|
|
|
* @param string|null $name Optional name of this route. Supply if you want to reverse route this url in your application.
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function map($method, $route, $target, $name = null)
|
|
|
|
|
public function map(string $method, string $route, mixed $target, string $name = null): void
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$this->routes[] = [$method, $route, $target, $name];
|
|
|
|
|
|
|
|
|
|
if ($name) {
|
|
|
|
|
if (isset($this->namedRoutes[$name])) {
|
|
|
|
|
throw new RuntimeException("Can not redeclare route '{$name}'");
|
|
|
|
|
throw new RuntimeException("Can not redeclare route ".$name);
|
|
|
|
|
}
|
|
|
|
|
$this->namedRoutes[$name] = $route;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -142,16 +143,16 @@ class AltoRouter
|
|
|
|
|
* Generate the URL for a named route. Replace regexes with supplied parameters
|
|
|
|
|
*
|
|
|
|
|
* @param string $routeName The name of the route.
|
|
|
|
|
* @param array @params Associative array of parameters to replace placeholders with.
|
|
|
|
|
* @param array $params @params Associative array of parameters to replace placeholders with.
|
|
|
|
|
* @return string The URL of the route with named parameters in place.
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function generate($routeName, array $params = [])
|
|
|
|
|
public function generate(string $routeName, array $params = []): string
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Check if named route exists
|
|
|
|
|
if (!isset($this->namedRoutes[$routeName])) {
|
|
|
|
|
throw new RuntimeException("Route '{$routeName}' does not exist.");
|
|
|
|
|
throw new RuntimeException("Route ".$routeName." does not exist.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replace named parameters
|
|
|
|
@ -186,18 +187,18 @@ class AltoRouter
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Match a given Request Url against stored routes
|
|
|
|
|
* @param string $requestUrl
|
|
|
|
|
* @param string $requestMethod
|
|
|
|
|
* @param string|null $requestUrl
|
|
|
|
|
* @param string|null $requestMethod
|
|
|
|
|
* @return array|boolean Array with route information on success, false on failure (no match).
|
|
|
|
|
*/
|
|
|
|
|
public function match($requestUrl = null, $requestMethod = null)
|
|
|
|
|
public function match(string $requestUrl = null, string $requestMethod = null): bool|array
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$params = [];
|
|
|
|
|
|
|
|
|
|
// set Request Url if it isn't passed as parameter
|
|
|
|
|
if ($requestUrl === null) {
|
|
|
|
|
$requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
|
|
|
|
|
$requestUrl = $_SERVER['REQUEST_URI'] ?? '/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// strip base path from request url
|
|
|
|
@ -212,7 +213,7 @@ class AltoRouter
|
|
|
|
|
|
|
|
|
|
// set Request Method if it isn't passed as a parameter
|
|
|
|
|
if ($requestMethod === null) {
|
|
|
|
|
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
|
|
|
|
|
$requestMethod = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($this->routes as $handler) {
|
|
|
|
@ -271,7 +272,7 @@ class AltoRouter
|
|
|
|
|
* @param $route
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function compileRoute($route)
|
|
|
|
|
protected function compileRoute($route): string
|
|
|
|
|
{
|
|
|
|
|
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
|
|
|
|
$matchTypes = $this->matchTypes;
|
|
|
|
|