-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.php
executable file
·173 lines (153 loc) · 4.54 KB
/
router.php
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Extreemly Simple Router
* @author Halil Qerimi
* @version 0.1
*
*/
namespace ESRouter;
/**
* Route class that holds predefined routes
* @access public
*/
class Route {
/**
*
* @var string
* @access public
*/
public $route;
/**
* @var string
* @access public
*/
public $regExpression;
/**
* @var string
* @access public
*/
public $method;
/**
* @var \Closure
* @access public
*/
public $handler;
/**
*
*/
public function __construct(string $route, string $regExpression, string $method, \Closure $handler) {
$this->route = $route;
$this->regExpression = $regExpression;
//Set method name to uppercase for comparison
$this->method = strtoupper($method);
$this->handler = $handler;
}
/**
*
*/
public function __toString(): string {
return (string) $this->method.$this->route;
}
}
/**
* @internal Class usess private property {@routes} to store all predefined routes
* @internal Class usess private methods {@prepareRegex(string $route)} to generate RegEx expression that can be uses to match Ure with predefined routes
* @access public
*/
class Router {
/**
* @var \Closure
* @access private
*/
private $e404;
/**
* @var array Array of Route Objects
* @access private
*/
private $routes = array();
/**
* Appends anonther route to the list of defined routes
* @param string $route Define route
* @param string $method Request method (POST, GET, PUT, DELETE ...)
* @param string $handler Function that will handle the request when route matches, it will call this function with only one parameter of array type
* @return void
* @access public
*/
public function addRoute(string $route, string $method, \Closure $handler): void {
$regExpression = "/" . $this->prepareRegex($route) . "/";
$route = new Route($route, $regExpression, $method, $handler);
foreach($this->routes as $rt) {
if((string)$rt == (string)$route) {
throw new \Exception('Route "'.$route->route.'" already exists for method "'.$route->method.'"');
}
}
array_push($this->routes, $route);
}
/**
* Appends anonther route to the list of defined routes
* @param string $handler E404 handler
* @return void
* @access public
*/
public function setE404(\Closure $handler) {
$this->e404 = $handler;
}
/**
* Searches for a match of current url with predefined routes
* @return void
* @access public
*/
public function run() {
$e404 = true;
$method = $_SERVER["REQUEST_METHOD"];
$url = $_SERVER["REQUEST_URI"];
if (substr_count($url, "?") > 0) {
$url = explode('?', $url)[0];
}
$url = rtrim($url, "/");
foreach ($this->routes as $route) {
if ($route->method == $method) {
preg_match($route->regExpression, $url, $matches);
if (count($matches) > 0) {
$e404 = false;
call_user_func_array($route->handler, array($matches));
//In case match is found, break foreach loop not to continue futher
//break;
}
}
}
if ($e404) {
if($this->e404 == null) {
http_response_code(404);
echo "404";
return;
}
call_user_func($this->e404);
}
}
/**
* Prepares RegEx expression from given route
* @param string $route Define route
* @return string RegEx expression
* @access private
*/
private function prepareRegex(string $route): string {
$regex = "^";
$routeParts = array_filter(explode('/', $route));
foreach ($routeParts as $part) {
if ($part[0] === "{" && $part[strlen($part) - 1] === "}") {
$expression = substr($part, 1, -1);
if (substr_count($expression, ":") == 1) {
$parts = explode(':', $expression);
$regex = $regex . "\\/(?P<" . $parts[0] . ">" . $parts[1] . "+)";
} else {
$regex = $regex . "\\/(?P<" . $expression . ">[^\\/]+)";
}
} else {
$regex = $regex . "\\/" . $part;
}
}
$regex = $regex . "$";
return $regex;
}
}