|
1
|
|
|
<?php |
|
2
|
|
|
namespace nebula\component\route; |
|
3
|
|
|
|
|
4
|
|
|
use nebula\component\route\uri\UriMatcher; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* 路由匹配器 |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class RouteMatcher |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* 匹配的方法 |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $methods; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* 匹配的URI |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $uri; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* 属性 |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $attribute; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Uri匹配器 |
|
36
|
|
|
* |
|
37
|
|
|
* @var UriMatcher |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $matcher; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
public function __construct(array $methods, string $uri, array $attribute=[]) |
|
43
|
|
|
{ |
|
44
|
|
|
array_walk($methods, function ($value) { |
|
45
|
|
|
return strtoupper($value); |
|
46
|
|
|
}); |
|
47
|
|
|
$this->methods = $methods; |
|
48
|
|
|
$this->uri = $uri; |
|
49
|
|
|
$this->matcher = UriMatcher::build($uri); |
|
50
|
|
|
$this->attribute = $attribute; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* 获取属性 |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @param mixed $default |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getAttribute(string $name = null, $default = null) |
|
61
|
|
|
{ |
|
62
|
|
|
if (is_string($name)) { |
|
63
|
|
|
return $this->attribute[$name] ?? $default; |
|
64
|
|
|
} |
|
65
|
|
|
return $this->attribute; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set 属性 |
|
70
|
|
|
* |
|
71
|
|
|
* @param array $attribute 属性 |
|
72
|
|
|
* |
|
73
|
|
|
* @return self |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setAttribute(array $attribute) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->attribute = $attribute; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* 添加属性 |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $name |
|
86
|
|
|
* @param mixed $attribute |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function addAttribute(string $name, $attribute) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->attribute[$name] = $attribute; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get 匹配的方法 |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getMethods() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->methods; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set 匹配的方法 |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $methods 匹配的方法 |
|
110
|
|
|
* |
|
111
|
|
|
* @return self |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setMethods(array $methods) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->methods = $methods; |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get 匹配的URI |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getUri() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->uri; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Set 匹配的URI |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $uri 匹配的URI |
|
134
|
|
|
* |
|
135
|
|
|
* @return self |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setUri(string $uri) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->uri = $uri; |
|
140
|
|
|
|
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get uri匹配器 |
|
146
|
|
|
* |
|
147
|
|
|
* @return UriMatcher |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getMatcher() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->matcher; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* 匹配请求 |
|
156
|
|
|
* |
|
157
|
|
|
* @param RequestInterface $request |
|
158
|
|
|
* @return array|null |
|
159
|
|
|
*/ |
|
160
|
|
|
public function match(RequestInterface $request):?array { |
|
161
|
|
|
if (\in_array($request->getMethod(), $this->methods)) { |
|
162
|
|
|
if ($parameter = $this->matcher->match($request->getUrl())) { |
|
163
|
|
|
return $this->matcher->buildParamter($parameter); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
return null; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|