appwrite/src/Appwrite/Utopia/Request.php

174 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Utopia;
use Appwrite\Utopia\Request\Filter;
2024-10-01 14:30:47 +00:00
use Utopia\Http\Adapter\Swoole\Request as HttpRequest;
use Utopia\Http\Route;
2024-10-01 14:30:47 +00:00
class Request extends HttpRequest
{
2024-03-07 13:48:36 +00:00
/**
* @var array<Filter>
*/
2024-03-07 14:41:20 +00:00
private array $filters = [];
private static ?Route $route = null;
2024-10-01 14:30:47 +00:00
/**
* Request constructor.
*/
public function __construct(HttpRequest $request)
2024-03-08 12:57:20 +00:00
{
2024-10-01 14:30:47 +00:00
parent::__construct($request->swoole);
2024-03-08 12:57:20 +00:00
}
/**
* @inheritdoc
*/
public function getParams(): array
{
2022-10-27 21:19:09 +00:00
$parameters = parent::getParams();
2024-03-07 14:41:20 +00:00
if ($this->hasFilters() && self::hasRoute()) {
2024-02-24 12:53:47 +00:00
$method = self::getRoute()->getLabel('sdk.method', 'unknown');
2023-12-12 11:54:05 +00:00
$endpointIdentifier = self::getRoute()->getLabel('sdk.namespace', 'unknown') . '.' . $method;
2024-03-07 13:48:36 +00:00
2024-03-07 14:41:20 +00:00
foreach ($this->getFilters() as $filter) {
$parameters = $filter->parse($parameters, $endpointIdentifier);
}
}
2022-01-04 12:30:50 +00:00
2022-10-27 21:19:09 +00:00
return $parameters;
}
/**
2024-03-07 13:48:36 +00:00
* Function to add a response filter, the order of filters are first in - first out.
*
2024-03-07 13:48:36 +00:00
* @param Filter $filter the response filter to set
*
* @return void
*/
2024-03-07 14:41:20 +00:00
public function addFilter(Filter $filter): void
{
2024-03-07 14:41:20 +00:00
$this->filters[] = $filter;
}
/**
* Return the currently set filter
*
2024-03-07 13:48:36 +00:00
* @return array<Filter>
*/
2024-03-07 14:41:20 +00:00
public function getFilters(): array
2024-03-07 13:48:36 +00:00
{
2024-03-07 14:41:20 +00:00
return $this->filters;
2024-03-07 13:48:36 +00:00
}
/**
* Reset filters
*
* @return void
*/
2024-03-07 14:41:20 +00:00
public function resetFilters(): void
{
2024-03-07 14:41:20 +00:00
$this->filters = [];
}
/**
* Check if a filter has been set
*
* @return bool
*/
2024-03-07 14:41:20 +00:00
public function hasFilters(): bool
{
2024-03-07 14:41:20 +00:00
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
{
2024-03-07 13:48:36 +00:00
return self::$route !== null;
}
2023-10-27 15:23:43 +00:00
2024-10-01 14:30:47 +00:00
public function removeHeader(string $key): static
{
if (isset($this->headers[$key])) {
unset($this->headers[$key]);
}
return parent::removeHeader($key);
}
2023-10-27 15:23:43 +00:00
/**
* Get headers
*
* Method for getting all HTTP header parameters, including cookies.
*
* @return array<string,mixed>
*/
public function getHeaders(): array
{
2024-10-01 14:30:47 +00:00
if ($this->headers !== null) {
return $this->headers;
}
$this->headers = $this->generateHeaders();
2023-10-27 15:23:43 +00:00
2023-10-31 18:25:35 +00:00
if (empty($this->swoole->cookie)) {
2024-10-01 14:30:47 +00:00
return $this->headers;
2023-10-31 18:25:35 +00:00
}
2023-10-27 15:23:43 +00:00
$cookieHeaders = [];
foreach ($this->swoole->cookie as $key => $value) {
$cookieHeaders[] = "{$key}={$value}";
}
if (!empty($cookieHeaders)) {
2024-10-01 14:30:47 +00:00
$this->headers['cookie'] = \implode('; ', $cookieHeaders);
2023-10-27 15:23:43 +00:00
}
2024-10-01 14:30:47 +00:00
return $this->headers;
2023-10-27 15:23:43 +00:00
}
/**
* 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;
}
2022-05-23 14:54:50 +00:00
}