-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
90 lines (74 loc) · 2.28 KB
/
Router.php
File metadata and controls
90 lines (74 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
require 'StatusCode.php';
class Router
{
private array $routes_controllers = [
'GET' => [],
'POST' => [],
];
public function map_get(string $url_path, string $controller): void
{
$this->routes_controllers['GET'][$url_path] = $controller;
}
public function map_post(string $url_path, string $controller): void
{
$this->routes_controllers['POST'][$url_path] = $controller;
}
public function route(string $uri, string $method): void
{
$url_path = parse_url($uri, component: PHP_URL_PATH);
$controllers = $this->routes_controllers[$method];
foreach ($controllers as $route => $controller) {
$route_params = matched_params($url_path, $route);
if ($route_params !== false) {
require "controllers/$controller";
exit;
}
}
abort(StatusCode::NOT_FOUND_404);
}
}
function matched_params(string $url_path, string $route): false|array
{
$params = [];
$route_len = mb_strlen($route);
$url_len = mb_strlen($url_path);
$i = 0;
$j = 0;
while ($i < $route_len) {
if ($j === $url_len) {
if ($i != $route_len - 1 || $route[$i] != '/') {
return false;
}
break;
}
if ($route[$i] != $url_path[$j]) {
if ($route[$i] != ':') {
return false;
}
$param_end = mb_strpos($route, '/', $i);
$arg_end = mb_strpos($url_path, '/', $j);
if ($arg_end === false) {
if ($param_end !== false && $param_end != $route_len - 1) {
return false;
}
$arg_end = $url_len;
}
if ($param_end === false) {
$param_end = $route_len;
}
$name = mb_substr($route, start: $i + 1, length: $param_end - ($i + 1));
$value = mb_substr($url_path, start: $j, length: $arg_end - $j);
$params[$name] = $value;
$i = $param_end;
$j = $arg_end;
} else {
$i++;
$j++;
}
}
if ($j != $url_len && ($j != $url_len - 1 || $url_path[$j] != '/')) {
return false;
}
return $params;
}