-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
198 lines (160 loc) · 5.49 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
<?php
namespace Sutija\Router;
use Closure;
use Exception;
const GET = 'GET';
const PATCH = 'PATCH';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
const OPTIONS = 'OPTIONS';
const COPY = 'COPY';
/**
* @author Marko Šutija <markosutija@gmail.com>
* @version 1.0
*
* Class Router
*
*/
class Router
{
protected const CALLBACK_STATIC = 'CALLBACK_STATIC';
protected const CALLBACK_CLASS = 'CALLBACK_CLASS';
protected const CALLBACK_CLOSURE = 'CALLBACK_CLOSURE';
protected static $instance;
protected $routes;
/**
* @var RouterData $routeData
*/
protected $routeData;
public function __construct()
{
$this->routes = new Routes();
$this->routeData = new RouterData();
$this->routeData->setRoute($_SERVER['PATH_INFO']);
$this->routeData->setRequestMethod($_SERVER['REQUEST_METHOD']);
if (isset($_SERVER['QUERY_STRING'])) {
$this->routeData->setRequestQueryParams($_SERVER['QUERY_STRING']);
}
}
public static function getInstance(): Router
{
if (!static::$instance) {
static::$instance = new self();
}
return static::$instance;
}
public function addRoute(Route $route): void
{
$this->routes->add($route);
}
public function resolve(string $route = null, string $method = null, string $query = null): string
{
$this->fillRouteData($route, $method, $query);
$requestMethod = ucfirst(strtolower($this->routeData->getRequestMethod()));
$requestMethodMethod = "resolve$requestMethod";
$matchedRoute = $this->routes->find($this->routeData->getRoute());
if (!$matchedRoute) {
http_response_code(404);
return false;
}
if (!empty($matchedRoute->getAllowedMethods())
&& !in_array($this->routeData->getRequestMethod(), $matchedRoute->getAllowedMethods())) {
http_response_code(405);
return false;
}
$this->resolveData($matchedRoute->getRoute());
$this->resolveQueryParams();
switch ($this->getCallBackType($matchedRoute->getCallback())) {
case self::CALLBACK_CLOSURE:
$matchedRoute->getCallback()();
break;
case self::CALLBACK_CLASS:
$this->doClassCallback($matchedRoute);
break;
case self::CALLBACK_STATIC:
$this->doStaticCallback($matchedRoute);
break;
};
return $this->getRouteData()->getRoute();
}
protected function fillRouteData(?string $route, ?string $method, ?string $query): void
{
if ($route) {
$this->routeData->setRoute($route);
}
if ($query) {
$this->routeData->setRequestQueryParams($query);
}
if ($method) {
$this->routeData->setRequestMethod($method);
}
}
protected function resolveData(string $routeTemplate)
{
$route = explode('/', $this->routeData->getRoute());
$template = explode('/', $routeTemplate);
foreach ($template as $key => $value) {
if (preg_match('/[:]/', $value)) {
$k = str_replace(':', '', $value);
if (isset($route[$key])) {
$this->routeData->setData($k, $route[$key]);
}
}
}
}
protected function resolveQueryParams()
{
$queryParams = [];
$requestQuery = $this->routeData->getRequestQueryParams();
if ($requestQuery) {
foreach (explode('&', $requestQuery) as $item) {
$itemParts = explode('=', $item);
$queryParams[$itemParts[0]] = $itemParts[1];
}
}
$this->routeData->setQueryParams($queryParams);
}
protected function getCallBackType($callback): ?string
{
$type = null;
if (is_string($callback)) {
$type = self::CALLBACK_STATIC;
}
if (is_object($callback) && !($callback instanceof Closure)) {
$type = self::CALLBACK_CLASS;
}
if ($callback instanceof Closure) {
$type = self::CALLBACK_CLOSURE;
}
return $type;
}
protected function doClassCallback(Route $matchedRoute)
{
$requestMethodMethod = 'resolve' . ucfirst(strtolower($this->routeData->getRequestMethod()));
// check if we have resolvePost/resolveGet...
if (method_exists($matchedRoute->getCallback(), $requestMethodMethod)) {
$matchedRoute->getCallback()->$requestMethodMethod();
} // Call resolve
else {
$matchedRoute->getCallback()->resolve();
}
}
protected function doStaticCallback(Route $matchedRoute)
{
$requestMethodMethod = 'resolve' . ucfirst(strtolower($this->routeData->getRequestMethod()));
if (method_exists($matchedRoute->getCallback(), $requestMethodMethod)) {
$callableMethod = [$matchedRoute->getCallback(), $requestMethodMethod];
call_user_func_array($callableMethod, []);
} elseif (method_exists($matchedRoute->getCallback(), 'resolve')) {
$callableMethod = [$matchedRoute->getCallback(), 'resolve'];
call_user_func_array($callableMethod, []);
} else {
throw new Exception('Callback not found: ' . $matchedRoute->getCallback());
}
}
public function getRouteData(): RouterData
{
return $this->routeData;
}
}