Skip to content

Commit ea8a6ce

Browse files
committed
UPDATE - Group routers
1 parent c6e0b08 commit ea8a6ce

File tree

6 files changed

+128
-34
lines changed

6 files changed

+128
-34
lines changed

public/index.php

+2-22
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,6 @@
22

33
require "../vendor/autoload.php";
44

5-
$router = new \Rodri\SimpleRouter\Router();
5+
# Routes of application
6+
require_once '../routes/routes.php';
67

7-
# Namespaces Configurations
8-
$router->setControllerNamespace('Rodri\SimpleRouter\Controllers');
9-
$router->setMiddlewareNamespace('Rodri\SimpleRouter\Middlewares');
10-
11-
$router->debug(true);
12-
13-
# Header Router Configurations
14-
$router->headerConfigs([
15-
'Content-type: application/json'
16-
]);
17-
18-
# Routers
19-
$router->get(['/hello'], 'HelloControllerExample#hello');
20-
$router->get(['/hello/message/:id'], 'HelloControllerExample#helloByMessage');
21-
22-
$router->post(['/post'], 'HelloControllerExample#postTest');
23-
24-
// TODO: Group router
25-
26-
# Execution of set router
27-
$router->dispatch();

routes/routes.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Router file - Here you can set all your application routes.
4+
* Remember to change debug value to false if not in development mode.
5+
*
6+
* @author Rodrigo Andrade
7+
* @since 2021-07-10
8+
* @version 1.0.0
9+
*/
10+
11+
use Rodri\SimpleRouter\Router;
12+
13+
$router = new Router();
14+
15+
# Namespaces Configurations
16+
$router->setControllerNamespace('Rodri\SimpleRouter\Controllers');
17+
$router->setMiddlewareNamespace('Rodri\SimpleRouter\Middlewares');
18+
19+
# Debug mode
20+
$router->debug(true);
21+
22+
# Header Router Configurations
23+
$router->headerConfigs([
24+
'Content-type: application/json'
25+
]);
26+
27+
# Routers without group
28+
$router->get(['/hello'], 'HelloControllerExample#hello');
29+
$router->get(['/hello/message/:id'], 'HelloControllerExample#helloByMessage');
30+
$router->post(['/post'], 'HelloControllerExample#postTest');
31+
32+
# Router with group
33+
$router->group(['/group/test'], function (Router $router) {
34+
$router->get([''], 'HelloControllerExample#hello');
35+
$router->get(['/:id'], 'HelloControllerExample#helloByMessage');
36+
$router->post([''], 'HelloControllerExample#postTest');
37+
});
38+
39+
$router->group(['/group'], function (Router $router) {
40+
$router->get([''], 'HelloControllerExample#hello');
41+
$router->get(['/:id'], 'HelloControllerExample#helloByMessage');
42+
$router->post([''], 'HelloControllerExample#postTest');
43+
});
44+
45+
# Execution of set router
46+
$router->dispatch();

src/Exceptions/ControllerMethodNotFoundException.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
class ControllerMethodNotFoundException extends \RuntimeException
88
{
9-
109
}

src/Exceptions/SimpleRouterException.php

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88

99
class SimpleRouterException extends ReflectionException
1010
{
11-
1211
}

src/Response.php

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ public function __construct(
2222
header($statusCode);
2323
}
2424

25+
/**
26+
* Check if has some response value.
27+
* @return bool
28+
*/
29+
public function hasResponseValue(): bool
30+
{
31+
return $this->response != null;
32+
}
33+
2534
public function __toString(): string
2635
{
2736
if($this->response != null)

src/Router.php

+71-10
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
use Closure;
77
use Exception;
8+
use PhpParser\Builder\Class_;
89
use ReflectionException;
910
use Rodri\SimpleRouter\Exceptions\ControllerMethodNotFoundException;
10-
use Rodri\SimpleRouter\Exceptions\SimpleRouterException;
11-
use Rodri\SimpleRouter\Handlers\GroupHttpHandler;
1211
use Rodri\SimpleRouter\Handlers\HttpHandler;
1312
use Rodri\SimpleRouter\Handlers\RouterHandler;
1413
use Rodri\SimpleRouter\Helpers\StatusCode;
@@ -25,11 +24,15 @@ class Router
2524
private ?RouterHandler $baseRouterHandler;
2625
private string $controllerNamespace;
2726
private bool $debugMode;
27+
private ?string $baseUrl = null;
28+
29+
private array $groupRouter;
2830

2931
public function __construct()
3032
{
3133
$this->baseRouterHandler = null;
3234
$this->debugMode = false;
35+
$this->groupRouter = array();
3336
}
3437

3538
/**
@@ -83,10 +86,30 @@ public function addRouterHandler(RouterHandler $routerHandler): void
8386
}
8487

8588
/**
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)
87102
*/
88103
public function group(array $routerOptions, Closure $closure): void
89104
{
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);
90113
}
91114

92115
/**
@@ -98,7 +121,7 @@ public function get(array $routerOptions, string $controller): void
98121
{
99122
$this->addRouterHandler(
100123
new HttpHandler(
101-
$routerOptions[0],
124+
$this->buildURI($routerOptions[0]),
102125
$this->concatControllerAndNamespace($controller),
103126
'GET'
104127
)
@@ -114,7 +137,7 @@ public function post(array $routerOptions, string $controller): void
114137
{
115138
$this->addRouterHandler(
116139
new HttpHandler(
117-
$routerOptions[0],
140+
$this->buildURI($routerOptions[0]),
118141
$this->concatControllerAndNamespace($controller),
119142
'POST'
120143
)
@@ -130,7 +153,7 @@ public function put(array $routerOptions, string $controller): void
130153
{
131154
$this->addRouterHandler(
132155
new HttpHandler(
133-
$routerOptions[0],
156+
$this->buildURI($routerOptions[0]),
134157
$this->concatControllerAndNamespace($controller),
135158
'PUT'
136159
)
@@ -146,7 +169,7 @@ public function patch(array $routerOptions, string $controller): void
146169
{
147170
$this->addRouterHandler(
148171
new HttpHandler(
149-
$routerOptions[0],
172+
$this->buildURI($routerOptions[0]),
150173
$this->concatControllerAndNamespace($controller),
151174
'PATCH'
152175
)
@@ -162,7 +185,7 @@ public function delete(array $routerOptions, string $controller): void
162185
{
163186
$this->addRouterHandler(
164187
new HttpHandler(
165-
$routerOptions[0],
188+
$this->buildURI($routerOptions[0]),
166189
$this->concatControllerAndNamespace($controller),
167190
'DELETE'
168191
)
@@ -172,11 +195,15 @@ public function delete(array $routerOptions, string $controller): void
172195
/**
173196
* Dispatch to router work call the expected handle.
174197
*/
175-
public function dispatch()
198+
public function dispatch(): void
176199
{
200+
201+
# If have router grouped routes
202+
if($this->dispatchGroupRoutes()) return;
203+
177204
try {
178205
echo $this->baseRouterHandler->handle(new Request());
179-
} catch (ControllerMethodNotFoundException|ReflectionException $e) {
206+
} catch (ControllerMethodNotFoundException | ReflectionException $e) {
180207
if ($this->debugMode) {
181208
echo new Response([
182209
'Mode' => 'Debug',
@@ -194,6 +221,26 @@ public function dispatch()
194221
flush();
195222
}
196223

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+
197244
/**
198245
* @param string $controller
199246
* @return string
@@ -202,4 +249,18 @@ private function concatControllerAndNamespace(string $controller): string
202249
{
203250
return $this->controllerNamespace . '\\' . $controller;
204251
}
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+
}
205266
}

0 commit comments

Comments
 (0)