-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.php
289 lines (277 loc) · 6.83 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
namespace MODULEWork;
/*===================================================
*
*
*
* Name: ROUTEWork
* Version: 1.0
* License: Apache 2.0
* Author: Christian Gärtner
* Author URL: http://christiangaertner.github.io
* Project URL: https://github.com/ChristianGaertner/MODULEWork
* Description: This is basic RESTful router for PHP.
*
*
*
===================================================*/
/**
* Router
* @author ChristianGaertner <christiangaertner.film@googlemail.com>
*/
class Route
{
/**
* The current uri seperated as array
* @var array
*/
protected static $uri;
/**
* The current HTTP request method
* @var string
*/
public static $rest;
/**
* The current scriptname, used for filtering the uri
* @var string
*/
protected static $scriptname;
/**
* Will be true after a route was found and serverd
* @var boolean
*/
public static $served;
/**
* Initializes the routing process and setups essential variables
*/
public static function tar()
{
static::$served = false;
static::$uri = explode('/', preg_replace('{/$}', '', $_SERVER['REQUEST_URI']));
static::$scriptname = explode('/', $_SERVER['SCRIPT_NAME']);
static::$rest = $_SERVER['REQUEST_METHOD'];
for ($i=0; $i < sizeof(static::$scriptname); $i++) {
if (isset(static::$uri[$i]) && isset(static::$scriptname[$i])) {
if (static::$uri[$i] == static::$scriptname[$i]) {
unset(static::$uri[$i]);
}
}
}
static::$uri = array_values(static::$uri);
if (empty(static::$uri[0])) {
static::$uri[0] = '/';
}
}
/**
* Retrieve the current URI
* @param boolean $array TRUE will return the uri as array (seperated by '/')
* @return array OR string The current URI
*/
public function getURI($array = false)
{
if ($array) {
return static::$uri;
} else {
return implode('/', static::$uri);
}
}
/**
* Will process the request on GET only
* @param string $uri_r
* @param closure OR string $callback
*/
public function get($uri_r, $callback)
{
if (static::$rest == 'GET') {
self::process($uri_r, $callback);
}
}
/**
* Will process the request on POST only
* @param string $uri_r
* @param closure OR string $callback
*/
public function post($uri_r, $callback)
{
if (static::$rest == 'POST') {
self::process($uri_r, $callback);
}
}
/**
* Will process the request on PUT only
* @param string $uri_r
* @param closure OR string $callback
*/
public function put($uri_r, $callback)
{
if (static::$rest == 'PUT') {
self::process($uri_r, $callback);
}
}
/**
* Will process the request on DELETE only
* @param string $uri_r
* @param closure OR string $callback
*/
public function delete($uri_r, $callback)
{
if (static::$rest == 'DELETE') {
self::process($uri_r, $callback);
}
}
/**
* Fallback method if the route was not found
* @param closure OR string $callback
*/
public function _404($callback)
{
if (static::$served) {
return;
}
if (is_callable($callback)) {
header('HTTP/1.0 404 Not Found');
echo $callback(implode('/', static::$uri));
} else {
$split = explode('#', $callback);
$controller = $split[0];
$method = $split[1];
self::controller($controller, $method, implode('/', static::$uri), true);
}
}
/**
* Register a controller to responde to a request
* URL / controller / method / parameter
* <code>example.com/home/about</code>
* Will call the about() method of a class called home
* If only one uri parameter was send the index() method will be called
* @param string $name
* @param boolean $default
*/
public function register($name, $default = false)
{
if (!is_string($name)) {
throw new \Exception("Controller name has to be a string", 1);
}
if ($name == static::$uri[0] || $default) {
if ($default && static::$uri[0] !== '/') {
return;
}
if (isset(static::$uri[1])) {
$method = static::$uri[1];
} else {
$method = 'index';
}
if (isset(static::$uri[2])) {
$param = static::$uri[2];
} else {
$param = null;
}
self::controller($name, $method, $param, true);
}
}
/**
* This will register the class to responde to the '/' route as well
* @param string $name
*/
public function setDefault($name)
{
self::register($name, true);
}
/**
* Process the get/post/put/delete methods
* @param string $uri_r
* @param closure OR string $callback
*/
protected function process($uri_r, $callback)
{
if (self::match($uri_r) && !static::$served) {
if (is_callable($callback)) {
echo $callback(self::getParameter($uri_r));
static::$served = true;
} else {
$split = explode('#', $callback);
$controller = $split[0];
$method = $split[1];
self::controller($controller, $method, $uri_r);
}
}
}
/**
* Call a controller
* <strong>This will require the CONTROLLERWork component</strong>
* @param string $controller_n The name of the class
* @param string $method The name of the method to call
* @param string $uri_r The current URI OR the parameter which should be passed to the function
* @param boolean $param TRUE will let $uri_r act as parameter
* @return [type]
*/
protected function controller($controller_n, $method, $uri_r, $param = false)
{
$controller['name'] = __NAMESPACE__ . '\\' . $controller_n;
if (class_exists($controller['name'])) {
$controller['object'] = Controller::get($controller['name']);
if ($controller['object']->rest) {
$method = strtolower(static::$rest) . '_' . $method;
}
if (method_exists($controller['object'], $method)) {
if ($param) {
$controller['object']->$method($uri_r);
} else {
$controller['object']->$method(self::getParameter($uri_r));
}
static::$served = true;
}
}
}
/**
* Check if the whished uri matches the current
* @param string $uri_r The URI to check
* @return boolean
*/
protected function match($uri_r)
{
if ($uri_r !== '/') {
$uri_r = explode('/', $uri_r);
} else {
$uri_r = array('/');
}
$uri_r = array_values($uri_r);
if (count($uri_r) !== count(static::$uri)) {
return false;
}
for ($i=0; $i < count($uri_r); $i++) {
if ($uri_r[$i] == static::$uri[$i]) {
$match = true;
} elseif ($uri_r[$i] == '(:any)') {
$match = true;
} elseif (is_numeric(static::$uri[$i]) && $uri_r[$i] == '(:num)') {
$match = true;
} else {
return false;
}
}
return $match;
}
/**
* Return the 'parameter' of the uri
* @param string $uri_r The URI template
* @return string the parameter
*/
protected function getParameter($uri_r)
{
if ($uri_r !== '/') {
$uri_r = explode('/', $uri_r);
} else {
return null;
}
$uri_r = array_values($uri_r);
for ($i=0; $i < count($uri_r); $i++) {
if ($uri_r[$i] == '(:any)') {
return static::$uri[$i];
} elseif($uri_r[$i] == '(:num)' && is_numeric(static::$uri[$i])) {
return static::$uri[$i];
}
}
return null;
}
}