1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Route; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Route\Exception\ConfigurationException; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Route to match a $path, mounted on $mount having a $handler to call. |
11
|
|
|
*/ |
12
|
|
|
class Route |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string $name a name for this route. |
16
|
|
|
* @var string $info description of route. |
17
|
|
|
* @var array $method the method(s) to support |
18
|
|
|
* @var string $methodMatched the matched method. |
19
|
|
|
* @var string $mount where to mount the path |
20
|
|
|
* @var string $path the path rule for this route |
21
|
|
|
* @var string $pathMatched the matched path. |
22
|
|
|
* @var callable $handler the callback to handle this route |
23
|
|
|
* @var null|array $arguments arguments for the callback, extracted |
24
|
|
|
* from path |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
private $info; |
28
|
|
|
private $method; |
29
|
|
|
private $methodMatched; |
30
|
|
|
private $mount; |
31
|
|
|
private $path; |
32
|
|
|
private $pathMatched; |
33
|
|
|
private $handler; |
34
|
|
|
private $arguments = []; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set values for route. |
40
|
|
|
* |
41
|
|
|
* @param string|array $method as request method to support |
42
|
|
|
* @param string $mount where to mount the path |
43
|
|
|
* @param string $path for this route |
44
|
|
|
* @param string|array|callable $handler for this path, callable or equal |
45
|
|
|
* @param string $info description of the route |
46
|
|
|
* |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
164 |
|
public function set( |
50
|
|
|
$method = null, |
51
|
|
|
$mount = null, |
52
|
|
|
$path = null, |
53
|
|
|
$handler = null, |
54
|
|
|
string $info = null |
55
|
|
|
) : object { |
56
|
164 |
|
$this->mount = rtrim($mount, "/"); |
57
|
164 |
|
$this->path = $path; |
58
|
164 |
|
$this->handler = $handler; |
59
|
164 |
|
$this->info = $info; |
60
|
|
|
|
61
|
164 |
|
$this->method = $method; |
62
|
164 |
|
if (is_string($method)) { |
63
|
22 |
|
$this->method = array_map("trim", explode("|", $method)); |
64
|
|
|
} |
65
|
164 |
|
if (is_array($this->method)) { |
66
|
64 |
|
$this->method = array_map("strtoupper", $this->method); |
67
|
|
|
} |
68
|
|
|
|
69
|
164 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if the route matches a query and request method. |
76
|
|
|
* |
77
|
|
|
* @param string $query to match against |
78
|
|
|
* @param string $method as request method |
79
|
|
|
* |
80
|
|
|
* @return boolean true if query matches the route |
81
|
|
|
*/ |
82
|
147 |
|
public function match(string $query, string $method = null) |
83
|
|
|
{ |
84
|
147 |
|
$this->arguments = []; |
85
|
147 |
|
$this->methodMatched = null; |
86
|
147 |
|
$this->pathMatched = null; |
87
|
|
|
|
88
|
147 |
|
$matcher = new RouteMatcher(); |
89
|
147 |
|
$res = $matcher->match( |
90
|
147 |
|
$this->mount, |
91
|
147 |
|
$this->path, |
92
|
147 |
|
$this->getAbsolutePath(), |
93
|
147 |
|
$query, |
94
|
147 |
|
$this->method, |
|
|
|
|
95
|
147 |
|
$method |
96
|
|
|
); |
97
|
147 |
|
$this->arguments = $matcher->arguments; |
|
|
|
|
98
|
147 |
|
$this->methodMatched = $matcher->methodMatched; |
99
|
147 |
|
$this->pathMatched = $matcher->pathMatched; |
100
|
|
|
|
101
|
147 |
|
return $res; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Handle the action for the route. |
108
|
|
|
* |
109
|
|
|
* @param string $path the matched path |
110
|
|
|
* @param ContainerInjectableInterface $di container with services |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
142 |
|
public function handle( |
115
|
|
|
string $path = null, |
116
|
|
|
ContainerInterface $di = null |
117
|
|
|
) { |
118
|
142 |
View Code Duplication |
if ($this->mount) { |
|
|
|
|
119
|
|
|
// Remove the mount path to get base for controller |
120
|
24 |
|
$len = strlen($this->mount); |
121
|
24 |
|
if (substr($path, 0, $len) == $this->mount) { |
122
|
24 |
|
$path = ltrim(substr($path, $len), "/"); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
142 |
|
$handler = new RouteHandler(); |
127
|
142 |
|
return $handler->handle($this->methodMatched, $path, $this->handler, $this->arguments, $di); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the matched basename of the path, its the part without the mount |
134
|
|
|
* point. |
135
|
|
|
* |
136
|
|
|
* @return string|null |
137
|
|
|
*/ |
138
|
3 |
|
public function getMatchedPath() |
139
|
|
|
{ |
140
|
3 |
|
$path = $this->pathMatched; |
141
|
3 |
View Code Duplication |
if ($this->mount) { |
|
|
|
|
142
|
1 |
|
$len = strlen($this->mount); |
143
|
1 |
|
if (substr($path, 0, $len) == $this->mount) { |
144
|
1 |
|
$path = ltrim(substr($path, $len), "/"); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
3 |
|
return $path; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set the name of the route. |
155
|
|
|
* |
156
|
|
|
* @param string $name set a name for the route |
157
|
|
|
* |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
1 |
|
public function setName($name) |
161
|
|
|
{ |
162
|
1 |
|
$this->name = $name; |
163
|
1 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set the path that matched this route. |
170
|
|
|
* |
171
|
|
|
* @param string $path to set |
172
|
|
|
* |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
21 |
|
public function setMatchedPath($path) |
176
|
|
|
{ |
177
|
21 |
|
$this->pathMatched = $path; |
178
|
21 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get information of the route. |
185
|
|
|
* |
186
|
|
|
* @return null|string as route information. |
187
|
|
|
*/ |
188
|
2 |
|
public function getInfo() |
189
|
|
|
{ |
190
|
2 |
|
return $this->info; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get the path for the route. |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
1 |
|
public function getPath() |
201
|
|
|
{ |
202
|
1 |
|
return $this->path; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get the absolute $path by adding $mount. |
209
|
|
|
* |
210
|
|
|
* @return string|null as absolute path for this route. |
211
|
|
|
*/ |
212
|
147 |
|
public function getAbsolutePath() |
213
|
|
|
{ |
214
|
147 |
|
if (is_null($this->mount) && is_null($this->path)) { |
215
|
|
|
return null; |
216
|
|
|
} |
217
|
|
|
|
218
|
147 |
|
if (empty($this->mount)) { |
219
|
127 |
|
return $this->path; |
220
|
|
|
} |
221
|
|
|
|
222
|
24 |
|
return $this->mount . "/" . $this->path; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the request method for the route. |
229
|
|
|
* |
230
|
|
|
* @return string representing the request method supported |
231
|
|
|
*/ |
232
|
1 |
|
public function getRequestMethod() : string |
233
|
|
|
{ |
234
|
1 |
|
return is_array($this->method) |
235
|
1 |
|
? implode("|", $this->method) |
236
|
1 |
|
: ""; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.