1
|
|
|
<?php |
2
|
|
|
namespace DevOp\Core\Router; |
3
|
|
|
|
4
|
|
|
use DevOp\Core\Router\Route; |
5
|
|
|
use Psr\Http\Message\RequestInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
class Router |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $collection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $name |
18
|
|
|
* @param array $methods |
19
|
|
|
* @param string $pattern |
20
|
|
|
* @param object|callback|array $callback |
21
|
|
|
* @return $this |
22
|
|
|
*/ |
23
|
2 |
|
public function add($name, array $methods, $pattern, $callback) |
24
|
|
|
{ |
25
|
|
|
/* @var $route Route */ |
26
|
2 |
|
$route = new Route($pattern, $callback); |
27
|
|
|
|
28
|
2 |
|
foreach ($methods AS $method) { |
29
|
2 |
|
if (!isset($this->collection[$method])) { |
30
|
2 |
|
$this->collection[$method] = []; |
31
|
1 |
|
} |
32
|
2 |
|
$this->collection[$method][$name] = $route; |
33
|
1 |
|
} |
34
|
|
|
|
35
|
2 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getAll() |
39
|
|
|
{ |
40
|
|
|
return $this->collection; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* @param RequestInterface $request |
46
|
|
|
* @param ResponseInterface $response |
47
|
|
|
* @return ResponseInterface |
48
|
|
|
* @throws Exceptions\RouteNotFoundException |
49
|
|
|
*/ |
50
|
|
|
public function dispatch(RequestInterface $request, ResponseInterface $response) |
51
|
|
|
{ |
52
|
|
|
|
53
|
|
|
if (!isset($this->collection[$request->getMethod()])) { |
54
|
|
|
throw new Exceptions\RouteNotFoundException(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$uri = $request->getUri()->getPath(); |
58
|
|
|
foreach ($this->collection[$request->getMethod()] AS /* @var $route Route */ $route) { |
59
|
|
|
if (preg_match("#^{$route->getPattern()}+$#iu", $uri, $match)) { |
60
|
|
|
return $this->process($request, $response, $route); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw new Exceptions\RouteNotFoundException(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
* @param Route $route |
70
|
|
|
* @return ResponseInterface |
71
|
|
|
* @throws Exceptions\RouteIsNotCallableException |
72
|
|
|
*/ |
73
|
|
|
public function process(RequestInterface $request, ResponseInterface $response, Route $route) |
74
|
|
|
{ |
75
|
|
|
if ($route->getCallback() === 'string') { |
76
|
|
|
if (strchr($route->getCallback(), ':')) { |
|
|
|
|
77
|
|
|
list($controller, $method) = explode(':', $route->getCallback()); |
|
|
|
|
78
|
|
|
} else { |
79
|
|
|
$controller = $route->getCallback(); |
80
|
|
|
$method = '__invoke'; |
81
|
|
|
} |
82
|
|
|
return call_user_func_array([$controller, $method], [$request, $response]); |
83
|
|
|
} else if (is_callable($route->getCallback())) { |
84
|
|
|
return call_user_func_array($route->getCallback(), [$request, $response]); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
throw new Exceptions\RouteIsNotCallableException(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function get($name, $pattern, $callback) |
91
|
|
|
{ |
92
|
|
|
return $this->add($name, 'GET', $pattern, $callback); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function post($name, $pattern, $callback) |
96
|
|
|
{ |
97
|
|
|
return $this->add($name, 'POST', $pattern, $callback); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function put($name, $pattern, $callback) |
101
|
|
|
{ |
102
|
|
|
return $this->add($name, 'PUT', $pattern, $callback); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function delete($name, $pattern, $callback) |
106
|
|
|
{ |
107
|
|
|
return $this->add($name, 'DELETE', $pattern, $callback); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function options($name, $pattern, $callback) |
111
|
|
|
{ |
112
|
|
|
return $this->add($name, 'OPTIONS', $pattern, $callback); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function patch($name, $pattern, $callback) |
116
|
|
|
{ |
117
|
|
|
return $this->add($name, 'PATCH', $pattern, $callback); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function head($name, $pattern, $callback) |
121
|
|
|
{ |
122
|
|
|
return $this->add($name, 'HEAD', $pattern, $callback); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function any($name, $pattern, $callback) |
126
|
|
|
{ |
127
|
|
|
return $this->add($name, ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD'], $pattern, $callback); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|