-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
151 lines (134 loc) · 4.97 KB
/
Copy pathRouter.php
File metadata and controls
151 lines (134 loc) · 4.97 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace core\routing;
require_once __DIR__ . "/methods.php";
//require_once __DIR__ . "/../methods.php";
//require "src/app/core/exceptions.php";
use controllers\StubController;
use core\exceptions\ControllerMissing;
use core\exceptions\IncludeParentRouter;
use core\exceptions\MethodNotAllowed;
use core\exceptions\RouterYetIncluded;
use core\protocols\Controller;
/*
* Роутер
* Основной класс маршрутизации запросов к контроллерам и их методам
* Может включать в себя другие роутеры
*/
class Router {
protected array $routes; // Массив эндпоинтов и соответсвующих методов
protected array $routers; // Дочерние роутеры
private array $parents; // Родительские роутеры
protected array $exceptionHandlers;
public function __construct(public ?Controller $controller = null, public string $prefix = "", public bool $login_required = false, public mixed $rights = false, public array $allowed_methods = HTTP_METHODS, public string $name = "") {
$this->routes = [];
$this->routers = [];
$this->parents = [];
$this->exceptionHandlers = [];
}
public function getParents(): array
{
return $this->parents;
}
public function register(string $method, string $path, callable | string $callback, ?Controller $controller = null, bool $login_required = false, mixed $rights = array()): void
{
if (in_array($method, $this->allowed_methods)) {
$this->routes[$method][$this->prefix . $path] = [
"controller" => $controller ?: $this->controller,
"callback" => $callback,
"login_required" => $login_required ?: $this->login_required,
"rights" => $rights ?: $this->login_required
];
if (!$this->routes[$method][$this->prefix . $path]["controller"]) {
throw new ControllerMissing();
}
} else {
throw new MethodNotAllowed("Method $method not allowed. Allowed methods: $this->allowed_methods");
}
}
public function get(string $path, callable | string $callback, ?Controller $controller = null, bool $login_required = false, mixed $rights = array()): void
{
$this->register("get", $path, $callback, $controller, $login_required, $rights);
}
public function post(string $path, callable | string $callback, ?Controller $controller = null, bool $login_required = false, mixed $rights = array()): void
{
$this->register("post", $path, $callback, $controller, $login_required, $rights);
}
protected function isMatch(string $pattern, string $uri): bool {
$pattern = preg_replace('#\{([a-zA-Z0-9_]+)}#', '([a-zA-Z0-9_]+)', $pattern);
$pattern = '#^' . $pattern . '$#';
return preg_match($pattern, $uri);
}
public function resolve(Request $request)
{
$path = $request->getPath();
$method = $request->getMethod();
$callback = false;
if (!empty($this->routes[$method])) {
foreach ($this->routes[$method] as $route => $action) {
if ($this->isMatch($route, $path)) {
$callback = $action;
$request->extractParams($route, $path);
break;
}
}
}
if ($callback === false) {
if (!$this->routers) {
return false;
}
foreach ($this->routers as $child) {
$callback = $child->resolve($request);
if ($callback) {
return $callback;
}
}
}
return $callback;
}
public function in(self $router): bool
{
if (!$this->routers) {
return false;
}
if (in_array($router, $this->routers)) {
return true;
} else {
foreach ($this->routers as $child) {
if ($child->in($router)) {
return true;
}
}
}
return false;
}
public function is_parent(self $router): bool
{
if (!$this->parents) {
return false;
}
if (in_array($router, $this->parents)) {
return true;
} else {
foreach ($this->parents as $parent) {
if ($parent->is_parent($router)) {
return true;
}
}
}
return false;
}
public function include_router(self $router, string $prefix = ""): void
{
if (!$router->prefix) {
$router->prefix = $prefix;
}
if ($this->in($router)) {
throw new RouterYetIncluded();
}
if ($this->is_parent($router)) {
throw new IncludeParentRouter();
}
$this->routers[] = $router;
$router->parents[] = $this;
}
}