appwrite/src/Appwrite/Utopia/Request.php

161 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace Appwrite\Utopia;
use Appwrite\Utopia\Request\Filter;
2022-01-02 13:25:13 +00:00
use Swoole\Http\Request as SwooleRequest;
use Utopia\Route;
2022-01-02 13:25:13 +00:00
use Utopia\Swoole\Request as UtopiaRequest;
class Request extends UtopiaRequest
{
2024-03-07 13:48:36 +00:00
/**
* @var array<Filter>
*/
private static array $filters = [];
private static ?Route $route = null;
2022-01-02 13:25:13 +00:00
public function __construct(SwooleRequest $request)
{
parent::__construct($request);
}
/**
* @inheritdoc
*/
public function getParams(): array
{
2022-10-27 21:19:09 +00:00
$parameters = parent::getParams();
2024-03-07 13:48:36 +00:00
if (self::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
$parameters = array_reduce(
self::getFilters(),
fn (array $carry, Filter $filter) => $filter->parse($carry, $endpointIdentifier),
$parameters
);
}
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 13:48:36 +00:00
public static function addFilter(Filter $filter): void
{
2024-03-07 13:48:36 +00:00
self::$filters[] = $filter;
}
/**
* Return the currently set filter
*
2024-03-07 13:48:36 +00:00
* @return array<Filter>
*/
public static function getFilters(): array
{
return self::$filters;
}
/**
* Reset filters
*
* @return void
*/
2024-03-07 13:48:36 +00:00
public static function resetFilters(): void
{
2024-03-07 13:48:36 +00:00
self::$filters = [];
}
/**
* Check if a filter has been set
*
* @return bool
*/
2024-03-07 13:48:36 +00:00
public static function hasFilters(): bool
{
2024-03-07 13:48:36 +00:00
return !empty(self::$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
/**
* Get headers
*
* Method for getting all HTTP header parameters, including cookies.
*
* @return array<string,mixed>
*/
public function getHeaders(): array
{
$headers = $this->generateHeaders();
2023-10-31 18:25:35 +00:00
if (empty($this->swoole->cookie)) {
return $headers;
}
2023-10-27 15:23:43 +00:00
$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;
}
2022-05-23 14:54:50 +00:00
}