-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.php
180 lines (145 loc) · 4 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
<?php
namespace Lumille\Routing;
use Symfony\Component\HttpFoundation\Request;
class Router
{
/**
* @var array
*/
private $namedRoutes = [];
/**
* @var string
*/
private $prefix;
/**
* @var Request
*/
private $request;
public function __construct (Request $request)
{
$this->request = $request;
}
public function get ($path, $callable, $name = null)
{
return $this->addRoute(['HEAD', 'GET', 'OPTIONS'], $path, $callable, $name);
}
public function post ($path, $callable, $name = null)
{
return $this->addRoute('POST', $path, $callable, $name);
}
public function patch ($path, $callable, $name = null)
{
return $this->addRoute('PATCH', $path, $callable, $name);
}
public function put ($path, $callable, $name = null)
{
return $this->addRoute('PUT', $path, $callable, $name);
}
public function delete ($path, $callable, $name = null)
{
return $this->addRoute('DELETE', $path, $callable, $name);
}
public function options ($path, $callable, $name = null)
{
return $this->addRoute('OPTIONS', $path, $callable, $name);
}
public function prefix ($prefix, $callable)
{
$router = new Router($this->request);
$prefix = rtrim($prefix, '/');
$prefix = $this->prefix . $prefix;
$router->setPrefix($prefix);
return \call_user_func_array($callable, [$router]);
}
private function addRoute ($methods, $path, $callable, $name = null)
{
if (!is_array($methods)) {
$methods = [$methods];
}
$path = $this->rtrim($path);
$path = $this->addPrefix($path);
$_route = RouteCollection::getInstance()->getRouteByPath($path);
if ($_route) {
$_route->setAcceptMethods($methods);
RouteCollection::getInstance()->setNamedRoutes($_route, $path);
return;
}
$route = new Route($path, $callable);
$route->setRouter($this);
$route->setAcceptMethods($methods);
if (is_string($callable) && $name === null) {
$name = $callable;
}
RouteCollection::getInstance()->setNamedRoutes($route, $name);
RouteCollection::getInstance()
->setRoute($route, $path);
return $route;
}
public function run ()
{
$uri = $this->request->getPathInfo();
$uri = $this->rtrim($uri);
$routes = RouteCollection::getInstance()
->getAllRoutes();
foreach ($routes as $route) {
if ($route->match($uri)) {
if ($route->checkAcceptMethods($this->request->getMethod())) {
return $route->call();
}
throw new MethodNotAcceptedException("Method not accepted");
}
}
throw new UrlNotFoundException('No matching routes');
}
public function getUrl ($name, $params = [])
{
$namedRoutes = RouteCollection::getInstance()
->getNamedRoutes();
if (!isset($namedRoutes[$name])) {
throw new RouterException('No route matches this name');
}
return $namedRoutes[$name]->getUrl($params);
}
/**
* @return mixed
*/
public
function getPrefix ()
{
return $this->prefix;
}
public
function addPrefix ($path)
{
if ($this->prefix) {
$path = $this->prefix . $path;
}
return $path;
}
/**
* @return Request
*/
public
function getRequest (): Request
{
return $this->request;
}
/**
* @param string $prefix
* @return Router
*/
public
function setPrefix (string $prefix): Router
{
$prefix = rtrim($prefix);
$this->prefix .= $prefix;
return $this;
}
private function rtrim ($path)
{
if (strlen($path) > 1) {
$path = rtrim($path, '/');
}
return $path;
}
}