2
2
3
3
namespace LionRoute ;
4
4
5
+ use Phroute \Phroute \{ RouteCollector , RouteParser , Dispatcher };
6
+ use Phroute \Phroute \Exception \{ HttpRouteNotFoundException , HttpMethodNotAllowedException };
5
7
use LionRoute \Middleware ;
6
8
7
9
class Route {
8
10
9
11
private static $ router ;
10
- private static array $ class_map ;
11
12
12
13
public function __construct () {
13
14
14
15
}
15
16
16
- public static function init (array $ filters ): void {
17
- self ::$ class_map = $ filters ['class ' ];
18
- $ parser = isset (self ::$ class_map ['RouteParser ' ]) ? new self::$ class_map ['RouteParser ' ]() : null ;
19
- self ::$ router = new self::$ class_map ['RouteCollector ' ]($ parser );
17
+ public static function init (array $ filters = []): void {
18
+ self ::$ router = new RouteCollector (new RouteParser ());
20
19
self ::createMiddleware (isset ($ filters ['middleware ' ]) ? $ filters ['middleware ' ] : []);
21
20
}
22
21
23
- public static function createMiddleware (array $ filters ): void {
22
+ public static function newMiddleware (string $ middlewareName , string $ objectClass , string $ methodClass ): Middleware {
23
+ return new Middleware ($ middlewareName , $ objectClass , $ methodClass );
24
+ }
25
+
26
+ private static function createMiddleware (array $ filters ): void {
24
27
if (count ($ filters ) > 0 ) {
25
28
foreach ($ filters as $ key => $ obj ) {
26
29
self ::$ router ->filter ($ obj ->getMiddlewareName (), function () use ($ obj ) {
@@ -40,24 +43,27 @@ public static function prefix(string $prefix_name, \Closure $closure): void {
40
43
}
41
44
42
45
public static function middleware (array $ middleware , \Closure $ closure ): void {
43
- self ::$ router ->group ($ middleware , function ($ router ) use ($ closure ) {
46
+ $ count = count ($ middleware );
47
+ $ list_middleware = [];
48
+
49
+ if ($ count === 1 ) {
50
+ $ list_middleware = ['before ' => $ middleware [0 ]];
51
+ } elseif ($ count === 2 ) {
52
+ $ list_middleware = ['before ' => $ middleware [0 ], 'after ' => $ middleware [1 ]];
53
+ } elseif ($ count >= 3 ) {
54
+ $ list_middleware = ['before ' => $ middleware [0 ], 'after ' => $ middleware [1 ], 'prefix ' => $ middleware [2 ]];
55
+ }
56
+
57
+ self ::$ router ->group ($ list_middleware , function ($ router ) use ($ closure ) {
44
58
$ closure ();
45
59
});
46
60
}
47
61
48
- public static function any (string $ url , \Closure |array $ controller_function , array $ filters = []): void {
49
- if (count ($ filters ) > 0 ) {
50
- self ::$ router ->any ($ url , $ controller_function , $ filters );
51
- } else {
52
- self ::$ router ->any ($ url , $ controller_function );
53
- }
54
- }
55
-
56
- public static function delete (string $ url , \Closure |array $ controller_function , array $ filters = []): void {
62
+ public static function get (string $ url , \Closure |array $ controller_function , array $ filters = []): void {
57
63
if (count ($ filters ) > 0 ) {
58
- self ::$ router ->delete ($ url , $ controller_function , $ filters );
64
+ self ::$ router ->get ($ url , $ controller_function , $ filters );
59
65
} else {
60
- self ::$ router ->delete ($ url , $ controller_function );
66
+ self ::$ router ->get ($ url , $ controller_function );
61
67
}
62
68
}
63
69
@@ -77,16 +83,20 @@ public static function put(string $url, \Closure|array $controller_function, arr
77
83
}
78
84
}
79
85
80
- public static function get (string $ url , \Closure |array $ controller_function , array $ filters = []): void {
86
+ public static function delete (string $ url , \Closure |array $ controller_function , array $ filters = []): void {
81
87
if (count ($ filters ) > 0 ) {
82
- self ::$ router ->get ($ url , $ controller_function , $ filters );
88
+ self ::$ router ->delete ($ url , $ controller_function , $ filters );
83
89
} else {
84
- self ::$ router ->get ($ url , $ controller_function );
90
+ self ::$ router ->delete ($ url , $ controller_function );
85
91
}
86
92
}
87
93
88
- public static function newMiddleware (string $ middlewareName , string $ objectClass , string $ methodClass ): Middleware {
89
- return new Middleware ($ middlewareName , $ objectClass , $ methodClass );
94
+ public static function any (string $ url , \Closure |array $ controller_function , array $ filters = []): void {
95
+ if (count ($ filters ) > 0 ) {
96
+ self ::$ router ->any ($ url , $ controller_function , $ filters );
97
+ } else {
98
+ self ::$ router ->any ($ url , $ controller_function );
99
+ }
90
100
}
91
101
92
102
private static function processInput ($ index ): string {
@@ -99,12 +109,14 @@ public static function processOutput($response): void {
99
109
100
110
public static function dispatch ($ index ) {
101
111
try {
102
- return (new self:: $ class_map [ ' Dispatcher ' ] (self ::$ router ->getData ()))->dispatch (
112
+ return (new Dispatcher (self ::$ router ->getData ()))->dispatch (
103
113
$ _SERVER ['REQUEST_METHOD ' ],
104
114
self ::processInput ($ index )
105
115
);
106
- } catch (\Exception $ e ) {
107
- return ['status ' => "error " , 'message ' => $ e ->getMessage ()];
116
+ } catch (HttpRouteNotFoundException $ e ) {
117
+ return ['status ' => "error " , 'message ' => "Path not found: {$ e ->getMessage ()}" ];
118
+ } catch (HttpMethodNotAllowedException $ e ) {
119
+ return ['status ' => "error " , 'message ' => "Method not allowed: {$ e ->getMessage ()}" ];
108
120
}
109
121
}
110
122
0 commit comments