Skip to content

Commit 804950c

Browse files
committed
Refactor: Routing mechanism improved
Indentation replaced with 4 spaces
1 parent cbebd4c commit 804950c

1 file changed

Lines changed: 87 additions & 70 deletions

File tree

src/Router.php

Lines changed: 87 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,81 +4,98 @@
44

55
class Router {
66

7-
private static $prefix = '';
8-
9-
public static function set_router_prefix($prefix)
10-
{
11-
self::$prefix = $prefix;
12-
}
13-
14-
public static function get($route, $callback)
15-
{
16-
if ($_SERVER['REQUEST_METHOD'] == 'GET') self::route($route, $callback);
17-
}
18-
19-
public static function post($route, $callback)
20-
{
21-
if ($_SERVER['REQUEST_METHOD'] == 'POST') self::route($route, $callback);
22-
}
23-
24-
public static function put($route, $callback)
25-
{
26-
if ($_SERVER['REQUEST_METHOD'] == 'PUT') self::route($route, $callback);
27-
}
28-
29-
public static function patch($route, $callback)
30-
{
31-
if ($_SERVER['REQUEST_METHOD'] == 'PATCH') self::route($route, $callback);
32-
}
33-
34-
public static function delete($route, $callback)
35-
{
36-
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') self::route($route, $callback);
37-
}
38-
39-
public static function any($route, $callback)
40-
{
41-
self::route($route, $callback);
42-
}
43-
44-
private static function route($route, $callback)
45-
{
46-
$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
47-
$request_url = rtrim($request_url, '/');
48-
$request_url = strtok($request_url, '?');
49-
$route_parts = explode('/', $route);
50-
$request_url_parts = explode('/', $request_url);
51-
$request_url_parts = array_diff($request_url_parts, explode('/', self::$prefix));
52-
array_shift($route_parts);
53-
array_shift($request_url_parts);
54-
55-
if ($route_parts[0] == '' && count($request_url_parts) == 0) {
56-
57-
// Callback function
58-
if (is_callable($callback)) {
59-
call_user_func_array($callback, array());
60-
exit();
61-
}
7+
private static $prefix = '';
8+
9+
public static function set_router_prefix($prefix)
10+
{
11+
self::$prefix = rtrim($prefix, '/ ');
6212
}
6313

64-
if (count($route_parts) != count($request_url_parts)) return;
65-
$parameters = array();
14+
public static function get($route, $callback)
15+
{
16+
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
17+
self::route($route, $callback);
18+
}
19+
}
6620

67-
for($__i__ = 0; $__i__ < count($route_parts); $__i__++) {
68-
$route_part = $route_parts[$__i__];
21+
public static function post($route, $callback)
22+
{
23+
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
24+
self::route($route, $callback);
25+
}
26+
}
27+
28+
public static function put($route, $callback)
29+
{
30+
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
31+
self::route($route, $callback);
32+
}
33+
}
34+
35+
public static function patch($route, $callback)
36+
{
37+
if ($_SERVER['REQUEST_METHOD'] == 'PATCH') {
38+
self::route($route, $callback);
39+
}
40+
}
41+
42+
public static function delete($route, $callback)
43+
{
44+
if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
45+
self::route($route, $callback);
46+
}
47+
}
6948

70-
if (preg_match("/^[$]/", $route_part)) {
71-
$route_part = ltrim($route_part, '$');
72-
array_push($parameters, $request_url_parts[$__i__]);
73-
} else if ($route_parts[$__i__] != $request_url_parts[$__i__]) {
74-
return;
75-
}
49+
public static function any($route, $callback)
50+
{
51+
self::route($route, $callback);
7652
}
7753

78-
// Callback function
79-
if (is_callable($callback)) {
80-
call_user_func_array($callback, $parameters);
81-
exit();
54+
private static function route($route, $callback)
55+
{
56+
$request_url = filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL);
57+
$request_url = rtrim($request_url, '/ ');
58+
59+
// If request URL doesn't contain router prefix
60+
if (
61+
! preg_match('/^'.preg_quote(self::$prefix, '/').'/', $request_url)
62+
) return;
63+
64+
$request_url = str_replace(self::$prefix, '', $request_url);
65+
$request_url_parts = explode('/', $request_url);
66+
array_shift($request_url_parts);
67+
68+
$route_url = rtrim($route, '/ ');
69+
$route_url_parts = explode('/', $route_url);
70+
array_shift($route_url_parts);
71+
72+
$route_params = array();
73+
74+
foreach ($route_url_parts as $index => $route_part) {
75+
76+
// Route part is a parameter
77+
if (preg_match('/^\$/', $route_part)) {
78+
$route_param = $request_url_parts[$index];
79+
80+
// Router parameter is optional
81+
if (preg_match('/\?$/', $route_part)) {
82+
83+
array_push($route_params, $route_param ?: null);
84+
} else {
85+
86+
if (empty($route_param)) return;
87+
array_push($route_params, $route_param);
88+
}
89+
} else {
90+
91+
// Request URL part doesn't match with route part
92+
if ($route_part !== $request_url_parts[$index]) return;
93+
}
94+
}
95+
96+
if (is_callable($callback)) {
97+
call_user_func_array($callback, $route_params);
98+
exit();
99+
}
82100
}
83-
}
84101
}

0 commit comments

Comments
 (0)