Update Route.php
This commit is contained in:
parent
ad655ef2be
commit
d0a026f5d9
36
Route.php
36
Route.php
@ -2,12 +2,31 @@
|
|||||||
|
|
||||||
namespace WillySoft;
|
namespace WillySoft;
|
||||||
|
|
||||||
use Exception;
|
use \Exception;
|
||||||
|
|
||||||
abstract class Route
|
abstract class Route
|
||||||
{
|
{
|
||||||
static $prefix = '';
|
static string $prefix = '';
|
||||||
static $groups = [[]];
|
static array $groups = [
|
||||||
|
[]
|
||||||
|
];
|
||||||
|
|
||||||
|
private static function getRequestPath(): string
|
||||||
|
{
|
||||||
|
static $request_url_path;
|
||||||
|
|
||||||
|
if (isset($request_url_path)) {
|
||||||
|
return $request_url_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
$request_url_path = substr_replace(
|
||||||
|
urldecode(strtok($_SERVER['REQUEST_URI'], '?')), '', 0, strlen($_SERVER['SCRIPT_NAME'])
|
||||||
|
);
|
||||||
|
if ($request_url_path === '')
|
||||||
|
$request_url_path = '/';
|
||||||
|
|
||||||
|
return $request_url_path;
|
||||||
|
}
|
||||||
|
|
||||||
static function match(string $methods, string $url_path, callable $callback)
|
static function match(string $methods, string $url_path, callable $callback)
|
||||||
{
|
{
|
||||||
@ -16,9 +35,8 @@ abstract class Route
|
|||||||
array_map(strtoupper(...), explode('|', $methods))
|
array_map(strtoupper(...), explode('|', $methods))
|
||||||
)) return;
|
)) return;
|
||||||
|
|
||||||
$request_url_path_parts = explode('/', urldecode(
|
$request_url_path_parts = explode('/', self::getRequestPath());
|
||||||
strtok($_SERVER['REQUEST_URI'], '?')
|
|
||||||
));
|
|
||||||
$url_path_parts = explode('/', self::$prefix . $url_path);
|
$url_path_parts = explode('/', self::$prefix . $url_path);
|
||||||
$callback_args = [];
|
$callback_args = [];
|
||||||
for ($i = 1; $i < count($url_path_parts); $i++) {
|
for ($i = 1; $i < count($url_path_parts); $i++) {
|
||||||
@ -85,10 +103,10 @@ abstract class Route
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static function group(string $prefix, callable $callback)
|
static function group(string $prefix = '', callable $callback)
|
||||||
{
|
{
|
||||||
if (!str_starts_with(
|
if (!str_starts_with(
|
||||||
$_SERVER['REQUEST_URI'],
|
self::getRequestPath(),
|
||||||
self::$prefix . $prefix
|
self::$prefix . $prefix
|
||||||
)) return;
|
)) return;
|
||||||
self::$prefix = self::$prefix . $prefix;
|
self::$prefix = self::$prefix . $prefix;
|
||||||
@ -97,4 +115,4 @@ abstract class Route
|
|||||||
array_pop(self::$groups);
|
array_pop(self::$groups);
|
||||||
self::$prefix = rtrim(self::$prefix, $prefix);
|
self::$prefix = rtrim(self::$prefix, $prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user