|
1
|
|
|
<?php |
|
2
|
|
|
namespace DevOp\Core\Router; |
|
3
|
|
|
|
|
4
|
|
|
use DevOp\Core\Router\Route; |
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
|
6
|
|
|
|
|
7
|
|
|
class Router |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
private $collection; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $name |
|
17
|
|
|
* @param array $methods |
|
18
|
|
|
* @param string $pattern |
|
19
|
|
|
* @param object|callback|array $callback |
|
20
|
|
|
* @return $this |
|
21
|
|
|
*/ |
|
22
|
10 |
|
public function add($name, array $methods, $pattern, $callback) |
|
23
|
|
|
{ |
|
24
|
|
|
/* @var $route Route */ |
|
25
|
10 |
|
$route = new Route($pattern, $callback); |
|
26
|
|
|
|
|
27
|
10 |
|
foreach ($methods AS $method) { |
|
28
|
10 |
|
if (!isset($this->collection[$method])) { |
|
29
|
10 |
|
$this->collection[$method] = []; |
|
30
|
5 |
|
} |
|
31
|
10 |
|
$this->collection[$method][$name] = $route; |
|
32
|
5 |
|
} |
|
33
|
|
|
|
|
34
|
10 |
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getAll() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->collection; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param RequestInterface $request |
|
47
|
|
|
* @return Route |
|
48
|
|
|
* @throws Exceptions\RouteNotFoundException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function dispatch(RequestInterface $request) |
|
51
|
|
|
{ |
|
52
|
|
|
|
|
53
|
|
|
if (!isset($this->collection[$request->getMethod()])) { |
|
54
|
|
|
throw new Exceptions\RouteNotFoundException(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$uri = $request->getUri()->getPath(); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($this->collection[$request->getMethod()] AS /* @var $route Route */ $route) { |
|
60
|
|
|
if (preg_match("#^{$route->getRegEx()}+$#iu", $uri, $match)) { |
|
61
|
|
|
return $this->process($route, $match); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
throw new Exceptions\RouteNotFoundException(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Route $route |
|
70
|
|
|
* @param array $match |
|
71
|
|
|
* @return Route |
|
72
|
|
|
* @throws Exceptions\RouteIsNotCallableException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function process(Route $route, $match) |
|
75
|
|
|
{ |
|
76
|
|
|
|
|
77
|
|
|
$arguments = []; |
|
78
|
|
|
foreach ($route->getParameters() AS $param) { |
|
79
|
|
|
if (isset($match[$param])) { |
|
80
|
|
|
$arguments[$param] = $match[$param]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$route->setValues($arguments); |
|
85
|
|
|
|
|
86
|
|
|
if (is_callable($route->getCallback(), true)) { |
|
87
|
|
|
return $route; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
throw new Exceptions\RouteIsNotCallableException; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @param string $pattern |
|
96
|
|
|
* @param mixed $callback |
|
97
|
|
|
* @return self |
|
98
|
|
|
*/ |
|
99
|
8 |
|
public function get($name, $pattern, $callback) |
|
100
|
|
|
{ |
|
101
|
8 |
|
return $this->add($name, ['GET'], $pattern, $callback); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* @param string $pattern |
|
107
|
|
|
* @param mixed $callback |
|
108
|
|
|
* @return self |
|
109
|
|
|
*/ |
|
110
|
|
|
public function post($name, $pattern, $callback) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->add($name, ['POST'], $pattern, $callback); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $name |
|
117
|
|
|
* @param string $pattern |
|
118
|
|
|
* @param mixed $callback |
|
119
|
|
|
* @return self |
|
120
|
|
|
*/ |
|
121
|
|
|
public function put($name, $pattern, $callback) |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->add($name, ['PUT'], $pattern, $callback); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $name |
|
128
|
|
|
* @param string $pattern |
|
129
|
|
|
* @param mixed $callback |
|
130
|
|
|
* @return self |
|
131
|
|
|
*/ |
|
132
|
|
|
public function delete($name, $pattern, $callback) |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->add($name, ['DELETE'], $pattern, $callback); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param string $name |
|
139
|
|
|
* @param string $pattern |
|
140
|
|
|
* @param mixed $callback |
|
141
|
|
|
* @return self |
|
142
|
|
|
*/ |
|
143
|
|
|
public function options($name, $pattern, $callback) |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->add($name, ['OPTIONS'], $pattern, $callback); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param string $name |
|
150
|
|
|
* @param string $pattern |
|
151
|
|
|
* @param mixed $callback |
|
152
|
|
|
* @return self |
|
153
|
|
|
*/ |
|
154
|
|
|
public function patch($name, $pattern, $callback) |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->add($name, ['PATCH'], $pattern, $callback); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $name |
|
161
|
|
|
* @param string $pattern |
|
162
|
|
|
* @param mixed $callback |
|
163
|
|
|
* @return self |
|
164
|
|
|
*/ |
|
165
|
|
|
public function head($name, $pattern, $callback) |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->add($name, ['HEAD'], $pattern, $callback); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $name |
|
172
|
|
|
* @param string $pattern |
|
173
|
|
|
* @param mixed $callback |
|
174
|
|
|
* @return self |
|
175
|
|
|
*/ |
|
176
|
|
|
public function any($name, $pattern, $callback) |
|
177
|
|
|
{ |
|
178
|
|
|
$methods = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD']; |
|
179
|
|
|
return $this->add($name, $methods, $pattern, $callback); |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|