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