|
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
|
|
|
public function add($name, array $methods, $pattern, $callback) |
|
23
|
|
|
{ |
|
24
|
|
|
/* @var $route Route */ |
|
25
|
|
|
$route = new Route($pattern, $callback); |
|
26
|
|
|
|
|
27
|
|
|
foreach ($methods AS $method) { |
|
28
|
|
|
if (!isset($this->collection[$method])) { |
|
29
|
|
|
$this->collection[$method] = []; |
|
30
|
|
|
} |
|
31
|
|
|
$this->collection[$method][$name] = $route; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function match(RequestInterface $request) |
|
38
|
|
|
{ |
|
39
|
|
|
if (isset($this->collection[$request->getMethod()])) { |
|
40
|
|
|
foreach ($this->collection[$request->getMethod()] AS /* @var $route Route */ $route) { |
|
41
|
|
|
$match = null; |
|
42
|
|
|
if (preg_match_all($route->getRegEx(), $request->getRequestTarget(), $match)) { |
|
43
|
|
|
var_dump($match); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getAll() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->collection; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function dispatch($method, $uri) |
|
55
|
|
|
{ |
|
56
|
|
|
|
|
57
|
|
|
if (!isset($this->collection[$method])) { |
|
58
|
|
|
die('$)$'); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
foreach ($this->collection[$method] AS /* @var $route Route */ $route) { |
|
62
|
|
|
if (preg_match("#^{$route->getPattern()}+$#iu", $uri)) { |
|
63
|
|
|
var_dump($route); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function get($name, $pattern, $callback) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->add($name, 'GET', $pattern, $callback); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function post($name, $pattern, $callback) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->add($name, 'POST', $pattern, $callback); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function put($name, $pattern, $callback) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->add($name, 'PUT', $pattern, $callback); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function delete($name, $pattern, $callback) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->add($name, 'DELETE', $pattern, $callback); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function options($name, $pattern, $callback) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->add($name, 'OPTIONS', $pattern, $callback); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function patch($name, $pattern, $callback) |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->add($name, 'PATCH', $pattern, $callback); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function head($name, $pattern, $callback) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->add($name, 'HEAD', $pattern, $callback); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function any($name, $pattern, $callback) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->add($name, ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD'], $pattern, $callback); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|