|
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
|
|
|
} |
|
32
|
2 |
|
$this->collection[$method][$name] = $route; |
|
33
|
|
|
} |
|
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 (is_callable($route->getCallback())) { |
|
76
|
|
|
return call_user_func_array(/** @scrutinizer ignore-type */ $route->getCallback(), [$request, $response]); |
|
77
|
|
|
} else if (is_string($route->getCallback())) { |
|
78
|
|
|
if (strchr($route->getCallback(), ':')) { |
|
79
|
|
|
list($controller, $method) = explode(':', $route->getCallback()); |
|
80
|
|
|
} else { |
|
81
|
|
|
$controller = $route->getCallback(); |
|
82
|
|
|
$method = '__invoke'; |
|
83
|
|
|
} |
|
84
|
|
|
return call_user_func_array([$controller, $method], [$request, $response]); |
|
85
|
|
|
} else if (is_array($route->getCallback())) { |
|
86
|
|
|
$controller = $route->getCallback()[0]; |
|
87
|
|
|
$method= $route->getCallback()[1]; |
|
88
|
|
|
$parameters = array_merge([$request, $response], array_slice($route->getCallback(), 2)); |
|
89
|
|
|
call_user_func_array([$controller, $method], $parameters); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
throw new Exceptions\RouteIsNotCallableException(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function get($name, $pattern, $callback) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->add($name, ['GET'], $pattern, $callback); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function post($name, $pattern, $callback) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->add($name, ['POST'], $pattern, $callback); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function put($name, $pattern, $callback) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->add($name, ['PUT'], $pattern, $callback); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function delete($name, $pattern, $callback) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->add($name, ['DELETE'], $pattern, $callback); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function options($name, $pattern, $callback) |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->add($name, ['OPTIONS'], $pattern, $callback); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function patch($name, $pattern, $callback) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->add($name, ['PATCH'], $pattern, $callback); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function head($name, $pattern, $callback) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->add($name, ['HEAD'], $pattern, $callback); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function any($name, $pattern, $callback) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->add($name, ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD'], $pattern, $callback); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|