1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\App\Route; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
// From 'charcoal-config' |
8
|
|
|
use Charcoal\Config\AbstractConfig; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base "Route" configuration. |
12
|
|
|
*/ |
13
|
|
|
class RouteConfig extends AbstractConfig |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Route identifier/name |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $ident; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Route pattern |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $route; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* HTTP methods supported by this route |
31
|
|
|
* |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
private $methods = [ 'GET' ]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Response controller classname |
38
|
|
|
* |
39
|
|
|
* Should be the class-ident of an action, a script or a template controller. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $controller; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Parent route groups |
47
|
|
|
* |
48
|
|
|
* @var string[] |
49
|
|
|
*/ |
50
|
|
|
private $groups = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Optional headers to set on response. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $headers = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Retrieve the default route types. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public static function defaultRouteTypes() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'templates', |
68
|
|
|
'actions', |
69
|
|
|
'scripts' |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set route identifier |
75
|
|
|
* |
76
|
|
|
* @param string $ident Route identifier. |
77
|
|
|
* @throws InvalidArgumentException If the identifier is not a string. |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
|
|
public function setIdent($ident) |
81
|
|
|
{ |
82
|
|
|
if (!is_string($ident)) { |
83
|
|
|
throw new InvalidArgumentException( |
84
|
|
|
'Route identifier must be a string.' |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->ident = $ident; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get route identifier |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function ident() |
99
|
|
|
{ |
100
|
|
|
return $this->ident; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set route pattern. |
105
|
|
|
* |
106
|
|
|
* @param string $pattern Route pattern. |
107
|
|
|
* @throws InvalidArgumentException If the pattern argument is not a string. |
108
|
|
|
* @return self |
109
|
|
|
*/ |
110
|
|
|
public function setRoute($pattern) |
111
|
|
|
{ |
112
|
|
|
if (!is_string($pattern)) { |
113
|
|
|
throw new InvalidArgumentException( |
114
|
|
|
'Route pattern must be a string.' |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->route = $pattern; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get route pattern |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function route() |
129
|
|
|
{ |
130
|
|
|
return $this->route; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Set parent route groups |
135
|
|
|
* |
136
|
|
|
* @param string[] $groups The parent route groups. |
137
|
|
|
* @return self |
138
|
|
|
*/ |
139
|
|
|
public function setGroups(array $groups) |
140
|
|
|
{ |
141
|
|
|
$this->groups = []; |
142
|
|
|
|
143
|
|
|
foreach ($groups as $group) { |
144
|
|
|
$this->addGroup($group); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Add parent route group |
152
|
|
|
* |
153
|
|
|
* @param string $group The parent route group. |
154
|
|
|
* @throws InvalidArgumentException If the group is invalid. |
155
|
|
|
* @return self |
156
|
|
|
*/ |
157
|
|
|
public function addGroup($group) |
158
|
|
|
{ |
159
|
|
|
if (!is_string($group)) { |
160
|
|
|
throw new InvalidArgumentException( |
161
|
|
|
'Parent route group must be a string.' |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->groups[] = $group; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get parent route groups |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function groups() |
176
|
|
|
{ |
177
|
|
|
return $this->groups; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Add custom headers |
182
|
|
|
* |
183
|
|
|
* @param array $headers The custom headers, in key=>val pairs. |
184
|
|
|
* @return self |
185
|
|
|
*/ |
186
|
|
|
public function setHeaders(array $headers) |
187
|
|
|
{ |
188
|
|
|
$this->headers = []; |
189
|
|
|
foreach($headers as $name => $val) { |
190
|
|
|
$this->addHeader($name, $val); |
191
|
|
|
} |
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $name The header name (ex: "Content-Type", "Cache-Control"). |
197
|
|
|
* @param string $val The header value. |
198
|
|
|
* @throws InvalidArgumentException If the header name or value is not a string. |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
|
|
public function addHeader($name, $val) |
202
|
|
|
{ |
203
|
|
|
if (!is_string($name)) { |
204
|
|
|
throw new InvalidArgumentException( |
205
|
|
|
'Route header name must be a string.' |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
if (!is_string($val)) { |
209
|
|
|
throw new InvalidArgumentException( |
210
|
|
|
'Route header value must be a string.' |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
$this->headers[$name] = $val; |
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get custom route headers. |
219
|
|
|
* |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
|
|
public function headers() |
223
|
|
|
{ |
224
|
|
|
return $this->headers; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set route view controller classname |
229
|
|
|
* |
230
|
|
|
* @param string $controller Route controller name. |
231
|
|
|
* @throws InvalidArgumentException If the route view controller is not a string. |
232
|
|
|
* @return self |
233
|
|
|
*/ |
234
|
|
|
public function setController($controller) |
235
|
|
|
{ |
236
|
|
|
if (!is_string($controller)) { |
237
|
|
|
throw new InvalidArgumentException( |
238
|
|
|
'Route view controller must be a string.' |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$this->controller = $controller; |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get the view controller classname |
249
|
|
|
* |
250
|
|
|
* If not set, the `self::ident()` will be used by default. |
251
|
|
|
* |
252
|
|
|
* @return string |
253
|
|
|
*/ |
254
|
|
|
public function controller() |
255
|
|
|
{ |
256
|
|
|
if (!isset($this->controller)) { |
257
|
|
|
return $this->ident(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $this->controller; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Set route methods |
265
|
|
|
* |
266
|
|
|
* @param string[] $methods The route's supported HTTP methods. |
267
|
|
|
* @return self |
268
|
|
|
*/ |
269
|
|
|
public function setMethods(array $methods) |
270
|
|
|
{ |
271
|
|
|
$this->methods = []; |
272
|
|
|
|
273
|
|
|
foreach ($methods as $method) { |
274
|
|
|
$this->addMethod($method); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Add route HTTP method. |
282
|
|
|
* |
283
|
|
|
* @param string $method The route's supported HTTP method. |
284
|
|
|
* @throws InvalidArgumentException If the HTTP method is invalid. |
285
|
|
|
* @return self |
286
|
|
|
*/ |
287
|
|
|
public function addMethod($method) |
288
|
|
|
{ |
289
|
|
|
if (!is_string($method)) { |
290
|
|
|
throw new InvalidArgumentException( |
291
|
|
|
sprintf( |
292
|
|
|
'Unsupported HTTP method; must be a string, received %s', |
293
|
|
|
(is_object($method) ? get_class($method) : gettype($method)) |
294
|
|
|
) |
295
|
|
|
); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// According to RFC, methods are defined in uppercase (See RFC 7231) |
299
|
|
|
$method = strtoupper($method); |
300
|
|
|
|
301
|
|
|
$validHttpMethods = [ |
302
|
|
|
'CONNECT', |
303
|
|
|
'DELETE', |
304
|
|
|
'GET', |
305
|
|
|
'HEAD', |
306
|
|
|
'OPTIONS', |
307
|
|
|
'PATCH', |
308
|
|
|
'POST', |
309
|
|
|
'PUT', |
310
|
|
|
'TRACE', |
311
|
|
|
]; |
312
|
|
|
|
313
|
|
|
if (!in_array($method, $validHttpMethods)) { |
314
|
|
|
throw new InvalidArgumentException(sprintf( |
315
|
|
|
'Unsupported HTTP method; must be one of "%s", received "%s"', |
316
|
|
|
implode('","', $validHttpMethods), |
317
|
|
|
$method |
318
|
|
|
)); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$this->methods[] = $method; |
322
|
|
|
|
323
|
|
|
return $this; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Get route methods |
328
|
|
|
* |
329
|
|
|
* @return string[] |
330
|
|
|
*/ |
331
|
|
|
public function methods() |
332
|
|
|
{ |
333
|
|
|
return $this->methods; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|