1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Igni\Http; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Routing\Route as BaseRoute; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Proxy class for symfony's route. |
9
|
|
|
* |
10
|
|
|
* @package Igni\Http |
11
|
|
|
*/ |
12
|
|
|
class Route |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var BaseRoute |
16
|
|
|
*/ |
17
|
|
|
private $baseRoute; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $name; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $requirements = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $defaults = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $attributes = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Route constructor. |
41
|
|
|
* |
42
|
|
|
* @param string $path |
43
|
|
|
* @param string $name |
44
|
|
|
* @param array $methods |
45
|
|
|
*/ |
46
|
15 |
|
public function __construct(string $path, array $methods = [], string $name = null) |
47
|
|
|
{ |
48
|
15 |
|
$this->name = $name ?? self::generateNameFromPath($path); |
49
|
15 |
|
$this->baseRoute = new BaseRoute($path); |
50
|
15 |
|
$this->baseRoute->setMethods($methods); |
51
|
15 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $parameter |
55
|
|
|
* @param string $regex |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function addRule(string $parameter, string $regex): self |
59
|
|
|
{ |
60
|
|
|
$this->requirements[$parameter] = $regex; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function setDefaults(array $defaults): self |
66
|
|
|
{ |
67
|
1 |
|
$this->defaults = $defaults; |
68
|
|
|
|
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function setName(string $name): self |
73
|
|
|
{ |
74
|
|
|
$this->name = $name; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
13 |
|
public function getName(): string |
80
|
|
|
{ |
81
|
13 |
|
return $this->name; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets controller that is going to handle all request |
86
|
|
|
* which uri matches the route pattern. |
87
|
|
|
* |
88
|
|
|
* @param string|callable $handler |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
11 |
|
public function delegate($handler): self |
92
|
|
|
{ |
93
|
11 |
|
$this->defaults['-controller'] = $handler; |
94
|
|
|
|
95
|
11 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns route pattern. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
2 |
|
public function getPath(): string |
104
|
|
|
{ |
105
|
2 |
|
return $this->baseRoute->getPath(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns request methods. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function getMethods(): array |
114
|
|
|
{ |
115
|
|
|
return $this->baseRoute->getMethods(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns controller that listen to request |
120
|
|
|
* which uri matches the route pattern. |
121
|
|
|
* |
122
|
|
|
* @return callable|string|null |
123
|
|
|
*/ |
124
|
9 |
|
public function getDelegator() |
125
|
|
|
{ |
126
|
9 |
|
return $this->defaults['-controller'] ?? null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Factories new instance of the current route with |
131
|
|
|
* attributes retrieved from client's request. |
132
|
|
|
* |
133
|
|
|
* @param array $attributes |
134
|
|
|
* @return Route |
135
|
|
|
*/ |
136
|
10 |
|
public function withAttributes(array $attributes): Route |
137
|
|
|
{ |
138
|
10 |
|
$route = clone $this; |
139
|
10 |
|
$route->attributes = $attributes; |
140
|
|
|
|
141
|
10 |
|
return $route; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @internal |
146
|
|
|
* @return BaseRoute |
147
|
|
|
*/ |
148
|
13 |
|
public function getBaseRoute(): BaseRoute |
149
|
|
|
{ |
150
|
13 |
|
$this->baseRoute->setDefaults($this->defaults); |
151
|
13 |
|
$this->baseRoute->setRequirements($this->requirements); |
152
|
|
|
|
153
|
13 |
|
return $this->baseRoute; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns attributes extracted from the uri. |
158
|
|
|
* |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
10 |
|
public function getAttributes(): array |
162
|
|
|
{ |
163
|
10 |
|
return $this->attributes; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Factories new instance of the route |
168
|
|
|
* that will be matched against get request. |
169
|
|
|
* |
170
|
|
|
* @param string $path |
171
|
|
|
* @param string $name |
172
|
|
|
* @return Route |
173
|
|
|
*/ |
174
|
9 |
|
public static function get(string $path, string $name = null): Route |
175
|
|
|
{ |
176
|
9 |
|
return new self($path, [Request::METHOD_GET], $name ?? self::generateNameFromPath($path)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Factories new instance of the route |
181
|
|
|
* that will be matched against post request. |
182
|
|
|
* |
183
|
|
|
* @param string $path |
184
|
|
|
* @param string $name |
185
|
|
|
* @return Route |
186
|
|
|
*/ |
187
|
2 |
|
public static function post(string $path, string $name = null): Route |
188
|
|
|
{ |
189
|
2 |
|
return new self($path, [Request::METHOD_POST], $name ?? self::generateNameFromPath($path)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Factories new instance of the route |
194
|
|
|
* that will be matched against put request. |
195
|
|
|
* |
196
|
|
|
* @param string $path |
197
|
|
|
* @param string $name |
198
|
|
|
* @return Route |
199
|
|
|
*/ |
200
|
1 |
|
public static function put(string $path, string $name = null): Route |
201
|
|
|
{ |
202
|
1 |
|
return new self($path, [Request::METHOD_PUT], $name ?? self::generateNameFromPath($path)); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Factories new instance of the route |
207
|
|
|
* that will be matched against delete request. |
208
|
|
|
* |
209
|
|
|
* @param string $path |
210
|
|
|
* @param string $name |
211
|
|
|
* @return Route |
212
|
|
|
*/ |
213
|
1 |
|
public static function delete(string $path, string $name = null): Route |
214
|
|
|
{ |
215
|
1 |
|
return new self($path, [Request::METHOD_DELETE], $name ?? self::generateNameFromPath($path)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Factories new instance of the route |
220
|
|
|
* that will be matched against patch request. |
221
|
|
|
* |
222
|
|
|
* @param string $path |
223
|
|
|
* @param string $name |
224
|
|
|
* @return Route |
225
|
|
|
*/ |
226
|
1 |
|
public static function patch(string $path, string $name = null): Route |
227
|
|
|
{ |
228
|
1 |
|
return new self($path, [Request::METHOD_PATCH], $name ?? self::generateNameFromPath($path)); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Factories new instance of the route |
233
|
|
|
* that will be matched against head request. |
234
|
|
|
* |
235
|
|
|
* @param string $path |
236
|
|
|
* @param string $name |
237
|
|
|
* @return Route |
238
|
|
|
*/ |
239
|
1 |
|
public static function head(string $path, string $name = null): Route |
240
|
|
|
{ |
241
|
1 |
|
return new self($path, [Request::METHOD_HEAD, Request::METHOD_GET], $name ?? self::generateNameFromPath($path)); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Factories new instance of the route |
246
|
|
|
* that will be matched against options request. |
247
|
|
|
* |
248
|
|
|
* @param string $path |
249
|
|
|
* @param string $name |
250
|
|
|
* @return Route |
251
|
|
|
*/ |
252
|
1 |
|
public static function options(string $path, string $name = null): Route |
253
|
|
|
{ |
254
|
1 |
|
return new self($path, [Request::METHOD_OPTIONS], $name ?? self::generateNameFromPath($path)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Generates default name from given path expression, |
259
|
|
|
* /some/{resource} becomes some_resource |
260
|
|
|
* @param string $path |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
15 |
|
private static function generateNameFromPath(string $path): string |
264
|
|
|
{ |
265
|
15 |
|
return str_replace(['{', '}', '/'], ['', '', '_'], trim($path, '/')); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|