-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
141 lines (130 loc) · 3.87 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
<?php
namespace alvin\phpmvc;
use alvin\phpmvc\exception\RouteNotFoundException;
/**
* Router class
*/
class Router {
/**
* Object for holding routes and corresponding callback and middlewares
* @var object
*/
private array $routeMap = [];
/**
* Current route
* @var string
*/
public string $currentRoute;
/**
* Current method
* @var string
*/
public string $currentMethod;
/**
* Set callback and middlewares for get method.
*
* @param string $route The route to set
* @param string[]|string $resolvable callback or view to handle
* @return void
*/
public function get(string $route, $resolvable) {
$regexRoute = $this->convertToRegex($route);
$this->currentRoute = $regexRoute;
$this->currentMethod = 'get';
$this->routeMap['get'][$regexRoute]['resolvable'] = $resolvable;
$this->routeMap['get'][$regexRoute]['middlewares'] = new Middleware();
}
/**
* Set callback and middlewares for post method.
*
* @param string $route The route to set
* @param string[]|string $resolvable callback or view to handle
* @return void
*/
public function post(string $route, $resolvable) {
$regexRoute = $this->convertToRegex($route);
$this->currentRoute = $regexRoute;
$this->currentMethod = 'post';
$this->routeMap['post'][$regexRoute]['resolvable'] = $resolvable;
$this->routeMap['post'][$regexRoute]['middlewares'] = new Middleware();
}
/**
* Set middleware for a route.
*
* @param object $middleware instance of alvin\phpmvc\Middleware
* @return void
*/
public function setRouteMiddleware($middleware) {
$this->routeMap[$this->currentMethod][$this->currentRoute]['middlewares']->add($middleware);
}
/**
* Convert a route to regex for storing in routemap.
*
* @param string $route The route to convert
* @return string regex
*/
public function convertToRegex(string $route) {
$route = str_replace('/', '\/', $route);
$route = preg_replace('/{[a-zA-Z0-9]+}/', '([a-zA-Z0-9]+)', $route);
return $route;
}
/**
* Returns middlewares, callback and its argument.
*
* @param string $httpMethod Current http method
* @param string $route Current route
* @return array array containing middlewarecontent, if any, callback function and its arguments
*/
public function getResults(string $httpMethod, string $route) {
$middlewareContent = '';
$resolvable = [];
$args = [Application::$app->request, Application::$app->response];
$params = [];
foreach ($this->routeMap[$httpMethod] as $regexRoute => $valueArray) {
if (preg_match("/^$regexRoute$/", $route)) {
$middlewareContent = $valueArray['middlewares']->resolve();
$resolvable = $valueArray['resolvable'];
preg_match_all("/^$regexRoute$/", $route, $params, PREG_SET_ORDER);
break;
}
}
if (sizeof($params) == 1) {
array_shift($params[0]);
return [$middlewareContent, $resolvable, [...$args, ...$params[0]]];
}
return [$middlewareContent, $resolvable, [...$args]];
}
/**
* Runs middlewares,callback corresponding to current route.
* @return void
*/
public function resolve() {
try {
$resolvable = [];
$args = [];
$middlewareContent = '';
$httpMethod = Application::$app->request->getMethod();
$route = Application::$app->request->getRoute();
list($middlewareContent, $resolvable, $args) = $this->getResults($httpMethod, $route);
echo $middlewareContent;
if (!$resolvable) {
throw new RouteNotFoundException();
exit();
}
if (is_array($resolvable)) {
$controller = new $resolvable[0];
echo call_user_func_array(array($controller, $resolvable[1]), $args);
exit();
} else if (is_callable($resolvable)) {
echo $resolvable(...$args);
} else if (is_string($resolvable)) {
echo Application::$app->view->renderViewOnly($resolvable);
exit();
}
} catch (RouteNotFoundException $e) {
Application::$app->response->statusCode(404);
echo $e->getMessage();
exit();
}
}
}