Upgrade to 3.0.3

This commit is contained in:
Bastian Allgeier
2019-03-05 10:55:03 +01:00
parent 8e3d86a590
commit 418db4b09b
40 changed files with 704 additions and 144 deletions

View File

@@ -0,0 +1,7 @@
<?php
namespace Kirby\Http\Exceptions;
class NextRouteException extends \Exception
{
}

View File

@@ -152,6 +152,17 @@ class Route
return $this->attributes['name'] ?? null;
}
/**
* Throws a specific exception to tell
* the router to jump to the next route
*
* @return void
*/
public function next()
{
throw new Exceptions\NextRouteException('next');
}
/**
* Getter for the pattern
*

View File

@@ -14,6 +14,8 @@ use InvalidArgumentException;
*/
class Router
{
public static $beforeEach;
public static $afterEach;
/**
* Store for the current route,
@@ -84,10 +86,30 @@ class Router
*/
public function call(string $path = '', string $method = 'GET')
{
return $this
->find($path, $method)
->action()
->call($this->route, ...$this->route->arguments());
$ignore = [];
$result = null;
$loop = true;
while ($loop === true) {
$route = $this->find($path, $method, $ignore);
if (is_a(static::$beforeEach, 'Closure') === true) {
(static::$beforeEach)($route, $path, $method);
}
try {
$result = $route->action()->call($route, ...$route->arguments());
$loop = false;
} catch (Exceptions\NextRouteException $e) {
$ignore[] = $route;
}
if (is_a(static::$afterEach, 'Closure') === true) {
(static::$afterEach)($route, $path, $method, $result);
}
}
return $result;
}
/**
@@ -98,9 +120,10 @@ class Router
*
* @param string $path
* @param string $method
* @param array $ignore
* @return Route|null
*/
public function find(string $path, string $method)
public function find(string $path, string $method, array $ignore = null)
{
if (isset($this->routes[$method]) === false) {
throw new InvalidArgumentException('Invalid routing method: ' . $method, 400);
@@ -113,7 +136,9 @@ class Router
$arguments = $route->parse($route->pattern(), $path);
if ($arguments !== false) {
return $this->route = $route;
if (empty($ignore) === true || in_array($route, $ignore) === false) {
return $this->route = $route;
}
}
}