-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.php
More file actions
184 lines (170 loc) · 5.71 KB
/
Router.php
File metadata and controls
184 lines (170 loc) · 5.71 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
<?php
/**
* @author Swen Rühl
* @license MIT public license
*/
class Router {
private $routes = [];
private $beforeroutes = [];
private $error404 = null;
private $controllerpath = null;
/**
* Constructor
*
* @return void
*/
public function __construct($controllerpath) {
$this->routes = [];
$this->beforeroutes = [];
$this->add404(function() { });
$this->controllerpath = $controllerpath;
}
/**
* REturns base directory
*
* @return string basedir
*/
public static function getBaseDir() {
return substr($_SERVER['SCRIPT_NAME'], 0, strlen($_SERVER['SCRIPT_NAME']) - 9);
}
/**
* @param string $path The last part of the URL
* @return string The URL
*/
public static function buildURL($path) {
return self::getBaseDir() . $path;
}
/**
* Adds a route to the list
*
* Add a route and a callback function
*
* @param string $method The method like GET, POST, etc
* @param string $route Part of the URI
* @param callable|string $fn Callback function or 'Method@Controller'
* @return void
*/
public function add($method, $route, $fn) {
if (strlen($route) > 0 && $route[0] === '/') {
$route = substr($route, 1);
}
foreach (explode('|', $method) as $m) {
$this->routes[] = Array('method' => $m, 'fn' => $fn, 'route' => $route);
}
}
/**
* Adds a GET route to the list
*
* Add a route and a callback function
*
* @param string $route Part of the URI
* @param callable|string $fn Callback function or 'Method@Controller'
* @return void
*/
public function addGet($route, $fn) {
$this->add('GET', $route, $fn);
}
/**
* Adds a before route to the list
*
* The before routes are executed first
*
* @param string $method The method like GET, POST, etc
* @param string $route Part of the URI
* @param callable|string $fn Callback function or 'Method@Controller'
* @return void
*/
public function addBefore($method, $route, $fn) {
if (strlen($route) > 0 && $route[0] === '/') {
$route = substr($route, 1);
}
foreach (explode('|', $method) as $m) {
$this->beforeroutes[] = Array('method' => $m, 'fn' => $fn, 'route' => $route);
}
}
/**
* Adds a before route to the list (GET)
*
* The before routes are executed first
*
* @param string $route Part of the URI
* @param callable|string $fn Callback function or 'Method@Controller'
* @return void
*/
public function addBeforeGet($route, $fn) {
$this->addBefore('GET', $route, $fn);
}
/**
* Adds a 404 route to the list
*
* Add a callback
*
* @param callable $fn Callback function
* @return void
*/
public function add404($fn) {
$this->error404 = $fn;
}
/**
* Starts routing
*
* @return int Number of routes
*/
public function start() {
$port = $_SERVER['SERVER_PORT'];
$request_uri = $_SERVER['REQUEST_URI'];
$request_method = $_SERVER['REQUEST_METHOD'];
$query = $_SERVER['QUERY_STRING'];
$uri = substr($request_uri, 1);
$found = 0;
if (!empty($this->beforeroutes)) {
$this->doRoutes($this->beforeroutes, $uri, $request_method);
}
if (!empty($this->routes)) {
$found = $this->doRoutes($this->routes, $uri, $request_method);
}
# Route not found, show 404 message
if ($found === 0 && $this->error404 !== null) {
call_user_func($this->error404);
$found++;
}
return $found;
}
private function doRoutes($routes, $uri, $request_method) {
$found = 0;
# Test all stored routes
foreach ($routes as $route) {
$method = $route['method'];
$fn = $route['fn'];
if ($method === $request_method) {
if (preg_match('#^' . $route['route'] . '$#', $uri, $matches) === 1) {
# Remove first array element
$matches = array_slice($matches, 1);
# Callback given
if (is_callable($fn)) {
call_user_func_array($fn, $matches);
$found++;
} else if (is_string($fn)) {
# String 'controller@method' given
$splitted = preg_split('/@/', substr($fn, strrpos($fn, '\\')));
$path = substr($fn, 0, strrpos($fn, '\\'));
$controller = $splitted[0];
$method = $splitted[1] ?? '';
$filename = $this->controllerpath . $path . $controller . '.php';
if (file_exists($filename)) {
require_once $filename;
$instance = new $controller();
if ($method !== '' && method_exists($controller, $method)) {
$instance->{$method}($matches);
} else {
$instance->index();
}
$found++;
}
}
}
}
}
return $found;
}
}