1
0

downgrade to kirby v3

This commit is contained in:
Philip Wagner
2024-09-01 10:47:15 +02:00
parent a4b2aece7b
commit af86acb7a1
1085 changed files with 54743 additions and 65042 deletions

View File

@@ -18,27 +18,35 @@ class Router
{
/**
* Hook that is called after each route
*
* @var \Closure
*/
protected Closure|null $afterEach;
protected $afterEach;
/**
* Hook that is called before each route
*
* @var \Closure
*/
protected Closure|null $beforeEach;
protected $beforeEach;
/**
* Store for the current route,
* if one can be found
*
* @var \Kirby\Http\Route|null
*/
protected Route|null $route = null;
protected $route;
/**
* All registered routes, sorted by
* their request method. This makes
* it faster to find the right route
* later.
*
* @var array
*/
protected array $routes = [
protected $routes = [
'GET' => [],
'HEAD' => [],
'POST' => [],
@@ -54,6 +62,7 @@ class Router
* 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 = [])
@@ -95,12 +104,14 @@ class Router
* 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|null $path = null,
string $method = 'GET',
Closure|null $callback = null
) {
public function call(string $path = null, string $method = 'GET', Closure $callback = null)
{
$path ??= '';
$ignore = [];
$result = null;
@@ -109,7 +120,7 @@ class Router
while ($loop === true) {
$route = $this->find($path, $method, $ignore);
if ($this->beforeEach instanceof Closure) {
if (is_a($this->beforeEach, 'Closure') === true) {
($this->beforeEach)($route, $path, $method);
}
@@ -117,18 +128,15 @@ class Router
if ($callback) {
$result = $callback($route);
} else {
$result = $route->action()->call(
$route,
...$route->arguments()
);
$result = $route->action()->call($route, ...$route->arguments());
}
$loop = false;
} catch (Exceptions\NextRouteException) {
} catch (Exceptions\NextRouteException $e) {
$ignore[] = $route;
}
if ($this->afterEach instanceof Closure) {
if (is_a($this->afterEach, 'Closure') === true) {
$final = $loop === false;
$result = ($this->afterEach)($route, $path, $method, $result, $final);
}
@@ -141,13 +149,15 @@ class Router
* 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|null $path = null,
string $method = 'GET',
array $routes = [],
Closure|null $callback = null
) {
public static function execute(?string $path = null, string $method = 'GET', array $routes = [], ?Closure $callback = null)
{
return (new static($routes))->call($path, $method, $callback);
}
@@ -156,12 +166,14 @@ class Router
* 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|null $ignore = null
): Route {
public function find(string $path, string $method, array $ignore = null)
{
if (isset($this->routes[$method]) === false) {
throw new InvalidArgumentException('Invalid routing method: ' . $method, 400);
}
@@ -173,10 +185,7 @@ class Router
$arguments = $route->parse($route->pattern(), $path);
if ($arguments !== false) {
if (
empty($ignore) === true ||
in_array($route, $ignore) === false
) {
if (empty($ignore) === true || in_array($route, $ignore) === false) {
return $this->route = $route;
}
}
@@ -190,8 +199,10 @@ class Router
* 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(): Route|null
public function route()
{
return $this->route;
}