-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRouter.php
142 lines (114 loc) · 5.58 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
<?php
namespace dlpwd\router;
use ReflectionFunction;
use ReflectionMethod;
class Router
{
private $__routes = [
'get' => [],
'post' => [],
'delete' => [],
'put' => [],
'patch' => [],
'options' => [],
'head' => [],
];
private $__prefix = '';
public function get($_url, $_handler) {
$this->__routes['get'][$this->toRegex($_url)] = $_handler;
}
public function post($_url, $_handler) {
$this->__routes['post'][$this->toRegex($_url)] = $_handler;
}
public function delete($_url, $_handler) {
$this->__routes['delete'][$this->toRegex($_url)] = $_handler;
}
public function patch($_url, $_handler) {
$this->__routes['patch'][$this->toRegex($_url)] = $_handler;
}
public function put($_url, $_handler) {
$this->__routes['put'][$this->toRegex($_url)] = $_handler;
}
public function options($_url, $_handler) {
$this->__routes['options'][$this->toRegex($_url)] = $_handler;
}
public function head($_url, $_handler) {
$this->__routes['head'][$this->toRegex($_url)] = $_handler;
}
public function addRoute($_method, $_url, $_handler) {
if(!\in_array(\strtolower($_method), ['get', 'post', 'delete', 'patch', 'put', 'options', 'head'])) {
throw new \InvalidArgumentException(sprintf('Argument 1 to \\dlpwd\\router\\Router::addRoute must be one of "GET", "POST", "DELETE", "PATCH", "PUT", "OPTIONS" or "HEAD". %s given', $_method));
}
$this->__routes[\strtolower($_method)][$this->toRegex($_url)] = $_handler;
}
private function toRegex($_url) {
$_url = rtrim($this->__prefix . '/' . trim($_url, '/'), '/');
return \preg_replace_callback('/{\s*?([a-zA-Z_][\w]+)\s*?}/', function($matches) {
return '(?<'.trim($matches[1]).'>[^\/]+)';
}, str_replace('/', '\/',$_url));
}
public function addPrefix($_prefix, callable $_callback) {
$oldPrefix = $this->__prefix;
$this->__prefix = $oldPrefix . '/' . trim($_prefix,'/');
$_callback($this);
$this->__prefix = $oldPrefix;
}
public function resolve($_url = null, $_method = null)
{
if(!isset($_url)) {
$_url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
}
if(!isset($_method)) {
$_method = $_SERVER['REQUEST_METHOD'];
}
foreach($this->__routes[strtolower($_method)] as $_regex => $_handler) {
$matches = [];
$result = \preg_match('/^' . $_regex . '\/?$/', $_url, $matches);
if($result == 1) {
if($_handler instanceof \Closure) {
$reflection = new \ReflectionFunction($_handler);
} else {
if(\stripos($_handler, '::')) {
$handlerData = \explode('::', $_handler);
if(!\class_exists($handlerData[0])) {
throw new RouterException(sprintf("The class specified as the controller for the requested url does not exist.\nRequested URL: %s\nController class: %s", $_url, $handlerData[0]), 500);
}
if(!\method_exists($handlerData[0], $handlerData[1])) {
throw new RouterException(sprintf("The method specified as the controller for the requested url does not exist on the specified class.\nRequested URL: %s\nController class: %s\nMethod: %s", $_url, $handlerData[0], $handlerData[1]), 500);
}
$reflection = new \ReflectionMethod($_handler);
if(!$reflection->isPublic()) {
throw new RouterException(sprintf("The method specified as the controller for the requested url is not public\nRequested URL: %s\nController Class: %s\nMethod: %s", $_url, $handlerData[0], $handlerData[1]), 500);
}
} else {
$reflection = new \ReflectionFunction($_handler);
}
}
$pass = array();
foreach ($reflection->getParameters() as $param) {
if (isset($matches[$param->getName()])) {
$pass[] = $matches[$param->getName()];
} else {
if($param->isDefaultValueAvailable()) {
$pass[] = $param->getDefaultValue();
}
else {
throw new RouterException(sprintf('No value provided for parameter %s on method %s in class %s. Either update the route calling this controller method to capture the parameter value, or provide a default value for the parameter in the method definition.',$param->getName(), $handlerData[1], $handlerData[0]), 500);
}
}
}
if($reflection instanceof ReflectionMethod) {
if ($reflection->isStatic()) {
return $reflection->invokeArgs(null, $pass);
} else {
$controllerClass = new $handlerData[0]();
return $reflection->invokeArgs($controllerClass, $pass);
}
} else if ($reflection instanceof ReflectionFunction) {
return $reflection->invokeArgs($pass);
}
}
}
throw new RouterException(\sprintf('No route found which matches requested URL "%s"', $_url), 404);
}
}