@@ -82,15 +82,15 @@ class Route extends Controller implements RouteInterface
82
82
*
83
83
* ------------------------------------------------------------------------
84
84
*/
85
- public static function any (
86
- array |string $ route ,
87
- mixed $ callback ,
88
- string $ method = '* ' ,
85
+ public static function any (
86
+ array |string $ route ,
87
+ mixed $ callback ,
88
+ string $ method = '* ' ,
89
89
): self {
90
90
self ::$ any = [
91
- 'route ' => $ route ,
92
- 'method ' => $ method ,
93
- 'callback ' => $ callback ,
91
+ 'route ' => $ route ,
92
+ 'method ' => $ method ,
93
+ 'callback ' => $ callback ,
94
94
];
95
95
96
96
self ::$ route [] = $ route ;
@@ -105,11 +105,11 @@ public static function any (
105
105
* @param string $method Request method
106
106
* @param string|array $route Route parameter
107
107
*/
108
- public static function map (string $ method , string |array $ route ): self
108
+ public static function map (string $ method , string |array $ route ): self
109
109
{
110
110
self ::$ map = [
111
- 'method ' => $ method ,
112
- 'route ' => $ route ,
111
+ 'method ' => $ method ,
112
+ 'route ' => $ route ,
113
113
];
114
114
self ::$ route [] = $ route ;
115
115
return new self ();
@@ -121,18 +121,14 @@ public static function map (string $method, string|array $route): self
121
121
*
122
122
* @param string $name Set the name of the route
123
123
*/
124
- public function name (string $ name ): self
124
+ public function name (string $ name ): self
125
125
{
126
- if (is_array (end (self ::$ route )))
127
- {
128
- for ($ i = 0 ; $ i < count (end (self ::$ route )); $ i ++)
129
- {
126
+ if (is_array (end (self ::$ route ))) {
127
+ for ($ i = 0 ; $ i < count (end (self ::$ route )); $ i ++) {
130
128
add_route_name ("$ name:: $ i " , end (self ::$ route )[$ i ]);
131
129
self ::$ routes ["$ name:: $ i " ] = end (self ::$ route )[$ i ];
132
130
}
133
- }
134
- else
135
- {
131
+ } else {
136
132
add_route_name ($ name , end (self ::$ route ));
137
133
self ::$ routes [$ name ] = end (self ::$ route );
138
134
}
@@ -146,20 +142,17 @@ public function name (string $name): self
146
142
* @param string $route
147
143
* @param Closure $callback
148
144
*/
149
- public function route (string $ route , Closure $ callback ): self
145
+ public function route (string $ route , callable $ callback ): self
150
146
{
151
- $ route = rtrim (' / ' , self ::$ map ['route ' ]) . '/ ' . ltrim ('/ ' , $ route );
147
+ $ route = rtrim (self ::$ map ['route ' ], ' / ' ) . '/ ' . ltrim ($ route , '/ ' );
152
148
153
- if (self ::$ map )
154
- {
149
+ if (self ::$ map ) {
155
150
$ this ->mapRoute = [
156
- 'route ' => $ route ,
157
- 'method ' => self :: $ map [ ' method ' ] ,
158
- 'callback ' => $ callback ,
151
+ 'route ' => $ route ,
152
+ 'method ' => ' * ' ,
153
+ 'callback ' => $ callback ,
159
154
];
160
- }
161
- else
162
- {
155
+ } else {
163
156
throw new Exception ('There is no map to route. ' );
164
157
}
165
158
self ::$ route [] = $ route ;
@@ -172,10 +165,9 @@ public function route (string $route, Closure $callback): self
172
165
*
173
166
* @param mixed $callback
174
167
*/
175
- public function action (mixed $ callback ): self
168
+ public function action (mixed $ callback ): self
176
169
{
177
- if (self ::$ map )
178
- {
170
+ if (self ::$ map ) {
179
171
$ this ->action = $ callback ;
180
172
}
181
173
return $ this ;
@@ -190,8 +182,7 @@ public function action (mixed $callback): self
190
182
*/
191
183
public function use (string $ controller ): self
192
184
{
193
- if (self ::$ map )
194
- {
185
+ if (self ::$ map ) {
195
186
$ this ->use = $ controller ;
196
187
}
197
188
return $ this ;
@@ -203,10 +194,9 @@ public function use(string $controller): self
203
194
*
204
195
* @param string $file
205
196
*/
206
- public function file (string $ file ): self
197
+ public function file (string $ file ): self
207
198
{
208
- if (self ::$ map )
209
- {
199
+ if (self ::$ map ) {
210
200
$ this ->file = $ file ;
211
201
}
212
202
return $ this ;
@@ -221,13 +211,13 @@ public function file (string $file): self
221
211
* @param Closure $closure The closure to handle invalid parameter types.
222
212
* @return self Returns the current instance for method chaining.
223
213
*/
224
- public function handleInvalidParameterType (Closure $ closure ): self
214
+ public function handleInvalidParameterType (Closure $ closure ): self
225
215
{
226
216
$ this ->handleInvalidParameterType = $ closure ;
227
217
return $ this ;
228
218
}
229
-
230
- public function caseSensitive (): self
219
+
220
+ public function caseSensitive (): self
231
221
{
232
222
$ this ->caseSensitive = true ;
233
223
return $ this ;
@@ -239,10 +229,9 @@ public function caseSensitive (): self
239
229
* @param string ...$guards String parameters of registered guards.
240
230
* @return self
241
231
*/
242
- public function withGuard (string ...$ guards ): self
232
+ public function withGuard (string ...$ guards ): self
243
233
{
244
- if (self ::$ map || self ::$ method || self ::$ view )
245
- {
234
+ if (self ::$ map || self ::$ method || self ::$ view ) {
246
235
$ this ->guards = $ guards ;
247
236
}
248
237
return $ this ;
@@ -263,11 +252,11 @@ public function withGuard (string ...$guards): self
263
252
*
264
253
* ---------------------------------------------------------------------------
265
254
*/
266
- public static function view (array |string $ route , string $ view ): self
255
+ public static function view (array |string $ route , string $ view ): self
267
256
{
268
257
self ::$ view = [
269
- 'route ' => $ route ,
270
- 'view ' => $ view ,
258
+ 'route ' => $ route ,
259
+ 'view ' => $ view ,
271
260
];
272
261
273
262
self ::$ route [] = $ route ;
@@ -287,15 +276,15 @@ public static function view (array|string $route, string $view): self
287
276
*
288
277
* ---------------------------------------------------------------
289
278
*/
290
- public static function redirect (
291
- string $ route ,
292
- string $ new_url ,
293
- int $ code = 302 ,
279
+ public static function redirect (
280
+ string $ route ,
281
+ string $ new_url ,
282
+ int $ code = 302 ,
294
283
): self {
295
284
self ::$ redirect = [
296
- 'route ' => $ route ,
297
- 'new_url ' => $ new_url ,
298
- 'code ' => $ code ,
285
+ 'route ' => $ route ,
286
+ 'new_url ' => $ new_url ,
287
+ 'code ' => $ code ,
299
288
];
300
289
301
290
self ::$ route [] = $ route ;
@@ -311,12 +300,12 @@ public static function redirect (
311
300
*
312
301
* --------------------------------------------------------------
313
302
*/
314
- public static function get (array |string $ route , $ callback ): self
303
+ public static function get (array |string $ route , $ callback ): self
315
304
{
316
305
self ::$ method = [
317
- 'route ' => $ route ,
318
- 'method ' => 'GET ' ,
319
- 'callback ' => $ callback ,
306
+ 'route ' => $ route ,
307
+ 'method ' => 'GET ' ,
308
+ 'callback ' => $ callback ,
320
309
];
321
310
322
311
self ::$ route [] = $ route ;
@@ -332,12 +321,12 @@ public static function get (array|string $route, $callback): self
332
321
*
333
322
* --------------------------------------------------------------
334
323
*/
335
- public static function post (array |string $ route , $ callback ): self
324
+ public static function post (array |string $ route , $ callback ): self
336
325
{
337
326
self ::$ method = [
338
- 'route ' => $ route ,
339
- 'method ' => 'POST ' ,
340
- 'callback ' => $ callback ,
327
+ 'route ' => $ route ,
328
+ 'method ' => 'POST ' ,
329
+ 'callback ' => $ callback ,
341
330
];
342
331
343
332
self ::$ route [] = $ route ;
@@ -353,12 +342,12 @@ public static function post (array|string $route, $callback): self
353
342
*
354
343
* --------------------------------------------------------------
355
344
*/
356
- public static function put (array |string $ route , $ callback ): self
345
+ public static function put (array |string $ route , $ callback ): self
357
346
{
358
347
self ::$ method = [
359
- 'route ' => $ route ,
360
- 'method ' => 'PUT ' ,
361
- 'callback ' => $ callback ,
348
+ 'route ' => $ route ,
349
+ 'method ' => 'PUT ' ,
350
+ 'callback ' => $ callback ,
362
351
];
363
352
364
353
self ::$ route [] = $ route ;
@@ -374,12 +363,12 @@ public static function put (array|string $route, $callback): self
374
363
*
375
364
* --------------------------------------------------------------
376
365
*/
377
- public static function patch (array |string $ route , $ callback ): self
366
+ public static function patch (array |string $ route , $ callback ): self
378
367
{
379
368
self ::$ method = [
380
- 'route ' => $ route ,
381
- 'method ' => 'PATCH ' ,
382
- 'callback ' => $ callback ,
369
+ 'route ' => $ route ,
370
+ 'method ' => 'PATCH ' ,
371
+ 'callback ' => $ callback ,
383
372
];
384
373
385
374
self ::$ route [] = $ route ;
@@ -395,86 +384,74 @@ public static function patch (array|string $route, $callback): self
395
384
*
396
385
* --------------------------------------------------------------
397
386
*/
398
- public static function delete (array |string $ route , $ callback ): self
387
+ public static function delete (array |string $ route , $ callback ): self
399
388
{
400
389
self ::$ method = [
401
- 'route ' => $ route ,
402
- 'method ' => 'DELETE ' ,
403
- 'callback ' => $ callback ,
390
+ 'route ' => $ route ,
391
+ 'method ' => 'DELETE ' ,
392
+ 'callback ' => $ callback ,
404
393
];
405
394
406
395
self ::$ route [] = $ route ;
407
396
return new self ();
408
397
}
409
398
410
- public function __destruct ()
399
+ public function __destruct ()
411
400
{
412
401
$ route_index = end (self ::$ route );
413
402
$ route_index = is_array ($ route_index ) ? $ route_index [0 ] : $ route_index ;
414
403
415
- $ GLOBALS ['__registered_routes ' ][$ route_index ][
416
- 'caseSensitive '
417
- ] = $ this ->caseSensitive ;
404
+ $ GLOBALS ['__registered_routes ' ][$ route_index ]['caseSensitive ' ] =
405
+ $ this ->caseSensitive ;
418
406
419
- if (self ::$ map !== null )
420
- {
407
+ if (self ::$ map !== null ) {
421
408
$ GLOBALS ['__registered_routes ' ][$ route_index ]['map ' ] = self ::$ map ;
422
409
}
423
410
424
- if ($ this ->guards !== null )
425
- {
411
+ if ($ this ->guards !== null ) {
426
412
$ GLOBALS ['__registered_routes ' ][$ route_index ]['guards ' ] =
427
- $ this ->guards ;
413
+ $ this ->guards ;
428
414
}
429
415
430
- if (self ::$ redirect !== null )
431
- {
416
+ if (self ::$ redirect !== null ) {
432
417
$ GLOBALS ['__registered_routes ' ][$ route_index ]['redirect ' ] =
433
- self ::$ redirect ;
418
+ self ::$ redirect ;
434
419
}
435
420
436
- if ($ this ->action !== null )
437
- {
421
+ if ($ this ->action !== null ) {
438
422
$ GLOBALS ['__registered_routes ' ][$ route_index ]['action ' ] =
439
- $ this ->action ;
423
+ $ this ->action ;
440
424
}
441
425
442
- if ($ this ->mapRoute !== null )
443
- {
426
+ if ($ this ->mapRoute !== null ) {
444
427
$ GLOBALS ['__registered_routes ' ][$ route_index ]['mapRoute ' ] =
445
- $ this ->mapRoute ;
428
+ $ this ->mapRoute ;
446
429
}
447
430
448
- if (self ::$ any !== null )
449
- {
431
+ if (self ::$ any !== null ) {
450
432
$ GLOBALS ['__registered_routes ' ][$ route_index ]['any ' ] = self ::$ any ;
451
433
}
452
434
453
- if ($ this ->use !== null )
454
- {
435
+ if ($ this ->use !== null ) {
455
436
$ GLOBALS ['__registered_routes ' ][$ route_index ]['use ' ] = $ this ->use ;
456
437
}
457
438
458
- if ($ this ->file !== null )
459
- {
439
+ if ($ this ->file !== null ) {
460
440
$ GLOBALS ['__registered_routes ' ][$ route_index ]['file ' ] = $ this ->file ;
461
441
}
462
442
463
- if ($ this ->handleInvalidParameterType !== null )
464
- {
443
+ if ($ this ->handleInvalidParameterType !== null ) {
465
444
$ GLOBALS ['__registered_routes ' ][$ route_index ][
466
- 'handleInvalidParameterType '
445
+ 'handleInvalidParameterType '
467
446
] = $ this ->handleInvalidParameterType ;
468
447
}
469
448
470
- if (self ::$ method !== null )
471
- {
449
+ if (self ::$ method !== null ) {
472
450
$ GLOBALS ['__registered_routes ' ][$ route_index ]['method ' ] =
473
- self ::$ method ;
451
+ self ::$ method ;
474
452
}
475
453
476
- if (self ::$ view !== null )
477
- {
454
+ if (self ::$ view !== null ) {
478
455
$ GLOBALS ['__registered_routes ' ][$ route_index ]['view ' ] = self ::$ view ;
479
456
}
480
457
}
0 commit comments