-
Notifications
You must be signed in to change notification settings - Fork 1
/
Router.php
170 lines (146 loc) · 3.82 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
<?php
class Router{
/**
* @var string[] $server_uri - List of URI's of the server
*/
private $server_uri = [];
/**
* @var string $server_method - Method of the server
*/
private $server_method;
/**
* @var callable $callback - Callback to run after URI match
*/
private $callback;
/**
* @var boolean $matched - Toggle on route matched
*/
private $matched = false;
/**
* @var array $params - List of dynamic URI parameters
*/
private $params = [];
/**
* @var string $trim - Rejex to trim URL
*/
private $trim = '/\^$';
/**
* __construct - Fetch infromation from server
* @return object
*/
function __construct(){
$uri = trim($_SERVER['REQUEST_URI'], $this->trim);
$this->server_method = strtolower($_SERVER['REQUEST_METHOD']);
$this->server_uri = explode('/', $uri);
}
/**
* get - Adds a URI for matching with GET method
*
* @param string $uri
* @param callable $callback
* @return void
*/
public function get($uri, $callback){
$this->match('get', $uri, $callback);
}
/**
* post - Adds a URI for matching with POST method
*
* @param string $uri
* @param callable $callback
* @return void
*/
public function post($uri, $callback){
$this->match('post', $uri, $callback);
}
/**
* put - Adds a URI for matching with PUT method
*
* @param string $uri
* @param callable $callback
* @return void
*/
public function put($uri, $callback){
$this->match('post', $uri, $callback);
}
/**
* patch - Adds a URI for matching with PATCH method
*
* @param string $uri
* @param callable $callback
* @return void
*/
public function patch($uri, $callback){
$this->match('patch', $uri, $callback);
}
/**
* delete - Adds a URI for matching with DELETE method
*
* @param string $uri
* @param callable $callback
* @return void
*/
public function delete($uri, $callback){
$this->match('delete', $uri, $callback);
}
/**
* add - Adds a URI for matching
*
* @param string $method
* @param string $uri
* @param callable $callback
* @return void
*/
public function add($method, $uri, $callback){
$this->match(strtolower($method), $uri, $callback);
}
/**
* match - Match URIs with server
*
* @param string $method
* @param string $uri
* @param callable $callback
* @return void
*/
private function match($method, $uri, $callback){
if($this->matched){
return;
}
$uri = trim($uri, $this->trim);
$current_uri = explode('/', $uri);
$uri_length = count($current_uri);
if($method != $this->server_method){
return;
}
if($uri_length != count($this->server_uri)){
return;
}
$matched = true;
for($i = 0; $i < $uri_length; $i++){
if($current_uri[$i] == $this->server_uri[$i]){
continue;
}
if(isset($current_uri[$i][0]) && $current_uri[$i][0] == ':'){
$this->params[substr($current_uri[$i], 1)] = $this->server_uri[$i];
continue;
}
$matched = false;
break;
}
if($matched){
$this->callback = $callback;
$this->matched = true;
}
}
/**
* listen - Run the callback function of matched route
* @return void
*/
public function listen(){
if(!$this->matched){
header("HTTP/1.1 404 Not Found");
return;
}
call_user_func($this->callback, $this->params);
}
}