-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewrouter.php
166 lines (140 loc) · 4.41 KB
/
newrouter.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
<?PHP
/*
https://github.com/ferrriii/NewRouter
*/
abstract class NewRouterCallBackType
{
const lambda = 0;
const router = 1;
const classmethod = 2;
}
class NewRouterRoute {
public $method;
public $route;
private $callBack;
private $callBackType;
private $_pattern = array();
public static function fromRouteStr($route) {
if (empty($route)) return new NewRouterRoute;
$route = trim($route);
$route = str_replace("//","/", $route);
$routeIndex = strpos($route, "/");
if ($routeIndex === false) {
$method = $route;
$route = '';
} else {
$method = $routeIndex > 0 ? substr($route,0, $routeIndex-1) : '';
$route = substr($route, $routeIndex);
}
$method = strtoupper(trim($method));
$r = new NewRouterRoute;
$r->method = $method;
$r->route = $route;
return $r;
}
public function setCallback($callBack) {
$this->callBack = $callBack;
if ($callBack instanceof Closure) {
$this->callBackType = NewRouterCallBackType::lambda;
}else if ($callBack instanceof NewRouter) {
$this->callBackType = NewRouterCallBackType::router;
} else {
$this->callBackType = NewRouterCallBackType::classmethod;
}
}
public function run(&$request, $prefixPattern, $method, $path) {
if ($this->callBackType === NewRouterCallBackType::lambda) {
$func = $this->callBack;
return $func($request);
} else if ($this->callBackType === NewRouterCallBackType::router) {
return $this->callBack->dispatch($method, $path, $prefixPattern);
} else if ($this->callBackType === NewRouterCallBackType::classmethod) {
return call_user_func($this->callBack, $request);
}
}
public function pattern($prefixPattern = '^') {
if (array_key_exists($prefixPattern, $this->_pattern)) {
return $this->_pattern[$prefixPattern];
}
$this->_pattern[$prefixPattern] = $this->routePattern($this->route, $prefixPattern);
return $this->_pattern[$prefixPattern];
}
private function routePattern($route = '*', $prefixRoute = '^') {
//translate route to regex
if (empty($route)) {
$route = '*';
}
$openEnd = false;
if (substr($route,-1) === '*') {
//remove trailing *: it's not needed at the end of regex
$route = substr($route,0, strlen($route)-1);
$openEnd = true;
}
$route = str_replace("/","\/", $route);
$route = str_replace("*",".*", $route);
$route = str_replace("+",".+", $route);
$route = preg_replace('/:(.*?):/', '(?<${1}>[^\/]*?)', $route);
$route = $prefixRoute . $route;
// normalize // to /
$route = str_replace('\/\/','\/', $route);
return $route . ($openEnd ? '' : '$');
}
}
class NewRouter {
private $routes = array();
private function routeFromArgument($firstArgument) {
$type = gettype($firstArgument);
if ($type === 'string') return $firstArgument;
if ($type === 'object') return null;
if ($type === 'NULL') return '';
return false;
}
public function route() {
$route = $this->routeFromArgument(func_get_arg(0));
$funcIndex = 1;
if ($route === null) {
$funcIndex = 0; // route is not defined, all arguments are middleware functions
}
$totalArgs = func_num_args();
for ($i = $funcIndex; $i<$totalArgs; ++$i) {
$r = NewRouterRoute::fromRouteStr($route);
$r->setCallback(func_get_arg($i));
$this->routes[] = $r;
}
}
public function dispatch($method = null, $path = null, $prefixPattern = '^') {
if ($method === null) {
$method = $_SERVER['REQUEST_METHOD'];
}
if ($path === null) {
$path = $_SERVER['REQUEST_URI'];
}
$path = strtok($path,'?');
return $this->execute($method, $path, $prefixPattern);
}
private function execute($method, $path, $prefixPattern = '^') {
$routeFound = null; // no route found
$req = new stdClass();
$req->params = array();
foreach ($this->routes as $route) {
if (!empty($route->method) && $method !== $route->method) {
continue; // method is not same as route method
}
$pattern = $route->pattern($prefixPattern);
if (!preg_match('/' . $pattern . '/i', $path, $matches)) {
continue;
}
$routeFound = true; // route found and routing should be continued
$req->params = array_merge($req->params, $matches);
$req->url = $matches[0];
$req->path = $path;
// TODO: uncomment below line
// $req->route = $route;
$res = $route->run($req, $pattern, $method, $path);
if ($res === NULL || $res === false) {
return false; // route found and further routing should be stopped
}
}
return $routeFound;
}
}