appwrite/src/Appwrite/Utopia/Request.php

145 lines
3.2 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
{
private static ?Filter $filter = null;
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();
if (self::hasFilter() && self::hasRoute()) {
2023-12-12 11:24:50 +00:00
$method = self::getRoute()->getLabel('sdk.method', ['unknown']);
if (\is_array($method)) {
$method = $method[0];
}
$endpointIdentifier = self::getRoute()->getLabel('sdk.namespace', 'unknown') . '.' .$method;
2022-10-27 21:19:09 +00:00
$parameters = self::getFilter()->parse($parameters, $endpointIdentifier);
}
2022-01-04 12:30:50 +00:00
2022-10-27 21:19:09 +00:00
return $parameters;
}
/**
* Function to set a response filter
*
* @param Filter|null $filter Filter the response filter to set
*
* @return void
*/
public static function setFilter(?Filter $filter): void
{
self::$filter = $filter;
}
/**
* Return the currently set filter
*
* @return Filter|null
*/
public static function getFilter(): ?Filter
{
return self::$filter;
}
/**
* Check if a filter has been set
*
* @return bool
*/
public static function hasFilter(): bool
{
return self::$filter != null;
}
/**
* 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;
}
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
}