-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTinyRouter.php
More file actions
201 lines (162 loc) · 5.33 KB
/
TinyRouter.php
File metadata and controls
201 lines (162 loc) · 5.33 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
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
<?php
namespace TinyLara\TinyRouter;
/**
* @method static TinyRouter get(string $route, Callable $callback)
* @method static TinyRouter post(string $route, Callable $callback)
* @method static TinyRouter put(string $route, Callable $callback)
* @method static TinyRouter delete(string $route, Callable $callback)
* @method static TinyRouter options(string $route, Callable $callback)
* @method static TinyRouter head(string $route, Callable $callback)
*/
class TinyRouter {
public static $routes = array();
public static $methods = array();
public static $callbacks = array();
public static $patterns = array(
':any' => '[^/]+',
':num' => '[0-9]+',
':all' => '.*'
);
public static $error_callback;
/**
* add filter for your routes
*/
public static function filter($filter, $result) {
if ($filter()) {
$result();
}
}
/**
* Defines a route w/ callback and method
*/
public static function __callstatic($method, $params)
{
$uri = $params[0];
$callback = $params[1];
if ( $method == 'any' ) {
self::pushToArray($uri, 'get', $callback);
self::pushToArray($uri, 'post', $callback);
} else {
self::pushToArray($uri, $method, $callback);
}
}
/**
* Push route items to class arrays
*
*/
public static function pushToArray($uri, $method, $callback)
{
array_push(self::$routes, $uri);
array_push(self::$methods, strtoupper($method));
array_push(self::$callbacks, $callback);
}
/**
* Defines callback if route is not found
*/
public static function error($callback)
{
self::$error_callback = $callback;
}
/**
* Runs the callback for the given request
*
* $after: Processor After. It will process the value returned by Controller.
* Example: View@process
*
*/
public static function dispatch($after=null)
{
$uri = self::detect_uri();
$method = $_SERVER['REQUEST_METHOD'];
$searches = array_keys(static::$patterns);
$replaces = array_values(static::$patterns);
$found_route = false;
// check if route is defined without regex
if (in_array($uri, self::$routes)) {
$route_pos = array_keys(self::$routes, $uri);
foreach ($route_pos as $route) {
if (self::$methods[$route] == $method) {
$found_route = true;
//if route is not an object
if(!is_object(self::$callbacks[$route])){
//grab all parts based on a / separator
$parts = explode('/',self::$callbacks[$route]);
//collect the last index of the array
$last = end($parts);
//grab the controller name and method call
$segments = explode('@',$last);
//instanitate controller
$controller = new $segments[0]();
//call method
$return = $controller->$segments[1]();
if ($after) {
$after_segments = explode('@', $after);
$after_segments[0]::$after_segments[1]($return);
}
} else {
//call closure
call_user_func(self::$callbacks[$route]);
}
}
}
} else {
// check if defined with regex
$pos = 0;
foreach (self::$routes as $route) {
if (strpos($route, ':') !== false) {
$route = str_replace($searches, $replaces, $route);
}
if (preg_match('#^' . $route . '$#', $uri, $matched)) {
if (self::$methods[$pos] == $method) {
$found_route = true;
array_shift($matched); //remove $matched[0] as [1] is the first parameter.
if(!is_object(self::$callbacks[$pos])){
//grab all parts based on a / separator
$parts = explode('/',self::$callbacks[$pos]);
//collect the last index of the array
$last = end($parts);
//grab the controller name and method call
$segments = explode('@',$last);
//instanitate controller
$controller = new $segments[0]();
//call method and pass any extra parameters to the method
$return = $controller->$segments[1](implode(",", $matched));
if ($after) {
$after_segments = explode('@', $after);
$after_segments[0]::$after_segments[1]($return);
}
} else {
call_user_func_array(self::$callbacks[$pos], $matched);
}
}
}
$pos++;
}
}
// run the error callback if the route was not found
if ($found_route == false) {
if (!self::$error_callback) {
self::$error_callback = function() {
header($_SERVER['SERVER_PROTOCOL']." 404 Not Found");
echo '404';
};
}
call_user_func(self::$error_callback);
}
}
// detect true URI, inspired by CodeIgniter 2
private static function detect_uri()
{
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) {
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
} elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) {
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
if ($uri == '/' || empty($uri)) {
return '/';
}
$uri = parse_url($uri, PHP_URL_PATH);
return str_replace(array('//', '../'), '/', trim($uri, '/'));
}
}