5
5
6
6
use Closure ;
7
7
use Exception ;
8
+ use PhpParser \Builder \Class_ ;
8
9
use ReflectionException ;
9
10
use Rodri \SimpleRouter \Exceptions \ControllerMethodNotFoundException ;
10
- use Rodri \SimpleRouter \Exceptions \SimpleRouterException ;
11
- use Rodri \SimpleRouter \Handlers \GroupHttpHandler ;
12
11
use Rodri \SimpleRouter \Handlers \HttpHandler ;
13
12
use Rodri \SimpleRouter \Handlers \RouterHandler ;
14
13
use Rodri \SimpleRouter \Helpers \StatusCode ;
@@ -25,11 +24,15 @@ class Router
25
24
private ?RouterHandler $ baseRouterHandler ;
26
25
private string $ controllerNamespace ;
27
26
private bool $ debugMode ;
27
+ private ?string $ baseUrl = null ;
28
+
29
+ private array $ groupRouter ;
28
30
29
31
public function __construct ()
30
32
{
31
33
$ this ->baseRouterHandler = null ;
32
34
$ this ->debugMode = false ;
35
+ $ this ->groupRouter = array ();
33
36
}
34
37
35
38
/**
@@ -83,10 +86,30 @@ public function addRouterHandler(RouterHandler $routerHandler): void
83
86
}
84
87
85
88
/**
86
- * @throws Exception
89
+ * Add a new Router to group array
90
+ * @param Router $router
91
+ */
92
+ public function addRouterGroup (Router $ router ): void
93
+ {
94
+ $ this ->groupRouter [] = $ router ;
95
+ }
96
+
97
+ /**
98
+ * Group the routes to work with the same base URI. The route in group
99
+ * can have the same middleware defined.
100
+ * @param array $routerOptions Router options [0] => '/router' ['middleware' => Middleware
101
+ * @param Closure $closure function(Router $router)
87
102
*/
88
103
public function group (array $ routerOptions , Closure $ closure ): void
89
104
{
105
+ $ router = clone $ this ;
106
+ $ router ->baseUrl = null ;
107
+ $ router ->baseUrl = $ routerOptions [0 ];
108
+
109
+ # Pass to closure function a instate of route
110
+ $ closure ($ router );
111
+
112
+ $ this ->addRouterGroup ($ router );
90
113
}
91
114
92
115
/**
@@ -98,7 +121,7 @@ public function get(array $routerOptions, string $controller): void
98
121
{
99
122
$ this ->addRouterHandler (
100
123
new HttpHandler (
101
- $ routerOptions [0 ],
124
+ $ this -> buildURI ( $ routerOptions [0 ]) ,
102
125
$ this ->concatControllerAndNamespace ($ controller ),
103
126
'GET '
104
127
)
@@ -114,7 +137,7 @@ public function post(array $routerOptions, string $controller): void
114
137
{
115
138
$ this ->addRouterHandler (
116
139
new HttpHandler (
117
- $ routerOptions [0 ],
140
+ $ this -> buildURI ( $ routerOptions [0 ]) ,
118
141
$ this ->concatControllerAndNamespace ($ controller ),
119
142
'POST '
120
143
)
@@ -130,7 +153,7 @@ public function put(array $routerOptions, string $controller): void
130
153
{
131
154
$ this ->addRouterHandler (
132
155
new HttpHandler (
133
- $ routerOptions [0 ],
156
+ $ this -> buildURI ( $ routerOptions [0 ]) ,
134
157
$ this ->concatControllerAndNamespace ($ controller ),
135
158
'PUT '
136
159
)
@@ -146,7 +169,7 @@ public function patch(array $routerOptions, string $controller): void
146
169
{
147
170
$ this ->addRouterHandler (
148
171
new HttpHandler (
149
- $ routerOptions [0 ],
172
+ $ this -> buildURI ( $ routerOptions [0 ]) ,
150
173
$ this ->concatControllerAndNamespace ($ controller ),
151
174
'PATCH '
152
175
)
@@ -162,7 +185,7 @@ public function delete(array $routerOptions, string $controller): void
162
185
{
163
186
$ this ->addRouterHandler (
164
187
new HttpHandler (
165
- $ routerOptions [0 ],
188
+ $ this -> buildURI ( $ routerOptions [0 ]) ,
166
189
$ this ->concatControllerAndNamespace ($ controller ),
167
190
'DELETE '
168
191
)
@@ -172,11 +195,15 @@ public function delete(array $routerOptions, string $controller): void
172
195
/**
173
196
* Dispatch to router work call the expected handle.
174
197
*/
175
- public function dispatch ()
198
+ public function dispatch (): void
176
199
{
200
+
201
+ # If have router grouped routes
202
+ if ($ this ->dispatchGroupRoutes ()) return ;
203
+
177
204
try {
178
205
echo $ this ->baseRouterHandler ->handle (new Request ());
179
- } catch (ControllerMethodNotFoundException | ReflectionException $ e ) {
206
+ } catch (ControllerMethodNotFoundException | ReflectionException $ e ) {
180
207
if ($ this ->debugMode ) {
181
208
echo new Response ([
182
209
'Mode ' => 'Debug ' ,
@@ -194,6 +221,26 @@ public function dispatch()
194
221
flush ();
195
222
}
196
223
224
+ /**
225
+ * Dispatch all grouped routes.
226
+ *
227
+ * @return bool
228
+ */
229
+ private function dispatchGroupRoutes (): bool
230
+ {
231
+ foreach ($ this ->groupRouter as $ groupRouter ) {
232
+ if ($ groupRouter instanceof Router) {
233
+ $ response = $ groupRouter ->baseRouterHandler ->handle (new Request ());
234
+ if ($ response ->hasResponseValue ()) {
235
+ echo $ response ;
236
+ return true ;
237
+ }
238
+ }
239
+ }
240
+
241
+ return false ;
242
+ }
243
+
197
244
/**
198
245
* @param string $controller
199
246
* @return string
@@ -202,4 +249,18 @@ private function concatControllerAndNamespace(string $controller): string
202
249
{
203
250
return $ this ->controllerNamespace . '\\' . $ controller ;
204
251
}
252
+
253
+ /**
254
+ * Build a URI with a base URL if exist
255
+ * @param String $route
256
+ * @return String
257
+ */
258
+ private function buildURI (String $ route ): String
259
+ {
260
+ if (isset ($ this ->baseUrl )) {
261
+ $ route = $ this ->baseUrl .$ route ;
262
+ }
263
+
264
+ return $ route ;
265
+ }
205
266
}
0 commit comments