1717
1818use Closure ;
1919use PhpSlides \Exception ;
20- use PhpSlides \Src \Traits \FileHandler ;
21- use PhpSlides \Src \Controller \Controller ;
20+ use PhpSlides \Core \Traits \FileHandler ;
21+ use PhpSlides \Core \Controller \Controller ;
2222use PhpSlides \Router \Interface \RouteInterface ;
2323
2424/**
@@ -80,15 +80,15 @@ class Route extends Controller implements RouteInterface
8080 *
8181 * ------------------------------------------------------------------------
8282 */
83- public static function any (
84- array |string $ route ,
85- mixed $ callback ,
86- string $ method = '* ' ,
83+ public static function any (
84+ array |string $ route ,
85+ mixed $ callback ,
86+ string $ method = '* ' ,
8787 ): self {
8888 self ::$ any = [
89- 'route ' => $ route ,
90- 'method ' => $ method ,
91- 'callback ' => $ callback ,
89+ 'route ' => $ route ,
90+ 'method ' => $ method ,
91+ 'callback ' => $ callback ,
9292 ];
9393
9494 self ::$ route [] = $ route ;
@@ -103,11 +103,11 @@ public static function any(
103103 * @param string $method Request method
104104 * @param string|array $route Route parameter
105105 */
106- public static function map (string $ method , string |array $ route ): self
106+ public static function map (string $ method , string |array $ route ): self
107107 {
108108 self ::$ map = [
109- 'method ' => $ method ,
110- 'route ' => $ route ,
109+ 'method ' => $ method ,
110+ 'route ' => $ route ,
111111 ];
112112 self ::$ route [] = $ route ;
113113 return new self ();
@@ -119,14 +119,18 @@ public static function map(string $method, string|array $route): self
119119 *
120120 * @param string $name Set the name of the route
121121 */
122- public function name (string $ name ): self
122+ public function name (string $ name ): self
123123 {
124- if (is_array (end (self ::$ route ))) {
125- for ($ i = 0 ; $ i < count (end (self ::$ route )); $ i ++) {
124+ if (is_array (end (self ::$ route )))
125+ {
126+ for ($ i = 0 ; $ i < count (end (self ::$ route )); $ i ++)
127+ {
126128 add_route_name ("$ name:: $ i " , end (self ::$ route )[$ i ]);
127129 self ::$ routes ["$ name:: $ i " ] = end (self ::$ route )[$ i ];
128130 }
129- } else {
131+ }
132+ else
133+ {
130134 add_route_name ($ name , end (self ::$ route ));
131135 self ::$ routes [$ name ] = end (self ::$ route );
132136 }
@@ -140,17 +144,20 @@ public function name(string $name): self
140144 * @param string $route
141145 * @param Closure $callback
142146 */
143- public function route (string $ route , Closure $ callback ): self
147+ public function route (string $ route , Closure $ callback ): self
144148 {
145149 $ route = rtrim ('/ ' , self ::$ map ['route ' ]) . '/ ' . ltrim ('/ ' , $ route );
146150
147- if (self ::$ map ) {
151+ if (self ::$ map )
152+ {
148153 $ this ->mapRoute = [
149- 'route ' => $ route ,
150- 'method ' => self ::$ map ['method ' ],
151- 'callback ' => $ callback ,
154+ 'route ' => $ route ,
155+ 'method ' => self ::$ map ['method ' ],
156+ 'callback ' => $ callback ,
152157 ];
153- } else {
158+ }
159+ else
160+ {
154161 throw new Exception ('There is no map to route. ' );
155162 }
156163 self ::$ route [] = $ route ;
@@ -163,9 +170,10 @@ public function route(string $route, Closure $callback): self
163170 *
164171 * @param mixed $callback
165172 */
166- public function action (mixed $ callback ): self
173+ public function action (mixed $ callback ): self
167174 {
168- if (self ::$ map ) {
175+ if (self ::$ map )
176+ {
169177 $ this ->action = $ callback ;
170178 }
171179 return $ this ;
@@ -180,7 +188,8 @@ public function action(mixed $callback): self
180188 */
181189 public function use (string $ controller ): self
182190 {
183- if (self ::$ map ) {
191+ if (self ::$ map )
192+ {
184193 $ this ->use = $ controller ;
185194 }
186195 return $ this ;
@@ -192,9 +201,10 @@ public function use(string $controller): self
192201 *
193202 * @param string $file
194203 */
195- public function file (string $ file ): self
204+ public function file (string $ file ): self
196205 {
197- if (self ::$ map ) {
206+ if (self ::$ map )
207+ {
198208 $ this ->file = $ file ;
199209 }
200210 return $ this ;
@@ -209,7 +219,7 @@ public function file(string $file): self
209219 * @param Closure $closure The closure to handle invalid parameter types.
210220 * @return self Returns the current instance for method chaining.
211221 */
212- public function handleInvalidParameterType (Closure $ closure ): self
222+ public function handleInvalidParameterType (Closure $ closure ): self
213223 {
214224 $ this ->handleInvalidParameterType = $ closure ;
215225 return $ this ;
@@ -221,9 +231,10 @@ public function handleInvalidParameterType(Closure $closure): self
221231 * @param string ...$guards String parameters of registered guards.
222232 * @return self
223233 */
224- public function withGuard (string ...$ guards ): self
234+ public function withGuard (string ...$ guards ): self
225235 {
226- if (self ::$ map || self ::$ method || self ::$ view ) {
236+ if (self ::$ map || self ::$ method || self ::$ view )
237+ {
227238 $ this ->guards = $ guards ;
228239 }
229240 return $ this ;
@@ -244,11 +255,11 @@ public function withGuard(string ...$guards): self
244255 *
245256 * ---------------------------------------------------------------------------
246257 */
247- public static function view (array |string $ route , string $ view ): self
258+ public static function view (array |string $ route , string $ view ): self
248259 {
249260 self ::$ view = [
250- 'route ' => $ route ,
251- 'view ' => $ view ,
261+ 'route ' => $ route ,
262+ 'view ' => $ view ,
252263 ];
253264
254265 self ::$ route [] = $ route ;
@@ -268,15 +279,15 @@ public static function view(array|string $route, string $view): self
268279 *
269280 * ---------------------------------------------------------------
270281 */
271- public static function redirect (
272- string $ route ,
273- string $ new_url ,
274- int $ code = 302 ,
282+ public static function redirect (
283+ string $ route ,
284+ string $ new_url ,
285+ int $ code = 302 ,
275286 ): self {
276287 self ::$ redirect = [
277- 'route ' => $ route ,
278- 'new_url ' => $ new_url ,
279- 'code ' => $ code ,
288+ 'route ' => $ route ,
289+ 'new_url ' => $ new_url ,
290+ 'code ' => $ code ,
280291 ];
281292
282293 self ::$ route [] = $ route ;
@@ -292,12 +303,12 @@ public static function redirect(
292303 *
293304 * --------------------------------------------------------------
294305 */
295- public static function get (array |string $ route , $ callback ): self
306+ public static function get (array |string $ route , $ callback ): self
296307 {
297308 self ::$ method = [
298- 'route ' => $ route ,
299- 'method ' => 'GET ' ,
300- 'callback ' => $ callback ,
309+ 'route ' => $ route ,
310+ 'method ' => 'GET ' ,
311+ 'callback ' => $ callback ,
301312 ];
302313
303314 self ::$ route [] = $ route ;
@@ -313,12 +324,12 @@ public static function get(array|string $route, $callback): self
313324 *
314325 * --------------------------------------------------------------
315326 */
316- public static function post (array |string $ route , $ callback ): self
327+ public static function post (array |string $ route , $ callback ): self
317328 {
318329 self ::$ method = [
319- 'route ' => $ route ,
320- 'method ' => 'POST ' ,
321- 'callback ' => $ callback ,
330+ 'route ' => $ route ,
331+ 'method ' => 'POST ' ,
332+ 'callback ' => $ callback ,
322333 ];
323334
324335 self ::$ route [] = $ route ;
@@ -334,12 +345,12 @@ public static function post(array|string $route, $callback): self
334345 *
335346 * --------------------------------------------------------------
336347 */
337- public static function put (array |string $ route , $ callback ): self
348+ public static function put (array |string $ route , $ callback ): self
338349 {
339350 self ::$ method = [
340- 'route ' => $ route ,
341- 'method ' => 'PUT ' ,
342- 'callback ' => $ callback ,
351+ 'route ' => $ route ,
352+ 'method ' => 'PUT ' ,
353+ 'callback ' => $ callback ,
343354 ];
344355
345356 self ::$ route [] = $ route ;
@@ -355,12 +366,12 @@ public static function put(array|string $route, $callback): self
355366 *
356367 * --------------------------------------------------------------
357368 */
358- public static function patch (array |string $ route , $ callback ): self
369+ public static function patch (array |string $ route , $ callback ): self
359370 {
360371 self ::$ method = [
361- 'route ' => $ route ,
362- 'method ' => 'PATCH ' ,
363- 'callback ' => $ callback ,
372+ 'route ' => $ route ,
373+ 'method ' => 'PATCH ' ,
374+ 'callback ' => $ callback ,
364375 ];
365376
366377 self ::$ route [] = $ route ;
@@ -376,71 +387,82 @@ public static function patch(array|string $route, $callback): self
376387 *
377388 * --------------------------------------------------------------
378389 */
379- public static function delete (array |string $ route , $ callback ): self
390+ public static function delete (array |string $ route , $ callback ): self
380391 {
381392 self ::$ method = [
382- 'route ' => $ route ,
383- 'method ' => 'DELETE ' ,
384- 'callback ' => $ callback ,
393+ 'route ' => $ route ,
394+ 'method ' => 'DELETE ' ,
395+ 'callback ' => $ callback ,
385396 ];
386397
387398 self ::$ route [] = $ route ;
388399 return new self ();
389400 }
390401
391- public function __destruct ()
402+ public function __destruct ()
392403 {
393404 $ route_index = end (self ::$ route );
394405 $ route_index = is_array ($ route_index ) ? $ route_index [0 ] : $ route_index ;
395406
396- if (self ::$ map !== null ) {
407+ if (self ::$ map !== null )
408+ {
397409 $ GLOBALS ['__registered_routes ' ][$ route_index ]['map ' ] = self ::$ map ;
398410 }
399411
400- if ($ this ->guards !== null ) {
412+ if ($ this ->guards !== null )
413+ {
401414 $ GLOBALS ['__registered_routes ' ][$ route_index ]['guards ' ] =
402- $ this ->guards ;
415+ $ this ->guards ;
403416 }
404417
405- if (self ::$ redirect !== null ) {
418+ if (self ::$ redirect !== null )
419+ {
406420 $ GLOBALS ['__registered_routes ' ][$ route_index ]['redirect ' ] =
407- self ::$ redirect ;
421+ self ::$ redirect ;
408422 }
409423
410- if ($ this ->action !== null ) {
424+ if ($ this ->action !== null )
425+ {
411426 $ GLOBALS ['__registered_routes ' ][$ route_index ]['action ' ] =
412- $ this ->action ;
427+ $ this ->action ;
413428 }
414429
415- if ($ this ->mapRoute !== null ) {
430+ if ($ this ->mapRoute !== null )
431+ {
416432 $ GLOBALS ['__registered_routes ' ][$ route_index ]['mapRoute ' ] =
417- $ this ->mapRoute ;
433+ $ this ->mapRoute ;
418434 }
419435
420- if (self ::$ any !== null ) {
436+ if (self ::$ any !== null )
437+ {
421438 $ GLOBALS ['__registered_routes ' ][$ route_index ]['any ' ] = self ::$ any ;
422439 }
423440
424- if ($ this ->use !== null ) {
441+ if ($ this ->use !== null )
442+ {
425443 $ GLOBALS ['__registered_routes ' ][$ route_index ]['use ' ] = $ this ->use ;
426444 }
427445
428- if ($ this ->file !== null ) {
446+ if ($ this ->file !== null )
447+ {
429448 $ GLOBALS ['__registered_routes ' ][$ route_index ]['file ' ] = $ this ->file ;
430449 }
431450
432- if ($ this ->handleInvalidParameterType !== null ) {
451+ if ($ this ->handleInvalidParameterType !== null )
452+ {
433453 $ GLOBALS ['__registered_routes ' ][$ route_index ][
434- 'handleInvalidParameterType '
454+ 'handleInvalidParameterType '
435455 ] = $ this ->handleInvalidParameterType ;
436456 }
437457
438- if (self ::$ method !== null ) {
458+ if (self ::$ method !== null )
459+ {
439460 $ GLOBALS ['__registered_routes ' ][$ route_index ]['method ' ] =
440- self ::$ method ;
461+ self ::$ method ;
441462 }
442463
443- if (self ::$ view !== null ) {
464+ if (self ::$ view !== null )
465+ {
444466 $ GLOBALS ['__registered_routes ' ][$ route_index ]['view ' ] = self ::$ view ;
445467 }
446468 }
0 commit comments