Update Kirby and dependencies
This commit is contained in:
parent
503b339974
commit
399fa20902
439 changed files with 66915 additions and 64442 deletions
|
@ -16,158 +16,194 @@ use Kirby\Toolkit\A;
|
|||
*/
|
||||
class Router
|
||||
{
|
||||
public static $beforeEach;
|
||||
public static $afterEach;
|
||||
/**
|
||||
* Hook that is called after each route
|
||||
*
|
||||
* @var \Closure
|
||||
*/
|
||||
protected $afterEach;
|
||||
|
||||
/**
|
||||
* Store for the current route,
|
||||
* if one can be found
|
||||
*
|
||||
* @var Route|null
|
||||
*/
|
||||
protected $route;
|
||||
/**
|
||||
* Hook that is called before each route
|
||||
*
|
||||
* @var \Closure
|
||||
*/
|
||||
protected $beforeEach;
|
||||
|
||||
/**
|
||||
* All registered routes, sorted by
|
||||
* their request method. This makes
|
||||
* it faster to find the right route
|
||||
* later.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $routes = [
|
||||
'GET' => [],
|
||||
'HEAD' => [],
|
||||
'POST' => [],
|
||||
'PUT' => [],
|
||||
'DELETE' => [],
|
||||
'CONNECT' => [],
|
||||
'OPTIONS' => [],
|
||||
'TRACE' => [],
|
||||
'PATCH' => [],
|
||||
];
|
||||
/**
|
||||
* Store for the current route,
|
||||
* if one can be found
|
||||
*
|
||||
* @var \Kirby\Http\Route|null
|
||||
*/
|
||||
protected $route;
|
||||
|
||||
/**
|
||||
* Creates a new router object and
|
||||
* registers all the given routes
|
||||
*
|
||||
* @param array $routes
|
||||
*/
|
||||
public function __construct(array $routes = [])
|
||||
{
|
||||
foreach ($routes as $props) {
|
||||
if (isset($props['pattern'], $props['action']) === false) {
|
||||
throw new InvalidArgumentException('Invalid route parameters');
|
||||
}
|
||||
/**
|
||||
* All registered routes, sorted by
|
||||
* their request method. This makes
|
||||
* it faster to find the right route
|
||||
* later.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $routes = [
|
||||
'GET' => [],
|
||||
'HEAD' => [],
|
||||
'POST' => [],
|
||||
'PUT' => [],
|
||||
'DELETE' => [],
|
||||
'CONNECT' => [],
|
||||
'OPTIONS' => [],
|
||||
'TRACE' => [],
|
||||
'PATCH' => [],
|
||||
];
|
||||
|
||||
$patterns = A::wrap($props['pattern']);
|
||||
$methods = A::map(
|
||||
explode('|', strtoupper($props['method'] ?? 'GET')),
|
||||
'trim'
|
||||
);
|
||||
/**
|
||||
* Creates a new router object and
|
||||
* registers all the given routes
|
||||
*
|
||||
* @param array $routes
|
||||
* @param array<string, \Closure> $hooks Optional `beforeEach` and `afterEach` hooks
|
||||
*/
|
||||
public function __construct(array $routes = [], array $hooks = [])
|
||||
{
|
||||
$this->beforeEach = $hooks['beforeEach'] ?? null;
|
||||
$this->afterEach = $hooks['afterEach'] ?? null;
|
||||
|
||||
if ($methods === ['ALL']) {
|
||||
$methods = array_keys($this->routes);
|
||||
}
|
||||
foreach ($routes as $props) {
|
||||
if (isset($props['pattern'], $props['action']) === false) {
|
||||
throw new InvalidArgumentException('Invalid route parameters');
|
||||
}
|
||||
|
||||
foreach ($methods as $method) {
|
||||
foreach ($patterns as $pattern) {
|
||||
$this->routes[$method][] = new Route($pattern, $method, $props['action'], $props);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$patterns = A::wrap($props['pattern']);
|
||||
$methods = A::map(
|
||||
explode('|', strtoupper($props['method'] ?? 'GET')),
|
||||
'trim'
|
||||
);
|
||||
|
||||
/**
|
||||
* Calls the Router by path and method.
|
||||
* This will try to find a Route object
|
||||
* and then call the Route action with
|
||||
* the appropriate arguments and a Result
|
||||
* object.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $method
|
||||
* @param Closure|null $callback
|
||||
* @return mixed
|
||||
*/
|
||||
public function call(string $path = null, string $method = 'GET', Closure $callback = null)
|
||||
{
|
||||
$path ??= '';
|
||||
$ignore = [];
|
||||
$result = null;
|
||||
$loop = true;
|
||||
if ($methods === ['ALL']) {
|
||||
$methods = array_keys($this->routes);
|
||||
}
|
||||
|
||||
while ($loop === true) {
|
||||
$route = $this->find($path, $method, $ignore);
|
||||
foreach ($methods as $method) {
|
||||
foreach ($patterns as $pattern) {
|
||||
$this->routes[$method][] = new Route(
|
||||
$pattern,
|
||||
$method,
|
||||
$props['action'],
|
||||
$props
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (is_a(static::$beforeEach, 'Closure') === true) {
|
||||
(static::$beforeEach)($route, $path, $method);
|
||||
}
|
||||
/**
|
||||
* Calls the Router by path and method.
|
||||
* This will try to find a Route object
|
||||
* and then call the Route action with
|
||||
* the appropriate arguments and a Result
|
||||
* object.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $method
|
||||
* @param Closure|null $callback
|
||||
* @return mixed
|
||||
*/
|
||||
public function call(string $path = null, string $method = 'GET', Closure $callback = null)
|
||||
{
|
||||
$path ??= '';
|
||||
$ignore = [];
|
||||
$result = null;
|
||||
$loop = true;
|
||||
|
||||
try {
|
||||
if ($callback) {
|
||||
$result = $callback($route);
|
||||
} else {
|
||||
$result = $route->action()->call($route, ...$route->arguments());
|
||||
}
|
||||
while ($loop === true) {
|
||||
$route = $this->find($path, $method, $ignore);
|
||||
|
||||
$loop = false;
|
||||
} catch (Exceptions\NextRouteException $e) {
|
||||
$ignore[] = $route;
|
||||
}
|
||||
if (is_a($this->beforeEach, 'Closure') === true) {
|
||||
($this->beforeEach)($route, $path, $method);
|
||||
}
|
||||
|
||||
if (is_a(static::$afterEach, 'Closure') === true) {
|
||||
$final = $loop === false;
|
||||
$result = (static::$afterEach)($route, $path, $method, $result, $final);
|
||||
}
|
||||
}
|
||||
try {
|
||||
if ($callback) {
|
||||
$result = $callback($route);
|
||||
} else {
|
||||
$result = $route->action()->call($route, ...$route->arguments());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
$loop = false;
|
||||
} catch (Exceptions\NextRouteException $e) {
|
||||
$ignore[] = $route;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a Route object by path and method
|
||||
* The Route's arguments method is used to
|
||||
* find matches and return all the found
|
||||
* arguments in the path.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $method
|
||||
* @param array $ignore
|
||||
* @return \Kirby\Http\Route|null
|
||||
*/
|
||||
public function find(string $path, string $method, array $ignore = null)
|
||||
{
|
||||
if (isset($this->routes[$method]) === false) {
|
||||
throw new InvalidArgumentException('Invalid routing method: ' . $method, 400);
|
||||
}
|
||||
if (is_a($this->afterEach, 'Closure') === true) {
|
||||
$final = $loop === false;
|
||||
$result = ($this->afterEach)($route, $path, $method, $result, $final);
|
||||
}
|
||||
}
|
||||
|
||||
// remove leading and trailing slashes
|
||||
$path = trim($path, '/');
|
||||
return $result;
|
||||
}
|
||||
|
||||
foreach ($this->routes[$method] as $route) {
|
||||
$arguments = $route->parse($route->pattern(), $path);
|
||||
/**
|
||||
* Creates a micro-router and executes
|
||||
* the routing action immediately
|
||||
* @since 3.7.0
|
||||
*
|
||||
* @param string|null $path
|
||||
* @param string $method
|
||||
* @param array $routes
|
||||
* @param \Closure|null $callback
|
||||
* @return mixed
|
||||
*/
|
||||
public static function execute(?string $path = null, string $method = 'GET', array $routes = [], ?Closure $callback = null)
|
||||
{
|
||||
return (new static($routes))->call($path, $method, $callback);
|
||||
}
|
||||
|
||||
if ($arguments !== false) {
|
||||
if (empty($ignore) === true || in_array($route, $ignore) === false) {
|
||||
return $this->route = $route;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Finds a Route object by path and method
|
||||
* The Route's arguments method is used to
|
||||
* find matches and return all the found
|
||||
* arguments in the path.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $method
|
||||
* @param array $ignore
|
||||
* @return \Kirby\Http\Route|null
|
||||
*/
|
||||
public function find(string $path, string $method, array $ignore = null)
|
||||
{
|
||||
if (isset($this->routes[$method]) === false) {
|
||||
throw new InvalidArgumentException('Invalid routing method: ' . $method, 400);
|
||||
}
|
||||
|
||||
throw new Exception('No route found for path: "' . $path . '" and request method: "' . $method . '"', 404);
|
||||
}
|
||||
// remove leading and trailing slashes
|
||||
$path = trim($path, '/');
|
||||
|
||||
/**
|
||||
* Returns the current route.
|
||||
* This will only return something,
|
||||
* once Router::find() has been called
|
||||
* and only if a route was found.
|
||||
*
|
||||
* @return \Kirby\Http\Route|null
|
||||
*/
|
||||
public function route()
|
||||
{
|
||||
return $this->route;
|
||||
}
|
||||
foreach ($this->routes[$method] as $route) {
|
||||
$arguments = $route->parse($route->pattern(), $path);
|
||||
|
||||
if ($arguments !== false) {
|
||||
if (empty($ignore) === true || in_array($route, $ignore) === false) {
|
||||
return $this->route = $route;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception('No route found for path: "' . $path . '" and request method: "' . $method . '"', 404);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current route.
|
||||
* This will only return something,
|
||||
* once Router::find() has been called
|
||||
* and only if a route was found.
|
||||
*
|
||||
* @return \Kirby\Http\Route|null
|
||||
*/
|
||||
public function route()
|
||||
{
|
||||
return $this->route;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue