160 lines
3.4 KiB
PHP
160 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Appwrite\Utopia;
|
|
|
|
use Appwrite\Utopia\Request\Filter;
|
|
use Utopia\Http\Adapter\Swoole\Request as HttpRequest;
|
|
use Utopia\Http\Route;
|
|
|
|
class Request extends HttpRequest
|
|
{
|
|
/**
|
|
* @var array<Filter>
|
|
*/
|
|
private array $filters = [];
|
|
private static ?Route $route = null;
|
|
|
|
/**
|
|
* Request constructor.
|
|
*/
|
|
public function __construct(HttpRequest $request)
|
|
{
|
|
parent::__construct($request->swoole);
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function getParams(): array
|
|
{
|
|
$parameters = parent::getParams();
|
|
|
|
if ($this->hasFilters() && self::hasRoute()) {
|
|
$method = self::getRoute()->getLabel('sdk.method', 'unknown');
|
|
$endpointIdentifier = self::getRoute()->getLabel('sdk.namespace', 'unknown') . '.' . $method;
|
|
|
|
foreach ($this->getFilters() as $filter) {
|
|
$parameters = $filter->parse($parameters, $endpointIdentifier);
|
|
}
|
|
}
|
|
|
|
return $parameters;
|
|
}
|
|
|
|
/**
|
|
* Function to add a response filter, the order of filters are first in - first out.
|
|
*
|
|
* @param Filter $filter the response filter to set
|
|
*
|
|
* @return void
|
|
*/
|
|
public function addFilter(Filter $filter): void
|
|
{
|
|
$this->filters[] = $filter;
|
|
}
|
|
|
|
/**
|
|
* Return the currently set filter
|
|
*
|
|
* @return array<Filter>
|
|
*/
|
|
public function getFilters(): array
|
|
{
|
|
return $this->filters;
|
|
}
|
|
|
|
/**
|
|
* Reset filters
|
|
*
|
|
* @return void
|
|
*/
|
|
public function resetFilters(): void
|
|
{
|
|
$this->filters = [];
|
|
}
|
|
|
|
/**
|
|
* Check if a filter has been set
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function hasFilters(): bool
|
|
{
|
|
return !empty($this->filters);
|
|
}
|
|
|
|
/**
|
|
* Function to set a request route
|
|
*
|
|
* @param Route|null $route the request route to set
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function setRoute(?Route $route): void
|
|
{
|
|
self::$route = $route;
|
|
}
|
|
|
|
/**
|
|
* Return the current route
|
|
*
|
|
* @return Route|null
|
|
*/
|
|
public static function getRoute(): ?Route
|
|
{
|
|
return self::$route;
|
|
}
|
|
|
|
/**
|
|
* Check if a route has been set
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function hasRoute(): bool
|
|
{
|
|
return self::$route !== null;
|
|
}
|
|
|
|
/**
|
|
* Get headers
|
|
*
|
|
* Method for getting all HTTP header parameters, including cookies.
|
|
*
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function getHeaders(): array
|
|
{
|
|
$headers = $this->generateHeaders();
|
|
|
|
if (empty($this->swoole->cookie)) {
|
|
return $headers;
|
|
}
|
|
|
|
$cookieHeaders = [];
|
|
foreach ($this->swoole->cookie as $key => $value) {
|
|
$cookieHeaders[] = "{$key}={$value}";
|
|
}
|
|
|
|
if (!empty($cookieHeaders)) {
|
|
$headers['cookie'] = \implode('; ', $cookieHeaders);
|
|
}
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* Get header
|
|
*
|
|
* Method for querying HTTP header parameters. If $key is not found $default value will be returned.
|
|
*
|
|
* @param string $key
|
|
* @param string $default
|
|
* @return string
|
|
*/
|
|
public function getHeader(string $key, string $default = ''): string
|
|
{
|
|
$headers = $this->getHeaders();
|
|
return $headers[$key] ?? $default;
|
|
}
|
|
}
|