mirror of
https://github.com/notrab/dumbo.git
synced 2025-01-17 06:08:31 +01:00
fix nested route middleware
This commit is contained in:
parent
c2d6c9e83a
commit
61892adc06
@ -108,6 +108,10 @@ $app->use(function($c, $next) {
|
|||||||
|
|
||||||
### Bearer Auth
|
### Bearer Auth
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -H 'Authorization: Bearer mysupersecret' http://localhost:8000/api
|
||||||
|
```
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
@ -11,13 +11,7 @@ $user = new Dumbo();
|
|||||||
$protectedRoutes = new Dumbo();
|
$protectedRoutes = new Dumbo();
|
||||||
$token = "mysupersecret";
|
$token = "mysupersecret";
|
||||||
|
|
||||||
$protectedRoutes->use(BearerAuth::bearer($token));
|
$app->use(BearerAuth::bearer($token));
|
||||||
|
|
||||||
$protectedRoutes->get("/", function ($c) {
|
|
||||||
return $c->json(["message" => "Welcome to the protected routes!"]);
|
|
||||||
});
|
|
||||||
|
|
||||||
$app->route("/api", $protectedRoutes);
|
|
||||||
|
|
||||||
$userData = [
|
$userData = [
|
||||||
[
|
[
|
||||||
|
@ -68,6 +68,16 @@ class Dumbo
|
|||||||
$this->middleware[] = $middleware;
|
$this->middleware[] = $middleware;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the middleware stack
|
||||||
|
*
|
||||||
|
* @return array The middleware stack
|
||||||
|
*/
|
||||||
|
public function getMiddleware(): array
|
||||||
|
{
|
||||||
|
return $this->middleware;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a nested route to the application
|
* Add a nested route to the application
|
||||||
*
|
*
|
||||||
@ -77,10 +87,16 @@ class Dumbo
|
|||||||
public function route(string $prefix, self $nestedApp): void
|
public function route(string $prefix, self $nestedApp): void
|
||||||
{
|
{
|
||||||
foreach ($nestedApp->router->getRoutes() as $route) {
|
foreach ($nestedApp->router->getRoutes() as $route) {
|
||||||
|
$combinedMiddleware = array_merge(
|
||||||
|
$this->middleware,
|
||||||
|
$nestedApp->getMiddleware()
|
||||||
|
);
|
||||||
|
|
||||||
$this->router->addRoute(
|
$this->router->addRoute(
|
||||||
$route["method"],
|
$route["method"],
|
||||||
$prefix . $route["path"],
|
$prefix . $route["path"],
|
||||||
$route["handler"]
|
$route["handler"],
|
||||||
|
$combinedMiddleware
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -101,7 +117,17 @@ class Dumbo
|
|||||||
$route["params"],
|
$route["params"],
|
||||||
$route["routePath"]
|
$route["routePath"]
|
||||||
);
|
);
|
||||||
$response = $this->runMiddleware($context, $route["handler"]);
|
|
||||||
|
$combinedMiddleware = array_merge(
|
||||||
|
$this->middleware,
|
||||||
|
$route["middleware"] ?? []
|
||||||
|
);
|
||||||
|
|
||||||
|
$response = $this->runMiddleware(
|
||||||
|
$context,
|
||||||
|
$route["handler"],
|
||||||
|
$combinedMiddleware
|
||||||
|
);
|
||||||
|
|
||||||
return $response instanceof ResponseInterface
|
return $response instanceof ResponseInterface
|
||||||
? $response
|
? $response
|
||||||
@ -122,6 +148,11 @@ class Dumbo
|
|||||||
$this->send($response);
|
$this->send($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a server request from the PHP globals
|
||||||
|
*
|
||||||
|
* @return ServerRequestInterface The server request
|
||||||
|
*/
|
||||||
private function createServerRequestFromGlobals(): ServerRequestInterface
|
private function createServerRequestFromGlobals(): ServerRequestInterface
|
||||||
{
|
{
|
||||||
$request = ServerRequest::fromGlobals();
|
$request = ServerRequest::fromGlobals();
|
||||||
|
@ -6,6 +6,12 @@ use Dumbo\Context;
|
|||||||
|
|
||||||
class BearerAuth
|
class BearerAuth
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Create a middleware that checks for a Bearer token in the Authorization header
|
||||||
|
*
|
||||||
|
* @param string $token The expected Bearer token
|
||||||
|
* @return callable The middleware
|
||||||
|
*/
|
||||||
public static function bearer(string $token): callable
|
public static function bearer(string $token): callable
|
||||||
{
|
{
|
||||||
return function (Context $ctx, callable $next) use ($token) {
|
return function (Context $ctx, callable $next) use ($token) {
|
||||||
|
@ -6,21 +6,31 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||||||
|
|
||||||
class Router
|
class Router
|
||||||
{
|
{
|
||||||
/** @var array<array{method: string, path: string, handler: callable(Context): (ResponseInterface|null)}> */
|
/** @var array<array{method: string, path: string, handler: callable(Context): (ResponseInterface|null), middleware: array}> */
|
||||||
private $routes = [];
|
private $routes = [];
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $prefix = "";
|
private $prefix = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a route to the router
|
||||||
|
*
|
||||||
|
* @param string $method The HTTP method for this route
|
||||||
|
* @param string $path The path for this route
|
||||||
|
* @param callable(Context): (ResponseInterface|null) $handler The handler function for this route
|
||||||
|
* @param array $middleware Array of middleware functions for this route
|
||||||
|
*/
|
||||||
public function addRoute(
|
public function addRoute(
|
||||||
string $method,
|
string $method,
|
||||||
string $path,
|
string $path,
|
||||||
callable $handler
|
callable $handler,
|
||||||
|
array $middleware = []
|
||||||
): void {
|
): void {
|
||||||
$this->routes[] = [
|
$this->routes[] = [
|
||||||
"method" => $method,
|
"method" => $method,
|
||||||
"path" => $this->prefix . $path,
|
"path" => $this->prefix . $path,
|
||||||
"handler" => $handler,
|
"handler" => $handler,
|
||||||
|
"middleware" => $middleware,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user