This repository was archived by the owner on Dec 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
123 lines (100 loc) · 3.85 KB
/
Router.php
File metadata and controls
123 lines (100 loc) · 3.85 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
BluffingoCore
Copyright (C) 2025 Chaziz
BluffingoCore is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
BluffingoCore is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace BluffingoCore;
class Router
{
private array $routes = [];
private mixed $fallbackHandler = null;
public function add(string $path, mixed $handler, ?string $method = null): void
{
// automatically add BLUFF_PRIVATE_PATH for file paths
if (
is_string($handler)
&& !str_contains($handler, BLUFF_PRIVATE_PATH)
&& !str_starts_with($handler, '/')
) {
$handler = BLUFF_PRIVATE_PATH . '/pages/' . ltrim($handler, '/');
}
// if no method specified, just define for GET AND POST (we don't use any of the other methods, yet)
$methods = $method ? [strtoupper($method)] : ['GET', 'POST'];
foreach ($methods as $httpMethod) {
$this->routes[$httpMethod][] = [
'pattern' => $this->compilePattern($path),
'original' => $path,
'handler' => $handler
];
}
}
public function redirect(string $from, string $to, int $statusCode = 302): void
{
$this->add($from, fn() => CoreUtilities::redirect($to, $statusCode));
}
public function setFallback(mixed $handler): void
{
$this->fallbackHandler = $handler;
}
public function dispatch(?string $requestUri = null, ?string $requestMethod = null): void
{
$requestUri ??= $_SERVER['REQUEST_URI'] ?? '/';
$requestMethod = strtoupper($requestMethod ?? $_SERVER['REQUEST_METHOD'] ?? 'GET');
$uri = $this->normalizeUri($requestUri);
foreach ($this->routes[$requestMethod] ?? [] as $route) {
if (preg_match($route['pattern'], $uri, $matches)) {
$params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
$this->executeHandler($route['handler'], $params);
return;
}
}
$this->executeFallback();
}
private function compilePattern(string $path): string
{
$pattern = preg_quote($path, '#');
$pattern = preg_replace('#\\\\\{(\w+)\\\\}#', '(?<$1>[^/]+)', $pattern);
return '#^' . $pattern . '$#';
}
private function normalizeUri(string $uri): string
{
$uri = parse_url($uri, PHP_URL_PATH) ?? '/';
return rtrim($uri, '/') ?: '/';
}
private function executeHandler(mixed $handler, array $params = []): void
{
match (true) {
is_callable($handler) => $handler($params),
is_string($handler) && file_exists($handler) => $this->includeFile($handler, $params),
default => $this->executeFallback()
};
}
private function includeFile(string $file, array $params): void
{
extract($params, EXTR_SKIP);
require $file;
}
private function executeFallback(): void
{
match (true) {
is_callable($this->fallbackHandler) => ($this->fallbackHandler)(),
is_string($this->fallbackHandler) && file_exists($this->fallbackHandler) => require $this->fallbackHandler,
default => $this->default404()
};
}
private function default404(): void
{
http_response_code(404);
die("404");
}
}