-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute.php
More file actions
140 lines (104 loc) · 4.13 KB
/
Route.php
File metadata and controls
140 lines (104 loc) · 4.13 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
<?php
class Route{
private static $routes = Array();
private static $pathNotFound = null;
private static $methodNotAllowed = null;
private static $url = null;
public static function add($expression, $call, $method = 'get'){
$parameter = null;
$fun = self::getParameter($expression);
if ($fun){
$parameter = $fun['parameter'];
$expression = $fun['expression'];
}
array_push(self::$routes,Array(
'expression' => $expression,
'call' => $call,
'method' => $method,
'parameter' => $parameter,
));
}
public static function pathNotFound($function){
self::$pathNotFound = $function;
}
public static function methodNotAllowed($function){
self::$methodNotAllowed = $function;
}
public static function run($basepath = '/'){
// Parse current url
$parsed_url = parse_url(self::$url ? self::$url : $_SERVER['REQUEST_URI']);//Parse Uri
if(isset($parsed_url['path'])){
$path = $parsed_url['path'];
}else{
$path = '/';
}
// Get current request method
$method = $_SERVER['REQUEST_METHOD'];
// print_r($parsed_url) ;
$path_match_found = false;
$route_match_found = false;
foreach(self::$routes as $route){
// If the method matches check the path
// Add basepath to matching string
if($basepath!=''&&$basepath!='/'){
$route['expression'] = '('.$basepath.')'.$route['expression'];
}
// echo $route['expression'];
// echo "<br>";
// Add 'find string start' automatically
$route['expression'] = '^'.$route['expression'];
// Add 'find string end' automatically
$route['expression'] = $route['expression'].'$';
// echo $route['expression'].'<br/>';
// Check path match
if(preg_match('#'.$route['expression'].'#',$path,$matches)){
$path_match_found = true;
// Check method match
if(strtolower($method) == strtolower($route['method'])){
array_shift($matches);// Always remove first element. This contains the whole string
if($basepath!=''&&$basepath!='/'){
array_shift($matches);// Remove basepath
}
require_once $route['call']['path'];
(new $route['call']['class']())
->{$route['call']['fun']}($route['parameter']);
// call_user_func_array($route['function'], $matches);
$route_match_found = true;
// Do not check other routes
break;
}
}
}
// No matching route was found
if(!$route_match_found){
// But a matching path exists
if($path_match_found){
header("HTTP/1.0 405 Method Not Allowed");
if(self::$methodNotAllowed){
call_user_func_array(self::$methodNotAllowed, Array($path,$method));
}
}else{
header("HTTP/1.0 404 Not Found");
if(self::$pathNotFound){
call_user_func_array(self::$pathNotFound, Array($path));
}
}
}
}
private static function getParameter($expression){
if(preg_match('/{(.*?)}/', $expression, $output_array)){
$url_without_par = preg_replace('/{(.*?)}/', '', $expression);
$parameter = str_replace( $url_without_par, '', $_SERVER['REQUEST_URI']) ;
$cleaned_url = substr_replace($_SERVER['REQUEST_URI'],"",
similar_text($url_without_par, $_SERVER['REQUEST_URI']));
if($url_without_par=== $cleaned_url){
self::$url = $cleaned_url;
return [
"parameter"=>$parameter,
"expression"=>$url_without_par
];
}
}
return false;
}
}