mirror of
https://github.com/notrab/dumbo.git
synced 2025-01-17 14:18:14 +01:00
path and pathName helpers
This commit is contained in:
parent
12813172a2
commit
129f87270c
@ -29,27 +29,59 @@ class Context
|
|||||||
*
|
*
|
||||||
* @param ServerRequestInterface $request The server request object
|
* @param ServerRequestInterface $request The server request object
|
||||||
* @param array $params The route parameters
|
* @param array $params The route parameters
|
||||||
|
* @param string $routePath The registered route path
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ServerRequestInterface $request,
|
private ServerRequestInterface $request,
|
||||||
private array $params
|
private array $params,
|
||||||
|
private string $routePath
|
||||||
) {
|
) {
|
||||||
$this->response = new Response();
|
$this->response = new Response();
|
||||||
$this->req = new class ($request, $params) implements RequestWrapper {
|
$this->req = new class ($request, $params, $routePath) implements
|
||||||
|
RequestWrapper
|
||||||
|
{
|
||||||
/** @var array The parsed request body */
|
/** @var array The parsed request body */
|
||||||
private array $parsedBody;
|
private array $parsedBody;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ServerRequestInterface $request The server request object
|
* @param ServerRequestInterface $request The server request object
|
||||||
* @param array $params The route parameters
|
* @param array $params The route parameters
|
||||||
|
* @param string $routePath The registered route path
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ServerRequestInterface $request,
|
private ServerRequestInterface $request,
|
||||||
private array $params
|
private array $params,
|
||||||
|
private string $routePath
|
||||||
) {
|
) {
|
||||||
$this->parsedBody = $this->parseBody();
|
$this->parsedBody = $this->parseBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current request path
|
||||||
|
*
|
||||||
|
* This method returns the path component of the request URI.
|
||||||
|
* The path will be normalized to remove trailing slashes, except for the root path.
|
||||||
|
*
|
||||||
|
* @return string The current request path
|
||||||
|
*/
|
||||||
|
public function path(): string
|
||||||
|
{
|
||||||
|
return $this->request->getUri()->getPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the registered route path for the current request
|
||||||
|
*
|
||||||
|
* This method returns the original route path as it was registered,
|
||||||
|
* including any path parameters (e.g., '/posts/:id').
|
||||||
|
*
|
||||||
|
* @return string The registered route path
|
||||||
|
*/
|
||||||
|
public function routePath(): string
|
||||||
|
{
|
||||||
|
return $this->routePath;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the request body based on content type
|
* Parse the request body based on content type
|
||||||
*
|
*
|
||||||
@ -96,6 +128,22 @@ class Context
|
|||||||
return $this->params[$name] ?? null;
|
return $this->params[$name] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get query parameters as an array
|
||||||
|
*
|
||||||
|
* If the parameter is a comma-separated string, it will be split into an array.
|
||||||
|
* If the parameter is already an array, it will be returned as is.
|
||||||
|
*
|
||||||
|
* @param string $name The name of the query parameter
|
||||||
|
* @return array The query parameter value(s) as an array
|
||||||
|
*/
|
||||||
|
public function queries(string $name): array
|
||||||
|
{
|
||||||
|
$query = $this->request->getQueryParams();
|
||||||
|
$value = $query[$name] ?? "";
|
||||||
|
return is_array($value) ? $value : explode(",", $value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get query parameters
|
* Get query parameters
|
||||||
*
|
*
|
||||||
@ -141,6 +189,21 @@ class Context
|
|||||||
? $this->request->getHeaders()
|
? $this->request->getHeaders()
|
||||||
: $this->request->getHeader($name);
|
: $this->request->getHeader($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a single header value
|
||||||
|
*
|
||||||
|
* Returns the first value of the specified header. If the header has multiple
|
||||||
|
* values, only the first one is returned. If the header doesn't exist, null is returned.
|
||||||
|
*
|
||||||
|
* @param string $name The name of the header
|
||||||
|
* @return string|null The header value, or null if the header doesn't exist
|
||||||
|
*/
|
||||||
|
public function header(string $name): ?string
|
||||||
|
{
|
||||||
|
$headers = $this->request->getHeader($name);
|
||||||
|
return !empty($headers) ? $headers[0] : null;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,7 +338,7 @@ class Context
|
|||||||
*
|
*
|
||||||
* @return ResponseInterface The response object
|
* @return ResponseInterface The response object
|
||||||
*/
|
*/
|
||||||
protected function getResponse(): ResponseInterface
|
public function getResponse(): ResponseInterface
|
||||||
{
|
{
|
||||||
return $this->response;
|
return $this->response;
|
||||||
}
|
}
|
||||||
|
@ -120,7 +120,7 @@ class Dumbo
|
|||||||
$this->matchPath($route["path"], $path)
|
$this->matchPath($route["path"], $path)
|
||||||
) {
|
) {
|
||||||
$params = $this->extractParams($route["path"], $path);
|
$params = $this->extractParams($route["path"], $path);
|
||||||
$context = new Context($request, $params);
|
$context = new Context($request, $params, $route["path"]);
|
||||||
|
|
||||||
$response = $this->runMiddleware($context, $route["handler"]);
|
$response = $this->runMiddleware($context, $route["handler"]);
|
||||||
|
|
||||||
|
@ -8,8 +8,12 @@ namespace Dumbo;
|
|||||||
interface RequestWrapper
|
interface RequestWrapper
|
||||||
{
|
{
|
||||||
public function param(string $name): ?string;
|
public function param(string $name): ?string;
|
||||||
|
public function queries(string $name): array;
|
||||||
public function query(?string $name = null): array|string|null;
|
public function query(?string $name = null): array|string|null;
|
||||||
public function body(): array;
|
public function body(): array;
|
||||||
public function method(): string;
|
public function method(): string;
|
||||||
public function headers(?string $name = null): array;
|
public function headers(?string $name = null): array;
|
||||||
|
public function header(string $name): ?string;
|
||||||
|
public function path(): string;
|
||||||
|
public function routePath(): string;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user